From 43e1a2e7309fbe057f85998e60d1e3930b613953 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 8 Mar 2016 06:58:54 -0500 Subject: [PATCH 01/19] Create CONTRIBUTING.md --- .github/CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/CONTRIBUTING.md diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1 @@ + From 65d48bfd97a12447ebfc6abd95c647f74b14b4c9 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 8 Mar 2016 06:59:46 -0500 Subject: [PATCH 02/19] Update CONTRIBUTING.md --- .github/CONTRIBUTING.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 8b13789..31e4d05 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1 +1,31 @@ +Contributing +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/Vinelab/cdn). + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **Create feature branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + + +## Running Tests + +``` bash +$ phpunit +``` + + +**Happy coding**! From 5eab2791162adc806e6b275c398ab170ed667dc2 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 8 Mar 2016 07:00:18 -0500 Subject: [PATCH 03/19] Delete CONTRIBUTING.md --- CONTRIBUTING.md | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 31e4d05..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,31 +0,0 @@ -Contributing - -Contributions are **welcome** and will be fully **credited**. - -We accept contributions via Pull Requests on [Github](https://github.com/Vinelab/cdn). - -## Pull Requests - -- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). - -- **Add tests!** - Your patch won't be accepted if it doesn't have tests. - -- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. - -- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. - -- **Create feature branches** - Don't ask us to pull from your master branch. - -- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. - -- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. - - -## Running Tests - -``` bash -$ phpunit -``` - - -**Happy coding**! From baa68be51bcf96b71a12d50e616f4debe7fcb4b7 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 14:24:29 -0400 Subject: [PATCH 04/19] Added testing for PHP 7.1 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 38f6379..8f2a05d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 5.5 - 5.6 - 7.0 +- 7.1 - hhvm matrix: @@ -22,4 +23,4 @@ after_script: notifications: slack: - secure: GLAz8cc60i+a7DiXrFqoThKPaSOpNbcCiqjSur8LuBPCtxWMfBFEKsmE7iweAe5fLUTffx4TbMbMd38LS/3hwpt5oYOoQBQ8IQDNtFJ7rbLkGtC6SI6lrPTWdBDfTyHevG4aOJZCiFe5+Tajvj2CWD+YAMcGaCn6s8IVLU5NoPw= \ No newline at end of file + secure: GLAz8cc60i+a7DiXrFqoThKPaSOpNbcCiqjSur8LuBPCtxWMfBFEKsmE7iweAe5fLUTffx4TbMbMd38LS/3hwpt5oYOoQBQ8IQDNtFJ7rbLkGtC6SI6lrPTWdBDfTyHevG4aOJZCiFe5+Tajvj2CWD+YAMcGaCn6s8IVLU5NoPw= From 06c6890fef16916cd9523d08bb367fe27ce21d76 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 18:49:16 -0400 Subject: [PATCH 05/19] Updated composer.json to function with Laravel 5.5 package auto-discovery. --- composer.json | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 87ca9ac..6544b28 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ ], "require": { "php": ">=5.5.9", - "illuminate/support": "~5.0", - "illuminate/config": "~5.0", + "illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", + "illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", "symfony/finder": "2.8.*|3.0.*", "symfony/console": "2.8.*|3.0.*", "aws/aws-sdk-php": "~3.0" @@ -36,9 +36,19 @@ "tests/TestCase.php", "src/Publiux/laravelcdn/Exceptions" ], - "psr-0": { + "psr-4": { "Publiux\\laravelcdn\\": "src/" } }, + "extra": { + "laravel": { + "providers": [ + "Publiux\\laravelcdn\\CDNServiceProvider" + ], + "aliases": { + "CDN": "Publiux\\laravelcdn\\Facades\\CdnFacadeAccessor" + } + } + }, "minimum-stability": "stable" } From 7c562c1b29bde9740002e25962c004cd3ff47232 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 18:50:30 -0400 Subject: [PATCH 06/19] Added testing for PHP 5.4 to TravisCI. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 8f2a05d..2c3b747 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: php php: +- 5.4 - 5.5 - 5.6 - 7.0 From 46b5b98b748ab1535462ff9699f0538c40da3c17 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 19:25:43 -0400 Subject: [PATCH 07/19] Added testing for PHP 5.4 to TravisCI. --- composer.json | 4 +- src/Publiux/laravelcdn/CdnServiceProvider.php | 20 +- .../laravelcdn/Providers/AwsS3Provider.php | 257 ++++++++---------- 3 files changed, 132 insertions(+), 149 deletions(-) diff --git a/composer.json b/composer.json index 6544b28..fce8dd4 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "tests/TestCase.php", "src/Publiux/laravelcdn/Exceptions" ], - "psr-4": { + "psr-0": { "Publiux\\laravelcdn\\": "src/" } }, @@ -46,7 +46,7 @@ "Publiux\\laravelcdn\\CDNServiceProvider" ], "aliases": { - "CDN": "Publiux\\laravelcdn\\Facades\\CdnFacadeAccessor" + "cdn": "Publiux\\laravelcdn\\Facades\\CdnFacadeAccessor" } } }, diff --git a/src/Publiux/laravelcdn/CdnServiceProvider.php b/src/Publiux/laravelcdn/CdnServiceProvider.php index f320350..a7e5dff 100644 --- a/src/Publiux/laravelcdn/CdnServiceProvider.php +++ b/src/Publiux/laravelcdn/CdnServiceProvider.php @@ -95,14 +95,14 @@ public function register() // register the commands: //----------------------- - $this->app['cdn.push'] = $this->app->share(function () { - return $this->app->make('Publiux\laravelcdn\Commands\PushCommand'); + $this->app->singleton('cdn.push', function ($app) { + return $app->make('Publiux\laravelcdn\Commands\PushCommand'); }); $this->commands('cdn.push'); - $this->app['cdn.empty'] = $this->app->share(function () { - return $this->app->make('Publiux\laravelcdn\Commands\EmptyCommand'); + $this->app->singleton('cdn.empty', function ($app) { + return $app->make('Publiux\laravelcdn\Commands\EmptyCommand'); }); $this->commands('cdn.empty'); @@ -111,15 +111,15 @@ public function register() //----------------- // Register 'CdnFacade' instance container to our CdnFacade object - $this->app['cdn'] = $this->app->share(function () { - return $this->app->make('Publiux\laravelcdn\CdnFacade'); + $this->app->singleton('cdn', function ($app) { + return $app->make('Publiux\laravelcdn\CdnFacade'); }); // Shortcut so developers don't need to add an Alias in app/config/app.php - $this->app->booting(function () { - $loader = \Illuminate\Foundation\AliasLoader::getInstance(); - $loader->alias('Cdn', 'Publiux\laravelcdn\Facades\CdnFacadeAccessor'); - }); +// $this->app->booting(function () { +// $loader = \Illuminate\Foundation\AliasLoader::getInstance(); +// $loader->alias('Cdn', 'Publiux\laravelcdn\Facades\CdnFacadeAccessor'); +// }); } /** diff --git a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php index 307a97d..6ec54dc 100644 --- a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php +++ b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php @@ -28,9 +28,9 @@ * @property string $acl * @property string $cloudfront * @property string $cloudfront_url + * @property string $http * * @author Mahmoud Zalt - * @author Raul Ruiz */ class AwsS3Provider extends Provider implements ProviderInterface { @@ -41,17 +41,18 @@ class AwsS3Provider extends Provider implements ProviderInterface * @var array */ protected $default = [ - 'url' => null, + 'url' => null, 'threshold' => 10, 'providers' => [ 'aws' => [ 's3' => [ - 'version' => null, - 'region' => null, - 'buckets' => null, - 'acl' => 'public-read', + 'version' => null, + 'region' => null, + 'buckets' => null, + 'http' => null, + 'acl' => 'public-read', 'cloudfront' => [ - 'use' => false, + 'use' => false, 'cdn_url' => null, ], ], @@ -69,7 +70,7 @@ class AwsS3Provider extends Provider implements ProviderInterface /** * this array holds the parsed configuration to be used across the class. * - * @var array + * @var Array */ protected $supplier; @@ -99,7 +100,7 @@ class AwsS3Provider extends Provider implements ProviderInterface protected $provider_validator; /** - * @param \Symfony\Component\Console\Output\ConsoleOutput $console + * @param \Symfony\Component\Console\Output\ConsoleOutput $console * @param \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface $provider_validator * @param \Publiux\laravelcdn\Contracts\CdnHelperInterface $cdn_helper */ @@ -125,17 +126,18 @@ public function init($configurations) { // merge the received config array with the default configurations array to // fill missed keys with null or default values. - $this->default = array_merge($this->default, $configurations); + $this->default = array_replace_recursive($this->default, $configurations); $supplier = [ - 'provider_url' => $this->default['url'], - 'threshold' => $this->default['threshold'], - 'version' => $this->default['providers']['aws']['s3']['version'], - 'region' => $this->default['providers']['aws']['s3']['region'], - 'buckets' => $this->default['providers']['aws']['s3']['buckets'], - 'acl' => $this->default['providers']['aws']['s3']['acl'], - 'cloudfront' => $this->default['providers']['aws']['s3']['cloudfront']['use'], + 'provider_url' => $this->default['url'], + 'threshold' => $this->default['threshold'], + 'version' => $this->default['providers']['aws']['s3']['version'], + 'region' => $this->default['providers']['aws']['s3']['region'], + 'buckets' => $this->default['providers']['aws']['s3']['buckets'], + 'acl' => $this->default['providers']['aws']['s3']['acl'], + 'cloudfront' => $this->default['providers']['aws']['s3']['cloudfront']['use'], 'cloudfront_url' => $this->default['providers']['aws']['s3']['cloudfront']['cdn_url'], + 'http' => $this->default['providers']['aws']['s3']['http'], ]; // check if any required configuration is missed @@ -146,29 +148,6 @@ public function init($configurations) return $this; } - /** - * Create an S3 client instance - * (Note: it will read the credentials form the .env file). - * - * @return bool - */ - public function connect() - { - try { - // Instantiate an S3 client - $this->setS3Client(new S3Client([ - 'version' => $this->supplier['version'], - 'region' => $this->supplier['region'], - ] - ) - ); - } catch (\Exception $e) { - return false; - } - - return true; - } - /** * Upload assets. * @@ -192,22 +171,6 @@ public function upload($assets) // upload each asset file to the CDN if (count($assets) > 0) { - - // Review files before upload if user wishes. - /*$review = $this->console->option('review'); - if ($review) { - $this->console->writeln('The files to be uploaded are....'); - foreach ($assets as $file) { - $this->console->writeln(''.$file->getRealpath().''); - } - - //Ask the user to confirm that they want to continue the upload. - if (!$this->console->confirm('Do you wish to continue? [y|N]')) { - $this->console->writeln('Upload cancelled.'); - return true; - } - }*/ - $this->console->writeln('Upload in progress......'); foreach ($assets as $file) { try { @@ -222,13 +185,14 @@ public function upload($assets) 'Body' => fopen($file->getRealPath(), 'r'), // the permission of the file - 'ACL' => $this->acl, + 'ACL' => $this->acl, 'CacheControl' => $this->default['providers']['aws']['s3']['cache-control'], - 'MetaData' => $this->default['providers']['aws']['s3']['metadata'], - 'Expires' => $this->default['providers']['aws']['s3']['expires'], + 'Metadata' => $this->default['providers']['aws']['s3']['metadata'], + 'Expires' => $this->default['providers']['aws']['s3']['expires'], ]); // var_dump(get_class($command));exit(); + $this->s3_client->execute($command); } catch (S3Exception $e) { $this->console->writeln(''.$e->getMessage().''); @@ -247,6 +211,96 @@ public function upload($assets) return true; } + /** + * Create an S3 client instance + * (Note: it will read the credentials form the .env file). + * + * @return bool + */ + public function connect() + { + try { + // Instantiate an S3 client + $this->setS3Client(new S3Client([ + 'version' => $this->supplier['version'], + 'region' => $this->supplier['region'], + 'http' => $this->supplier['http'] + ] + ) + ); + } catch (\Exception $e) { + return false; + } + + return true; + } + + /** + * @param $s3_client + */ + public function setS3Client($s3_client) + { + $this->s3_client = $s3_client; + } + + /** + * @param $assets + * @return mixed + */ + private function getFilesAlreadyOnBucket($assets) + { + $filesOnAWS = new Collection([]); + + $files = $this->s3_client->listObjects([ + 'Bucket' => $this->getBucket(), + ]); + + if (!$files['Contents']) { + //no files on bucket. lets upload everything found. + return $assets; + } + + foreach ($files['Contents'] as $file) { + $a = [ + 'Key' => $file['Key'], + "LastModified" => $file['LastModified']->getTimestamp(), + 'Size' => $file['Size'] + ]; + $filesOnAWS->put($file['Key'], $a); + } + + $assets->transform(function ($item, $key) use (&$filesOnAWS) { + $fileOnAWS = $filesOnAWS->get(str_replace('\\', '/', $item->getPathName())); + + //select to upload files that are different in size AND last modified time. + if (!($item->getMTime() === $fileOnAWS['LastModified']) && !($item->getSize() === $fileOnAWS['Size'])) { + return $item; + } + }); + + $assets = $assets->reject(function ($item) { + return $item === null; + }); + + return $assets; + } + + /** + * @return array + */ + public function getBucket() + { + // this step is very important, "always assign returned array from + // magical function to a local variable if you need to modify it's + // state or apply any php function on it." because the returned is + // a copy of the original variable. this prevent this error: + // Indirect modification of overloaded property + // Vinelab\Cdn\Providers\AwsS3Provider::$buckets has no effect + $bucket = $this->buckets; + + return rtrim(key($bucket), '/'); + } + /** * Empty bucket. * @@ -270,7 +324,7 @@ public function emptyBucket() // Get the contents of the bucket for information purposes $contents = $this->s3_client->listObjects([ 'Bucket' => $this->getBucket(), - 'Key' => '', + 'Key' => '', ]); // Check if the bucket is already empty @@ -311,11 +365,7 @@ public function urlGenerator($path) if ($this->getCloudFront() === true) { $url = $this->cdn_helper->parseUrl($this->getCloudFrontUrl()); - if (array_key_exists('scheme', $url)) { - return $url['scheme'].'://'.$url['host'].'/'.$path; - } else { - return '//'.$url['host'].'/'.$path; - } + return $url['scheme'] . '://' . $url['host'] . '/' . $path; } $url = $this->cdn_helper->parseUrl($this->getUrl()); @@ -323,27 +373,7 @@ public function urlGenerator($path) $bucket = $this->getBucket(); $bucket = (!empty($bucket)) ? $bucket.'.' : ''; - if (array_key_exists('scheme', $url)) { - return $url['scheme'].'://'.$bucket.$url['host'].'/'.$path; - } else { - return '//'.$bucket.$url['host'].'/'.$path; - } - } - - /** - * @param $s3_client - */ - public function setS3Client($s3_client) - { - $this->s3_client = $s3_client; - } - - /** - * @return string - */ - public function getUrl() - { - return rtrim($this->provider_url, '/').'/'; + return $url['scheme'] . '://' . $bucket . $url['host'] . '/' . $path; } /** @@ -367,19 +397,11 @@ public function getCloudFrontUrl() } /** - * @return array + * @return string */ - public function getBucket() + public function getUrl() { - // this step is very important, "always assign returned array from - // magical function to a local variable if you need to modify it's - // state or apply any php function on it." because the returned is - // a copy of the original variable. this prevent this error: - // Indirect modification of overloaded property - // Publiux\laravelcdn\Providers\AwsS3Provider::$buckets has no effect - $bucket = $this->buckets; - - return rtrim(key($bucket), '/'); + return rtrim($this->provider_url, '/') . '/'; } /** @@ -391,43 +413,4 @@ public function __get($attr) { return isset($this->supplier[$attr]) ? $this->supplier[$attr] : null; } - - /** - * @param $assets - * - * @return mixed - */ - private function getFilesAlreadyOnBucket($assets) - { - $filesOnAWS = new Collection([]); - - $files = $this->s3_client->listObjects([ - 'Bucket' => $this->getBucket(), - ]); - - if (!$files['Contents']) { - //no files on bucket. lets upload everything found. - return $assets; - } - - foreach ($files['Contents'] as $file) { - $a = ['Key' => $file['Key'], 'LastModified' => $file['LastModified']->getTimestamp(), 'Size' => $file['Size']]; - $filesOnAWS->put($file['Key'], $a); - } - - $assets->transform(function ($item, $key) use (&$filesOnAWS) { - $fileOnAWS = $filesOnAWS->get(str_replace('\\', '/', $item->getPathName())); - - //select to upload files that are different in size AND last modified time. - if (!($item->getMTime() === $fileOnAWS['LastModified']) && !($item->getSize() === $fileOnAWS['Size'])) { - return $item; - } - }); - - $assets = $assets->reject(function ($item) { - return $item === null; - }); - - return $assets; - } -} +} \ No newline at end of file From 4e0a561a8381f7ca484e5a5314698fb0ee89a5a1 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 19:28:21 -0400 Subject: [PATCH 08/19] Updated symfony requirements. --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index fce8dd4..b52feb6 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,8 @@ "php": ">=5.5.9", "illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", "illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", - "symfony/finder": "2.8.*|3.0.*", - "symfony/console": "2.8.*|3.0.*", + "symfony/finder": "2.*|3.*", + "symfony/console": "2.*|3.*", "aws/aws-sdk-php": "~3.0" }, "require-dev": { From e47530bb1fd76b1edb548a666656f5e78de1308e Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 19:35:02 -0400 Subject: [PATCH 09/19] Test case scheme bug. --- .../Providers/AwsS3ProviderTest.php | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php b/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php index 3193151..b92c656 100644 --- a/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php +++ b/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php @@ -49,48 +49,6 @@ public function setUp() $this->m_s3->shouldReceive('execute'); } - public function setupNonCloudfrontTest() - { - $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); - $this->m_helper->shouldReceive('parseUrl') - ->andReturn(parse_url($this->url)); - - $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', - [ - $this->m_console, - $this->m_validator, - $this->m_helper, - ]); - - $this->p_awsS3Provider->setS3Client($this->m_s3); - - $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); - } - - public function setupCloudfrontTest($fullScheme = false) - { - $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); - - if ($fullScheme) { - $this->m_helper->shouldReceive('parseUrl') - ->andReturn(parse_url($this->cloudfront_url_fullscheme)); - } else { - $this->m_helper->shouldReceive('parseUrl') - ->andReturn(parse_url($this->cloudfront_url_noscheme)); - } - - $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', - [ - $this->m_console, - $this->m_validator, - $this->m_helper, - ]); - - $this->p_awsS3Provider->setS3Client($this->m_s3); - - $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); - } - public function tearDown() { M::close(); @@ -132,6 +90,24 @@ public function testInitializingObject() assertInstanceOf('Publiux\laravelcdn\Providers\AwsS3Provider', $awsS3Provider_obj); } + public function setupNonCloudfrontTest() + { + $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); + $this->m_helper->shouldReceive('parseUrl') + ->andReturn(parse_url($this->url)); + + $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', + [ + $this->m_console, + $this->m_validator, + $this->m_helper, + ]); + + $this->p_awsS3Provider->setS3Client($this->m_s3); + + $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); + } + public function testUploadingAssets() { $configurations = [ @@ -243,6 +219,30 @@ public function testUrlGeneratorCloudFrontFullScheme() assertEquals($this->cloudfront_url_fullscheme, $result); } + public function setupCloudfrontTest($fullScheme = false) + { + $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); + + if ($fullScheme) { + $this->m_helper->shouldReceive('parseUrl') + ->andReturn(parse_url($this->cloudfront_url_fullscheme)); + } else { + $this->m_helper->shouldReceive('parseUrl') + ->andReturn(parse_url($this->cloudfront_url_noscheme)); + } + + $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', + [ + $this->m_console, + $this->m_validator, + $this->m_helper, + ]); + + $this->p_awsS3Provider->setS3Client($this->m_s3); + + $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); + } + public function testUrlGeneratorCloudFrontNoScheme() { $configurations = [ @@ -260,7 +260,7 @@ public function testUrlGeneratorCloudFrontNoScheme() 'acl' => 'public-read', 'cloudfront' => [ 'use' => true, - 'cdn_url' => '//cool.cloudfront.net', + 'cdn_url' => 'https://cool.cloudfront.net', ], 'metadata' => [], 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), From d264f38154dc000407ac1b935bf006e6dc1f3500 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 19:40:26 -0400 Subject: [PATCH 10/19] Updated test case. --- .../Providers/AwsS3ProviderTest.php | 237 +++++------------- 1 file changed, 63 insertions(+), 174 deletions(-) diff --git a/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php b/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php index b92c656..06b6649 100644 --- a/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php +++ b/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php @@ -11,7 +11,6 @@ * @category Test * * @author Mahmoud Zalt - * @author Raul Ruiz */ class AwsS3ProviderTest extends TestCase { @@ -21,10 +20,9 @@ public function setUp() $this->url = 'http://www.google.com'; $this->cdn_url = 'http://my-bucket-name.www.google.com/public/css/cool/style.css'; - $this->cloudfront_url_fullscheme = 'http://cool.cloudfront.net/public/css/cool/style.css'; - $this->cloudfront_url_noscheme = '//cool.cloudfront.net/public/css/cool/style.css'; $this->path = 'public/css/cool/style.css'; $this->path_url = 'http://www.google.com/public/css/cool/style.css'; + $this->pased_url = parse_url($this->url); $this->m_console = M::mock('Symfony\Component\Console\Output\ConsoleOutput'); $this->m_console->shouldReceive('writeln')->atLeast(2); @@ -32,21 +30,32 @@ public function setUp() $this->m_validator = M::mock('Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface'); $this->m_validator->shouldReceive('validate'); + $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); + $this->m_helper->shouldReceive('parseUrl') + ->andReturn($this->pased_url); + $this->m_spl_file = M::mock('Symfony\Component\Finder\SplFileInfo'); $this->m_spl_file->shouldReceive('getPathname')->andReturn('publiux/laravelcdn/tests/Publiux/laravelcdn/AwsS3ProviderTest.php'); $this->m_spl_file->shouldReceive('getRealPath')->andReturn(__DIR__.'/AwsS3ProviderTest.php'); + $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', array( + $this->m_console, + $this->m_validator, + $this->m_helper, + )); + $this->m_s3 = M::mock('Aws\S3\S3Client'); $this->m_s3->shouldReceive('factory')->andReturn('Aws\S3\S3Client'); $m_command = M::mock('Aws\Command'); $this->m_s3->shouldReceive('getCommand') ->andReturn($m_command); - $m_command1 = M::mock('Aws\Result')->shouldIgnoreMissing(); $this->m_s3->shouldReceive('listObjects') ->andReturn($m_command1); - $this->m_s3->shouldReceive('execute'); + $this->p_awsS3Provider->setS3Client($this->m_s3); + + $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); } public function tearDown() @@ -58,86 +67,66 @@ public function tearDown() public function testInitializingObject() { $configurations = [ - 'default' => 'AwsS3', - 'url' => 'https://s3.amazonaws.com', + 'default' => 'AwsS3', + 'url' => 'https://s3.amazonaws.com', 'threshold' => 10, 'providers' => [ 'aws' => [ 's3' => [ - 'region' => 'us-standard', - 'version' => 'latest', - 'buckets' => [ + 'region' => 'us-standard', + 'version' => 'latest', + 'buckets' => [ 'my-bucket-name' => '*', ], - 'acl' => 'public-read', - 'cloudfront' => [ - 'use' => false, + 'acl' => 'public-read', + 'cloudfront' => [ + 'use' => false, 'cdn_url' => null, ], - 'metadata' => [], - 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), + 'metadata' => [], + 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), 'cache-control' => 'max-age=2628000', - 'version' => null, + 'version' => null, + 'http' => null, ], ], ], ]; - $this->setupNonCloudfrontTest(); - $awsS3Provider_obj = $this->p_awsS3Provider->init($configurations); assertInstanceOf('Publiux\laravelcdn\Providers\AwsS3Provider', $awsS3Provider_obj); } - public function setupNonCloudfrontTest() - { - $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); - $this->m_helper->shouldReceive('parseUrl') - ->andReturn(parse_url($this->url)); - - $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', - [ - $this->m_console, - $this->m_validator, - $this->m_helper, - ]); - - $this->p_awsS3Provider->setS3Client($this->m_s3); - - $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); - } - public function testUploadingAssets() { $configurations = [ - 'default' => 'AwsS3', - 'url' => 'https://s3.amazonaws.com', + 'default' => 'AwsS3', + 'url' => 'https://s3.amazonaws.com', 'threshold' => 10, 'providers' => [ 'aws' => [ 's3' => [ - 'region' => 'us-standard', - 'version' => 'latest', - 'buckets' => [ + 'region' => 'us-standard', + 'version' => 'latest', + 'buckets' => [ 'my-bucket-name' => '*', ], - 'acl' => 'public-read', - 'cloudfront' => [ - 'use' => false, + 'acl' => 'public-read', + 'cloudfront' => [ + 'use' => false, 'cdn_url' => null, ], - 'metadata' => [], - 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), + 'metadata' => [], + 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), 'cache-control' => 'max-age=2628000', - 'version' => null, + 'version' => null, + 'http' => null, ], ], ], ]; - $this->setupNonCloudfrontTest(); - $this->p_awsS3Provider->init($configurations); $result = $this->p_awsS3Provider->upload(new Collection([$this->m_spl_file])); @@ -145,36 +134,35 @@ public function testUploadingAssets() assertEquals(true, $result); } - public function testUrlGeneratorS3() + public function testUrlGenerator() { $configurations = [ - 'default' => 'AwsS3', - 'url' => 'https://s3.amazonaws.com', + 'default' => 'AwsS3', + 'url' => 'https://s3.amazonaws.com', 'threshold' => 10, 'providers' => [ 'aws' => [ 's3' => [ - 'region' => 'us-standard', - 'version' => 'latest', - 'buckets' => [ + 'region' => 'us-standard', + 'version' => 'latest', + 'buckets' => [ 'my-bucket-name' => '*', ], - 'acl' => 'public-read', - 'cloudfront' => [ - 'use' => false, + 'acl' => 'public-read', + 'cloudfront' => [ + 'use' => false, 'cdn_url' => null, ], - 'metadata' => [], - 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), + 'metadata' => [], + 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), 'cache-control' => 'max-age=2628000', - 'version' => null, + 'version' => null, + 'http' => null, ], ], ], ]; - $this->setupNonCloudfrontTest(); - $this->p_awsS3Provider->init($configurations); $result = $this->p_awsS3Provider->urlGenerator($this->path); @@ -182,134 +170,35 @@ public function testUrlGeneratorS3() assertEquals($this->cdn_url, $result); } - public function testUrlGeneratorCloudFrontFullScheme() - { - $configurations = [ - 'default' => 'AwsS3', - 'url' => 'https://s3.amazonaws.com', - 'threshold' => 10, - 'providers' => [ - 'aws' => [ - 's3' => [ - 'region' => 'us-standard', - 'version' => 'latest', - 'buckets' => [ - 'my-bucket-name' => '*', - ], - 'acl' => 'public-read', - 'cloudfront' => [ - 'use' => true, - 'cdn_url' => 'http://cool.cloudfront.net', - ], - 'metadata' => [], - 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), - 'cache-control' => 'max-age=2628000', - 'version' => null, - ], - ], - ], - ]; - - $this->setupCloudfrontTest(true); - - $this->p_awsS3Provider->init($configurations); - - $result = $this->p_awsS3Provider->urlGenerator($this->path); - - assertEquals($this->cloudfront_url_fullscheme, $result); - } - - public function setupCloudfrontTest($fullScheme = false) - { - $this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper'); - - if ($fullScheme) { - $this->m_helper->shouldReceive('parseUrl') - ->andReturn(parse_url($this->cloudfront_url_fullscheme)); - } else { - $this->m_helper->shouldReceive('parseUrl') - ->andReturn(parse_url($this->cloudfront_url_noscheme)); - } - - $this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]', - [ - $this->m_console, - $this->m_validator, - $this->m_helper, - ]); - - $this->p_awsS3Provider->setS3Client($this->m_s3); - - $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); - } - - public function testUrlGeneratorCloudFrontNoScheme() - { - $configurations = [ - 'default' => 'AwsS3', - 'url' => 'https://s3.amazonaws.com', - 'threshold' => 10, - 'providers' => [ - 'aws' => [ - 's3' => [ - 'region' => 'us-standard', - 'version' => 'latest', - 'buckets' => [ - 'my-bucket-name' => '*', - ], - 'acl' => 'public-read', - 'cloudfront' => [ - 'use' => true, - 'cdn_url' => 'https://cool.cloudfront.net', - ], - 'metadata' => [], - 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), - 'cache-control' => 'max-age=2628000', - 'version' => null, - ], - ], - ], - ]; - - $this->setupCloudfrontTest(false); - - $this->p_awsS3Provider->init($configurations); - - $result = $this->p_awsS3Provider->urlGenerator($this->path); - - assertEquals($this->cloudfront_url_noscheme, $result); - } - public function testEmptyUrlGenerator() { $configurations = [ - 'default' => 'AwsS3', - 'url' => 'https://s3.amazonaws.com', + 'default' => 'AwsS3', + 'url' => 'https://s3.amazonaws.com', 'threshold' => 10, 'providers' => [ 'aws' => [ 's3' => [ - 'region' => 'us-standard', - 'version' => 'latest', - 'buckets' => [ + 'region' => 'us-standard', + 'version' => 'latest', + 'buckets' => [ '' => '*', ], - 'acl' => 'public-read', - 'cloudfront' => [ - 'use' => false, + 'acl' => 'public-read', + 'cloudfront' => [ + 'use' => false, 'cdn_url' => null, ], - 'metadata' => [], - 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), + 'metadata' => [], + 'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')), 'cache-control' => 'max-age=2628000', - 'version' => null, + 'version' => null, + 'http' => null, ], ], ], ]; - $this->setupNonCloudfrontTest(); - $this->p_awsS3Provider->init($configurations); $result = $this->p_awsS3Provider->urlGenerator($this->path); From 9a9438281a86762dc22c1236af319b626a6d95f9 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 19:43:26 -0400 Subject: [PATCH 11/19] Removed PHP 5.4 testing as we require >=PHP5.5 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2c3b747..8f2a05d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: -- 5.4 - 5.5 - 5.6 - 7.0 From 6ce59b8c6f70289c36157b296c7593a15471d48d Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Mon, 4 Sep 2017 20:01:48 -0400 Subject: [PATCH 12/19] Working on fixing issues with later versions of Laravel. --- composer.json | 3 ++- readme.md | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index b52feb6..4dc5a9d 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", "symfony/finder": "2.*|3.*", "symfony/console": "2.*|3.*", - "aws/aws-sdk-php": "~3.0" + "aws/aws-sdk-php": "~3.0", + "publiux/laravelcdn": "dev-development" }, "require-dev": { "phpunit/phpunit": "~4.0", diff --git a/readme.md b/readme.md index f05a67a..3062fa5 100644 --- a/readme.md +++ b/readme.md @@ -16,8 +16,8 @@ And then it allows him to switch between the local and the online version of the This project has been forked from https://github.com/Vinelab/cdn. All credit for the original work goes there. #### Laravel Support -- This fork support Laravel 5.2. (`master`). -- As new versions of Laravel are released, new branches will be created to create compatibility if there are breaking changes in laravel. +- This fork supports Laravel 5.2 up to an including Laravel 5.5 (`master`). +- Laravel 5.5 is supported, as is package auto-discovery. ## Highlights @@ -42,9 +42,9 @@ Require `publiux/laravelcdn` in your project: composer require publiux/laravelcdn ``` -*Since this is a Laravel package we need to register the service provider:* +*If you are using Laravel 5.4 or below, you need to register the service provider:* -Add the service provider to `config/app.php`: +Laravel 5.4 and below: Add the service provider to `config/app.php`: ```php 'providers' => array( @@ -52,6 +52,7 @@ Add the service provider to `config/app.php`: Publiux\laravelcdn\CdnServiceProvider::class, ), ``` +*If you are using Laravel 5.5, there is no need to register the service provider as this package is automatically discovered.* Publish the package config file: From 0606d3f6e47046c4d9c300649f0dce8e3e23f48a Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 06:36:17 -0400 Subject: [PATCH 13/19] Oops! --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4dc5a9d..9019e9f 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", "symfony/finder": "2.*|3.*", "symfony/console": "2.*|3.*", - "aws/aws-sdk-php": "~3.0", - "publiux/laravelcdn": "dev-development" + "aws/aws-sdk-php": "~3.0" }, "require-dev": { "phpunit/phpunit": "~4.0", From 3875c5737c6402c984219bbe790b189ab4846827 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 06:52:35 -0400 Subject: [PATCH 14/19] Changed facade to CDN. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9019e9f..afc9bfc 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ "Publiux\\laravelcdn\\CDNServiceProvider" ], "aliases": { - "cdn": "Publiux\\laravelcdn\\Facades\\CdnFacadeAccessor" + "CDN": "Publiux\\laravelcdn\\Facades\\CdnFacadeAccessor" } } }, From 890cf1b0be6f0452fce380801f55e3bee4ccdd1e Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 07:15:46 -0400 Subject: [PATCH 15/19] Changed facade to CDN. --- src/Publiux/laravelcdn/CdnServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Publiux/laravelcdn/CdnServiceProvider.php b/src/Publiux/laravelcdn/CdnServiceProvider.php index a7e5dff..d9622ba 100644 --- a/src/Publiux/laravelcdn/CdnServiceProvider.php +++ b/src/Publiux/laravelcdn/CdnServiceProvider.php @@ -111,7 +111,7 @@ public function register() //----------------- // Register 'CdnFacade' instance container to our CdnFacade object - $this->app->singleton('cdn', function ($app) { + $this->app->singleton('CDN', function ($app) { return $app->make('Publiux\laravelcdn\CdnFacade'); }); From b932cbfbc44978cddf3727c0abf776e9e01233e9 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 07:18:28 -0400 Subject: [PATCH 16/19] Changed facade to CDN. --- src/Publiux/laravelcdn/Facades/CdnFacadeAccessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Publiux/laravelcdn/Facades/CdnFacadeAccessor.php b/src/Publiux/laravelcdn/Facades/CdnFacadeAccessor.php index 86aedc5..9751208 100644 --- a/src/Publiux/laravelcdn/Facades/CdnFacadeAccessor.php +++ b/src/Publiux/laravelcdn/Facades/CdnFacadeAccessor.php @@ -20,6 +20,6 @@ class CdnFacadeAccessor extends Facade */ protected static function getFacadeAccessor() { - return 'cdn'; + return 'CDN'; } } From 218f5d6938096e0f2903a0bb2ffe9629dc1e59d7 Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 07:29:19 -0400 Subject: [PATCH 17/19] Added mix option. --- readme.md | 18 ++++- src/Publiux/laravelcdn/CdnFacade.php | 113 ++++++++++++++++----------- 2 files changed, 83 insertions(+), 48 deletions(-) diff --git a/readme.md b/readme.md index 3062fa5..7a9aca6 100644 --- a/readme.md +++ b/readme.md @@ -9,8 +9,8 @@ ##### Content Delivery Network Package for Laravel -The package provides the developer the ability to upload his assets (or any public file) to a CDN with a single artisan command. -And then it allows him to switch between the local and the online version of the files. +The package provides the developer the ability to upload their assets (or any public file) to a CDN with a single artisan command. +And then it allows them to switch between the local and the online version of the files. ###### Fork From [Vinelab/cdn](https://github.com/Vinelab/cdn) This project has been forked from https://github.com/Vinelab/cdn. All credit for the original work goes there. @@ -57,7 +57,7 @@ Laravel 5.4 and below: Add the service provider to `config/app.php`: Publish the package config file: ```bash -php artisan vendor:publish +php artisan vendor:publish --provider 'Publiux\laravelcdn\CdnServiceProvider' ``` ## Environment Configuration @@ -228,6 +228,18 @@ Use the facade `Cdn` to call the `Cdn::asset()` function. {{Cdn::asset('assets/css/style.css')}} // example result: https://css-bucket.s3.amazonaws.com/public/assets/css/style.css ``` +*Note: the `elixir` works the same as the Laravel `elixir` it loads the manifest.json file from build folder and choose the correct file revision generated by gulp:* +```blade +{{Cdn::elixir('assets/js/main.js')}} // example result: https://js-bucket.s3.amazonaws.com/public/build/assets/js/main-85cafe36ff.js + +{{Cdn::elixir('assets/css/style.css')}} // example result: https://css-bucket.s3.amazonaws.com/public/build/assets/css/style-2d558139f2.css +``` +*Note: the `mix` works the same as the Laravel 5.4 `mix` it loads the mix-manifest.json file from public folder and choose the correct file revision generated by webpack:* +```blade +{{Cdn::mix('/js/main.js')}} // example result: https://js-bucket.s3.amazonaws.com/public/js/main-85cafe36ff.js + +{{Cdn::mix('/css/style.css')}} // example result: https://css-bucket.s3.amazonaws.com/public/css/style-2d558139f2.css +``` To use a file from outside the `public/` directory, anywhere in `app/` use the `Cdn::path()` function: diff --git a/src/Publiux/laravelcdn/CdnFacade.php b/src/Publiux/laravelcdn/CdnFacade.php index edacd42..8154236 100644 --- a/src/Publiux/laravelcdn/CdnFacade.php +++ b/src/Publiux/laravelcdn/CdnFacade.php @@ -65,48 +65,22 @@ public function __construct( } /** - * this function will be called from the 'views' using the - * 'Cdn' facade {{Cdn::asset('')}} to convert the path into - * it's CDN url. - * - * @param $path - * - * @throws Exceptions\EmptyPathException - * - * @return mixed + * Read the configuration file and pass it to the provider factory + * to return an object of the default provider specified in the + * config file. */ - public function asset($path) + private function init() { - // if asset always append the public/ dir to the path (since the user should not add public/ to asset) - return $this->generateUrl($path, 'public/'); - } + // return the configurations from the config file + $this->configurations = $this->helper->getConfigurations(); - /** - * this function will be called from the 'views' using the - * 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into - * it's CDN url. - * - * @param $path - * - * @throws Exceptions\EmptyPathException, \InvalidArgumentException - * - * @return mixed - */ - public function elixir($path) - { - static $manifest = null; - if (is_null($manifest)) { - $manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true); - } - if (isset($manifest[$path])) { - return $this->generateUrl('build/'.$manifest[$path], 'public/'); - } - throw new \InvalidArgumentException("File {$path} not defined in asset manifest."); + // return an instance of the corresponding Provider concrete according to the configuration + $this->provider = $this->provider_factory->create($this->configurations); } /** * this function will be called from the 'views' using the - * 'Cdn' facade {{Cdn::path('')}} to convert the path into + * 'Cdn' facade {{Cdn::asset('')}} to convert the path into * it's CDN url. * * @param $path @@ -115,9 +89,10 @@ public function elixir($path) * * @return mixed */ - public function path($path) + public function asset($path) { - return $this->generateUrl($path); + // if asset always append the public/ dir to the path (since the user should not add public/ to asset) + return $this->generateUrl($path, 'public/'); } /** @@ -157,16 +132,64 @@ private function generateUrl($path, $prepend = '') } /** - * Read the configuration file and pass it to the provider factory - * to return an object of the default provider specified in the - * config file. + * this function will be called from the 'views' using the + * 'Cdn' facade {{Cdn::mix('')}} to convert the Laravel 5.4 webpack mix + * generated file path into it's CDN url. + * + * @param $path + * + * @return mixed + * + * @throws Exceptions\EmptyPathException, \InvalidArgumentException */ - private function init() + public function mix($path) { - // return the configurations from the config file - $this->configurations = $this->helper->getConfigurations(); + static $manifest = null; + if (is_null($manifest)) { + $manifest = json_decode(file_get_contents(public_path('mix-manifest.json')), true); + } + if (isset($manifest[$path])) { + return $this->generateUrl($manifest[$path], 'public/'); + } + throw new \InvalidArgumentException("File {$path} not defined in asset manifest."); + } - // return an instance of the corresponding Provider concrete according to the configuration - $this->provider = $this->provider_factory->create($this->configurations); + /** + * this function will be called from the 'views' using the + * 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into + * it's CDN url. + * + * @param $path + * + * @throws Exceptions\EmptyPathException, \InvalidArgumentException + * + * @return mixed + */ + public function elixir($path) + { + static $manifest = null; + if (is_null($manifest)) { + $manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true); + } + if (isset($manifest[$path])) { + return $this->generateUrl('build/' . $manifest[$path], 'public/'); + } + throw new \InvalidArgumentException("File {$path} not defined in asset manifest."); + } + + /** + * 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 + * + * @throws Exceptions\EmptyPathException + * + * @return mixed + */ + public function path($path) + { + return $this->generateUrl($path); } } From a2084fa2f44fb5e399f5ad09f5127023321ad2bc Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 07:47:04 -0400 Subject: [PATCH 18/19] Fixed bug where a mix location without a prepended '/' would not work. --- readme.md | 2 +- src/Publiux/laravelcdn/CdnFacade.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 7a9aca6..11fe493 100644 --- a/readme.md +++ b/readme.md @@ -39,7 +39,7 @@ This project has been forked from https://github.com/Vinelab/cdn. All credit for Require `publiux/laravelcdn` in your project: ```bash -composer require publiux/laravelcdn +composer require "publiux/laravelcdn:~2.0" ``` *If you are using Laravel 5.4 or below, you need to register the service provider:* diff --git a/src/Publiux/laravelcdn/CdnFacade.php b/src/Publiux/laravelcdn/CdnFacade.php index 8154236..1a38335 100644 --- a/src/Publiux/laravelcdn/CdnFacade.php +++ b/src/Publiux/laravelcdn/CdnFacade.php @@ -151,6 +151,9 @@ public function mix($path) if (isset($manifest[$path])) { return $this->generateUrl($manifest[$path], 'public/'); } + if (isset($manifest['/' . $path])) { + return $this->generateUrl($manifest['/' . $path], 'public/'); + } throw new \InvalidArgumentException("File {$path} not defined in asset manifest."); } From 1a542fe8fc4bcd1eca84f9943d4b456325eb869a Mon Sep 17 00:00:00 2001 From: Raul Ruiz Date: Tue, 5 Sep 2017 07:50:07 -0400 Subject: [PATCH 19/19] Fixed bug where a mix location without a prepended '/' would not work. --- src/Publiux/laravelcdn/CdnFacade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Publiux/laravelcdn/CdnFacade.php b/src/Publiux/laravelcdn/CdnFacade.php index 1a38335..3afc308 100644 --- a/src/Publiux/laravelcdn/CdnFacade.php +++ b/src/Publiux/laravelcdn/CdnFacade.php @@ -148,12 +148,12 @@ public function mix($path) if (is_null($manifest)) { $manifest = json_decode(file_get_contents(public_path('mix-manifest.json')), true); } - if (isset($manifest[$path])) { - return $this->generateUrl($manifest[$path], 'public/'); - } if (isset($manifest['/' . $path])) { return $this->generateUrl($manifest['/' . $path], 'public/'); } + if (isset($manifest[$path])) { + return $this->generateUrl($manifest[$path], 'public/'); + } throw new \InvalidArgumentException("File {$path} not defined in asset manifest."); }