diff --git a/readme.md b/readme.md index 90dff41..f05a67a 100644 --- a/readme.md +++ b/readme.md @@ -270,6 +270,9 @@ The MIT License (MIT). Please see [License File](https://github.com/publiux/lara ## Changelog +#### v1.0.3 +- Fixed bug where schemeless Urls could not be used for CloudFront. Valid urls now begin with http, https, or simply '//' + #### v1.0.2 - Fixed bug where the elixir function was inadvertently omitted from the release. diff --git a/src/Publiux/laravelcdn/Commands/EmptyCommand.php b/src/Publiux/laravelcdn/Commands/EmptyCommand.php index 3c6a5bf..938dfe4 100644 --- a/src/Publiux/laravelcdn/Commands/EmptyCommand.php +++ b/src/Publiux/laravelcdn/Commands/EmptyCommand.php @@ -11,6 +11,7 @@ * @category Command * * @author Mahmoud Zalt + * @author Raul Ruiz */ class EmptyCommand extends Command { @@ -19,7 +20,7 @@ class EmptyCommand extends Command * * @var string */ - protected $name = 'cdn:empty'; + protected $signature = 'cdn:empty'; /** * The console command description. @@ -50,32 +51,8 @@ public function __construct(CdnInterface $cdn) * * @return mixed */ - public function fire() + public function handle() { $this->cdn->emptyBucket(); } - - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() - { - return [ -// array('cdn', InputArgument::OPTIONAL, 'cdn option.'), - ]; - } - - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() - { - return [ -// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null), - ]; - } } diff --git a/src/Publiux/laravelcdn/Commands/PushCommand.php b/src/Publiux/laravelcdn/Commands/PushCommand.php index 064f0c0..5b0d6c5 100644 --- a/src/Publiux/laravelcdn/Commands/PushCommand.php +++ b/src/Publiux/laravelcdn/Commands/PushCommand.php @@ -11,6 +11,7 @@ * @category Command * * @author Mahmoud Zalt + * @author Raul Ruiz */ class PushCommand extends Command { @@ -19,7 +20,7 @@ class PushCommand extends Command * * @var string */ - protected $name = 'cdn:push'; + protected $signature = 'cdn:push'; /** * The console command description. @@ -50,32 +51,8 @@ public function __construct(CdnInterface $cdn) * * @return mixed */ - public function fire() + public function handle() { $this->cdn->push(); } - - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() - { - return [ -// array('cdn', InputArgument::OPTIONAL, 'cdn option.'), - ]; - } - - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() - { - return [ -// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null), - ]; - } } diff --git a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php index f76d547..307a97d 100644 --- a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php +++ b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php @@ -30,6 +30,7 @@ * @property string $cloudfront_url * * @author Mahmoud Zalt + * @author Raul Ruiz */ class AwsS3Provider extends Provider implements ProviderInterface { @@ -191,6 +192,22 @@ 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 { @@ -294,7 +311,11 @@ public function urlGenerator($path) if ($this->getCloudFront() === true) { $url = $this->cdn_helper->parseUrl($this->getCloudFrontUrl()); - return $url['scheme'].'://'.$url['host'].'/'.$path; + if (array_key_exists('scheme', $url)) { + return $url['scheme'].'://'.$url['host'].'/'.$path; + } else { + return '//'.$url['host'].'/'.$path; + } } $url = $this->cdn_helper->parseUrl($this->getUrl()); @@ -302,7 +323,11 @@ public function urlGenerator($path) $bucket = $this->getBucket(); $bucket = (!empty($bucket)) ? $bucket.'.' : ''; - return $url['scheme'].'://'.$bucket.$url['host'].'/'.$path; + if (array_key_exists('scheme', $url)) { + return $url['scheme'].'://'.$bucket.$url['host'].'/'.$path; + } else { + return '//'.$bucket.$url['host'].'/'.$path; + } } /** diff --git a/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php b/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php index e16d109..3193151 100644 --- a/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php +++ b/tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php @@ -11,6 +11,7 @@ * @category Test * * @author Mahmoud Zalt + * @author Raul Ruiz */ class AwsS3ProviderTest extends TestCase { @@ -20,9 +21,10 @@ 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); @@ -30,21 +32,10 @@ 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('vinelab/cdn/tests/Vinelab/Cdn/AwsS3ProviderTest.php'); + $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]', - [ - $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'); @@ -56,6 +47,45 @@ public function setUp() ->andReturn($m_command1); $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); @@ -95,6 +125,8 @@ public function testInitializingObject() ], ]; + $this->setupNonCloudfrontTest(); + $awsS3Provider_obj = $this->p_awsS3Provider->init($configurations); assertInstanceOf('Publiux\laravelcdn\Providers\AwsS3Provider', $awsS3Provider_obj); @@ -128,6 +160,8 @@ public function testUploadingAssets() ], ]; + $this->setupNonCloudfrontTest(); + $this->p_awsS3Provider->init($configurations); $result = $this->p_awsS3Provider->upload(new Collection([$this->m_spl_file])); @@ -135,7 +169,7 @@ public function testUploadingAssets() assertEquals(true, $result); } - public function testUrlGenerator() + public function testUrlGeneratorS3() { $configurations = [ 'default' => 'AwsS3', @@ -163,6 +197,8 @@ public function testUrlGenerator() ], ]; + $this->setupNonCloudfrontTest(); + $this->p_awsS3Provider->init($configurations); $result = $this->p_awsS3Provider->urlGenerator($this->path); @@ -170,6 +206,80 @@ public function testUrlGenerator() 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 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' => '//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 = [ @@ -198,6 +308,8 @@ public function testEmptyUrlGenerator() ], ]; + $this->setupNonCloudfrontTest(); + $this->p_awsS3Provider->init($configurations); $result = $this->p_awsS3Provider->urlGenerator($this->path);