From 6a0ed63b22c20e3117c4e35c8b2f35e8926b4ccf Mon Sep 17 00:00:00 2001 From: mahmoud Date: Fri, 19 Sep 2014 19:02:23 +0300 Subject: [PATCH 1/7] add path function to work the same as the asset function the difference between those 2 functions is assets always starts from the public/ while asset takes a full path starting from the app/ --- src/Vinelab/Cdn/CdnFacade.php | 51 +++++++++++++++---- src/Vinelab/Cdn/CdnHelper.php | 25 +++++++++ .../Cdn/Contracts/CdnHelperInterface.php | 3 ++ 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/Vinelab/Cdn/CdnFacade.php b/src/Vinelab/Cdn/CdnFacade.php index 4d1cbee..dfa9dfb 100755 --- a/src/Vinelab/Cdn/CdnFacade.php +++ b/src/Vinelab/Cdn/CdnFacade.php @@ -45,7 +45,8 @@ class CdnFacade implements CdnFacadeInterface{ * * @internal param \Vinelab\Cdn\Repository $configurations */ - public function __construct( + public function __construct + ( ProviderFactoryInterface $provider_factory, CdnHelperInterface $helper, CdnFacadeValidator $cdn_facade_validator @@ -58,7 +59,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 +70,52 @@ public function __construct( */ public function asset($path) { - if ( ! isset($path)) - throw new EmptyPathException('Path does not exist.'); + return $this->preparePathAndGenerateUrl('asset', $path); + } - // 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->preparePathAndGenerateUrl('path', $path); } /** - * remove any extra slashes '/' from the path + * responsible of preparing the path before generating the url * + * @param $from * @param $path * - * @return string + * @return mixed + * @throws Exceptions\EmptyPathException */ - private function cleanPath($path) + private function preparePathAndGenerateUrl($from, $path) { - return rtrim(ltrim($path, '/'), '/'); + if ( ! isset($path)) + throw new EmptyPathException('Path does not exist.'); + + // remove slashes from begging and ending of the path then call the + $clean_path = $this->helper->cleanPath($path); + + if ($from == 'asset') + { + // if path starts with public + if ( ! $this->helper->startsWith('public', $clean_path)) + { + // append public/ to the path + $clean_path = 'public/' . $clean_path; + } + } + + 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); } From 3ebee7024d24174fe7e96c03fb413eec1a224c91 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Fri, 19 Sep 2014 19:25:42 +0300 Subject: [PATCH 2/7] update the tests to affect this commit 6a0ed63b22c20e3117c4e35c8b2f35e8926b4ccf changes --- src/Vinelab/Cdn/CdnFacade.php | 7 ++++--- tests/Vinelab/Cdn/CdnFacadeTest.php | 15 ++++----------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Vinelab/Cdn/CdnFacade.php b/src/Vinelab/Cdn/CdnFacade.php index dfa9dfb..bbfd036 100755 --- a/src/Vinelab/Cdn/CdnFacade.php +++ b/src/Vinelab/Cdn/CdnFacade.php @@ -70,9 +70,10 @@ public function __construct */ public function asset($path) { - return $this->preparePathAndGenerateUrl('asset', $path); + return $this->preparePathAndCallUrlGenerator('asset', $path); } + /** * this function will be called from the 'views' using the * 'Cdn' facade {{Cdn::path('')}} to convert the path into @@ -85,7 +86,7 @@ public function asset($path) */ public function path($path) { - return $this->preparePathAndGenerateUrl('path', $path); + return $this->preparePathAndCallUrlGenerator('path', $path); } /** @@ -97,7 +98,7 @@ public function path($path) * @return mixed * @throws Exceptions\EmptyPathException */ - private function preparePathAndGenerateUrl($from, $path) + private function preparePathAndCallUrlGenerator($from, $path) { if ( ! isset($path)) throw new EmptyPathException('Path does not exist.'); diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index b3b14ed..04f7571 100755 --- a/tests/Vinelab/Cdn/CdnFacadeTest.php +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -18,6 +18,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; @@ -44,21 +46,12 @@ public function testAssetIsCallingUrlGenerator() assertEquals($result, $this->cdn_url); } - public function testCleanPathIsCleaning() - { - $path = '/foo/bar/'; - $cleaned_path = 'foo/bar'; - // invoke the private function cleanPath() - $result = $this->invokeMethod($this->facade, 'cleanPath', array($path)); - assertEquals($result, $cleaned_path); - } - /** * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException */ - public function testAssetThrowsExceptionWhenEmptyParameter() + public function testPreparePathThrowsExceptionWhenEmptyParameter() { - $this->facade->asset(null); + $this->invokeMethod($this->facade, 'preparePathAndCallUrlGenerator', array(null, null)); } } From 1595751a2c858728b61edcb7818be188d087570e Mon Sep 17 00:00:00 2001 From: mahmoud Date: Fri, 19 Sep 2014 20:12:27 +0300 Subject: [PATCH 3/7] update the readme file --- readme.md | 33 ++++++++++++++++++++++++--------- src/Vinelab/Cdn/Cdn.php | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-) 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 From 9429cdefed20ef9682cc7a3b6cc74e166e4e84d7 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Mon, 22 Sep 2014 12:53:30 +0300 Subject: [PATCH 4/7] refactor class CdnFacade rename preparePathAndCallUrlGenerator to generateUrl and update this function parameter related to commit #6a0ed63b22c20e3117c4e35c8b2f35e8926b4ccf --- src/Vinelab/Cdn/CdnFacade.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Vinelab/Cdn/CdnFacade.php b/src/Vinelab/Cdn/CdnFacade.php index bbfd036..903f842 100755 --- a/src/Vinelab/Cdn/CdnFacade.php +++ b/src/Vinelab/Cdn/CdnFacade.php @@ -45,11 +45,10 @@ class CdnFacade implements CdnFacadeInterface{ * * @internal param \Vinelab\Cdn\Repository $configurations */ - public function __construct - ( - ProviderFactoryInterface $provider_factory, - CdnHelperInterface $helper, - CdnFacadeValidator $cdn_facade_validator + public function __construct( + ProviderFactoryInterface $provider_factory, + CdnHelperInterface $helper, + CdnFacadeValidator $cdn_facade_validator ) { $this->provider_factory = $provider_factory; $this->helper = $helper; @@ -70,7 +69,7 @@ public function __construct */ public function asset($path) { - return $this->preparePathAndCallUrlGenerator('asset', $path); + return $this->generateUrl($path, 'asset'); } @@ -86,29 +85,30 @@ public function asset($path) */ public function path($path) { - return $this->preparePathAndCallUrlGenerator('path', $path); + return $this->generateUrl($path, 'path'); } /** * responsible of preparing the path before generating the url * - * @param $from * @param $path + * @param $prepend * - * @return mixed * @throws Exceptions\EmptyPathException + * @return */ - private function preparePathAndCallUrlGenerator($from, $path) + private function generateUrl($path, $prepend = '') { if ( ! isset($path)) throw new EmptyPathException('Path does not exist.'); - // remove slashes from begging and ending of the path then call the + // remove slashes from begging and ending of the path $clean_path = $this->helper->cleanPath($path); - if ($from == 'asset') + // if the called of this function is the asset() + if ($prepend == 'asset') { - // if path starts with public + // check if path doesn't starts with public/ if ( ! $this->helper->startsWith('public', $clean_path)) { // append public/ to the path From 0c437d6a7803758d5b12cc8b6437e9c0b518ff41 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Mon, 22 Sep 2014 13:05:01 +0300 Subject: [PATCH 5/7] refactor test CdnFacadeTest rename testPreparePathThrowsExceptionWhenEmptyParameter to testUrlGeneratorThrowsException related to commit #6a0ed63b22c20e3117c4e35c8b2f35e8926b4ccf --- tests/Vinelab/Cdn/CdnFacadeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index 04f7571..67d780f 100755 --- a/tests/Vinelab/Cdn/CdnFacadeTest.php +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -49,9 +49,9 @@ public function testAssetIsCallingUrlGenerator() /** * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException */ - public function testPreparePathThrowsExceptionWhenEmptyParameter() + public function testUrlGeneratorThrowsException() { - $this->invokeMethod($this->facade, 'preparePathAndCallUrlGenerator', array(null, null)); + $this->invokeMethod($this->facade, 'generateUrl', array(null, null)); } } From f3bbce6e765988319bf5dd383e1a8ec55b594fda Mon Sep 17 00:00:00 2001 From: mahmoud Date: Mon, 22 Sep 2014 16:52:35 +0300 Subject: [PATCH 6/7] refactor CdnFacade related to commit #6a0ed63b22c20e3117c4e35c8b2f35e8926b4ccf --- src/Vinelab/Cdn/CdnFacade.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Vinelab/Cdn/CdnFacade.php b/src/Vinelab/Cdn/CdnFacade.php index 903f842..858720f 100755 --- a/src/Vinelab/Cdn/CdnFacade.php +++ b/src/Vinelab/Cdn/CdnFacade.php @@ -69,7 +69,8 @@ public function __construct( */ public function asset($path) { - return $this->generateUrl($path, 'asset'); + // if asset always append the public/ dir to the path (since the user should not add public/ to asset) + return $this->generateUrl($path, 'public/'); } @@ -85,7 +86,7 @@ public function asset($path) */ public function path($path) { - return $this->generateUrl($path, 'path'); + return $this->generateUrl($path); } /** @@ -103,19 +104,10 @@ private function generateUrl($path, $prepend = '') throw new EmptyPathException('Path does not exist.'); // remove slashes from begging and ending of the path - $clean_path = $this->helper->cleanPath($path); - - // if the called of this function is the asset() - if ($prepend == 'asset') - { - // check if path doesn't starts with public/ - if ( ! $this->helper->startsWith('public', $clean_path)) - { - // append public/ to the path - $clean_path = 'public/' . $clean_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); } From 7b577a7cae8f9877fa51b967df21bdf12d2284db Mon Sep 17 00:00:00 2001 From: mahmoud Date: Mon, 22 Sep 2014 17:18:19 +0300 Subject: [PATCH 7/7] fix the test after refactoring the CdnFacade related to commit #6a0ed63b22c20e3117c4e35c8b2f35e8926b4ccf --- tests/Vinelab/Cdn/CdnFacadeTest.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index 67d780f..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'); @@ -36,14 +37,23 @@ public function tearDown() public function testAssetIsCallingUrlGenerator() { $this->provider->shouldReceive('urlGenerator') - ->with($this->asset_path) + ->once() + ->andReturn($this->asset_url); + + $result = $this->facade->asset($this->asset_path); + // assert is calling the url generator + assertEquals($result, $this->asset_url); + } + + public function testPathIsCallingUrlGenerator() + { + $this->provider->shouldReceive('urlGenerator') ->once() - ->andReturn($this->cdn_url); + ->andReturn($this->asset_url); - $result = $this->facade->asset($this->asset_path); - + $result = $this->facade->asset($this->path_path); // assert is calling the url generator - assertEquals($result, $this->cdn_url); + assertEquals($result, $this->asset_url); } /**