diff --git a/readme.md b/readme.md index 52ba262..3dd2ff3 100755 --- a/readme.md +++ b/readme.md @@ -41,7 +41,11 @@ the `Cdn` class to `CdnFacadeProvider` so you can simply use the `Cdn` facade an ## Configuration -Publish the default config using `php artisan config:publish vinelab/cdn` and check it out at `app/config/packages/vinelab/cdn/cdn.php` +Publish the package config file: +```dos +php artisan config:publish vinelab/cdn +``` +and check it out at `app/config/packages/vinelab/cdn/cdn.php` In case you would like to have environment-based configuration `app/config/packages/vinelab/cdn/[ENV]/cdn.php` @@ -138,23 +142,34 @@ not to exceed what you have allowed in your PHP configuration. ### Push -Upload your assets with `php artisan cdn:push` - +Upload your assets with +```dos +php artisan cdn:push +``` ### Load Assets -Since the service provider of this package aliases itself as the facade `Cdn` you may use it as such: +Now you can use the facade `Cdn` to call the `Cdn::asset()` function. +Note: the `asset` works the same as the Laravel `asset` it start looking for assets in the `public/` directory: ```blade - {{Cdn::asset('public/index.php')}} - // https://default-bucket.s3.amazonaws.com/public/index.php - - {{Cdn::asset('public/assets/js/main.js')}} + {{Cdn::asset('assets/js/main.js')}} // https://js-bucket.s3.amazonaws.com/public/assets/js/main.js - {{Cdn::asset('public/assets/css/main.css')}} + {{Cdn::asset('assets/css/main.css')}} // https://css-bucket.s3.amazonaws.com/public/assets/css/main.css ``` +If you want to use a file from outside the `public/` directory anywhere in `app/` you can use the `Cdn::path()` function to do that: + +```blade + {{Cdn::path('public/assets/js/main.js')}} + // https://js-bucket.s3.amazonaws.com/public/assets/js/main.js + + {{Cdn::path('private/something/file.txt')}} + // https://css-bucket.s3.amazonaws.com/private/something/file.txt +``` + + ## Contributing Please see [CONTRIBUTING](https://github.com/Vinelab/cdn/blob/master/CONTRIBUTING.md) for details. diff --git a/src/Vinelab/Cdn/Cdn.php b/src/Vinelab/Cdn/Cdn.php index a48337a..b1324f9 100755 --- a/src/Vinelab/Cdn/Cdn.php +++ b/src/Vinelab/Cdn/Cdn.php @@ -60,7 +60,7 @@ public function push() // return the configurations from the config file $configurations = $this->helper->getConfigurations(); - // Initialize an instance of the asset holder to read the config urations + // Initialize an instance of the asset holder to read the configurations // then call the read(), to return all the allowed assets as a collection of files objects $assets = $this->finder->read($this->asset_holder->init($configurations)); // store the returned assets in the instance of the asset holder as collection of paths diff --git a/src/Vinelab/Cdn/CdnFacade.php b/src/Vinelab/Cdn/CdnFacade.php index 4d1cbee..858720f 100755 --- a/src/Vinelab/Cdn/CdnFacade.php +++ b/src/Vinelab/Cdn/CdnFacade.php @@ -46,9 +46,9 @@ class CdnFacade implements CdnFacadeInterface{ * @internal param \Vinelab\Cdn\Repository $configurations */ public function __construct( - ProviderFactoryInterface $provider_factory, - CdnHelperInterface $helper, - CdnFacadeValidator $cdn_facade_validator + ProviderFactoryInterface $provider_factory, + CdnHelperInterface $helper, + CdnFacadeValidator $cdn_facade_validator ) { $this->provider_factory = $provider_factory; $this->helper = $helper; @@ -58,7 +58,7 @@ public function __construct( } /** - * This function will be called from the 'views' using the + * this function will be called from the 'views' using the * 'Cdn' facade {{Cdn::asset('')}} to convert the path into * it's CDN url * @@ -69,24 +69,46 @@ public function __construct( */ public function asset($path) { - if ( ! isset($path)) - throw new EmptyPathException('Path does not exist.'); + // if asset always append the public/ dir to the path (since the user should not add public/ to asset) + return $this->generateUrl($path, 'public/'); + } - // remove slashes from begging and ending of the path then call the - // url generator of the provider - return $this->provider->urlGenerator($this->cleanPath($path)); + + /** + * this function will be called from the 'views' using the + * 'Cdn' facade {{Cdn::path('')}} to convert the path into + * it's CDN url + * + * @param $path + * + * @return mixed + * @throws Exceptions\EmptyPathException + */ + public function path($path) + { + return $this->generateUrl($path); } /** - * remove any extra slashes '/' from the path + * responsible of preparing the path before generating the url * * @param $path + * @param $prepend * - * @return string + * @throws Exceptions\EmptyPathException + * @return */ - private function cleanPath($path) + private function generateUrl($path, $prepend = '') { - return rtrim(ltrim($path, '/'), '/'); + if ( ! isset($path)) + throw new EmptyPathException('Path does not exist.'); + + // remove slashes from begging and ending of the path + // and append directories if needed + $clean_path = $prepend . $this->helper->cleanPath($path); + + // call the provider specific url generator + return $this->provider->urlGenerator($clean_path); } /** diff --git a/src/Vinelab/Cdn/CdnHelper.php b/src/Vinelab/Cdn/CdnHelper.php index dfcc40a..08b2455 100644 --- a/src/Vinelab/Cdn/CdnHelper.php +++ b/src/Vinelab/Cdn/CdnHelper.php @@ -90,4 +90,29 @@ public function parseUrl($url) return parse_url($url); } + /** + * check if a string starts with a string + * + * @param $with + * @param $str + * + * @return bool + */ + public function startsWith($with, $str) + { + return (substr($str, 0, strlen($with)) === $with); + } + + /** + * remove any extra slashes '/' from the path + * + * @param $path + * + * @return string + */ + public function cleanPath($path) + { + return rtrim(ltrim($path, '/'), '/'); + } + } diff --git a/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php b/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php index 6a80d54..cd677d2 100644 --- a/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php +++ b/src/Vinelab/Cdn/Contracts/CdnHelperInterface.php @@ -12,4 +12,7 @@ public function validate($configuration, $required); public function parseUrl($url); + public function startsWith($haystack, $needle); + + public function cleanPath($path); } diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index b3b14ed..a353791 100755 --- a/tests/Vinelab/Cdn/CdnFacadeTest.php +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -8,8 +8,9 @@ public function setUp() { parent::setUp(); - $this->asset_path = 'public/foo/bar.php'; - $this->cdn_url = 'https://amazon.foo.bar.com'; + $this->asset_path = 'foo/bar.php'; + $this->path_path = 'public/foo/bar.php'; + $this->asset_url = 'https://bucket.s3.amazonaws.com/public/foo/bar.php'; $this->provider = M::mock('Vinelab\Cdn\Providers\AwsS3Provider'); @@ -18,6 +19,8 @@ public function setUp() $this->helper = M::mock('Vinelab\Cdn\Contracts\CdnHelperInterface'); $this->helper->shouldReceive('getConfigurations')->once()->andReturn([]); + $this->helper->shouldReceive('cleanPath')->andReturn($this->asset_path); + $this->helper->shouldReceive('startsWith')->andReturn(true); $this->validator = new \Vinelab\Cdn\Validators\CdnFacadeValidator; @@ -34,31 +37,31 @@ public function tearDown() public function testAssetIsCallingUrlGenerator() { $this->provider->shouldReceive('urlGenerator') - ->with($this->asset_path) - ->once() - ->andReturn($this->cdn_url); + ->once() + ->andReturn($this->asset_url); - $result = $this->facade->asset($this->asset_path); - + $result = $this->facade->asset($this->asset_path); // assert is calling the url generator - assertEquals($result, $this->cdn_url); + assertEquals($result, $this->asset_url); } - public function testCleanPathIsCleaning() + public function testPathIsCallingUrlGenerator() { - $path = '/foo/bar/'; - $cleaned_path = 'foo/bar'; - // invoke the private function cleanPath() - $result = $this->invokeMethod($this->facade, 'cleanPath', array($path)); - assertEquals($result, $cleaned_path); + $this->provider->shouldReceive('urlGenerator') + ->once() + ->andReturn($this->asset_url); + + $result = $this->facade->asset($this->path_path); + // assert is calling the url generator + assertEquals($result, $this->asset_url); } /** * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException */ - public function testAssetThrowsExceptionWhenEmptyParameter() + public function testUrlGeneratorThrowsException() { - $this->facade->asset(null); + $this->invokeMethod($this->facade, 'generateUrl', array(null, null)); } }