diff --git a/Command/ResolveCacheCommand.php b/Command/ResolveCacheCommand.php index 3fa041e5f..7e32a6bb7 100644 --- a/Command/ResolveCacheCommand.php +++ b/Command/ResolveCacheCommand.php @@ -5,6 +5,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class ResolveCacheCommand extends ContainerAwareCommand @@ -14,14 +15,19 @@ protected function configure() $this ->setName('liip:imagine:cache:resolve') ->setDescription('Resolve cache for given path and set of filters.') - ->addArgument('path', InputArgument::REQUIRED, 'Image path') - ->addArgument('filters', InputArgument::OPTIONAL|InputArgument::IS_ARRAY, 'Filters list'); + ->addArgument('paths', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'Image paths') + ->addOption( + 'filters', + 'f', + InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, + 'Filters list' + ); } protected function execute(InputInterface $input, OutputInterface $output) { - $path = $input->getArgument('path'); - $filters = $input->getArgument('filters'); + $paths = $input->getArgument('paths'); + $filters = $input->getOption('filters'); /** @var FilterManager filterManager */ $filterManager = $this->getContainer()->get('liip_imagine.filter.manager'); @@ -34,18 +40,20 @@ protected function execute(InputInterface $input, OutputInterface $output) $filters = array_keys($filterManager->getFilterConfiguration()->all()); } - foreach ($filters as $filter) { - if (!$cacheManager->isStored($path, $filter)) { - $binary = $dataManager->find($filter, $path); + foreach ($paths as $path) { + foreach ($filters as $filter) { + if (!$cacheManager->isStored($path, $filter)) { + $binary = $dataManager->find($filter, $path); - $cacheManager->store( - $filterManager->applyFilter($binary, $filter), - $path, - $filter - ); - } + $cacheManager->store( + $filterManager->applyFilter($binary, $filter), + $path, + $filter + ); + } - $output->writeln($cacheManager->resolve($path, $filter)); + $output->writeln($cacheManager->resolve($path, $filter)); + } } } } diff --git a/README.md b/README.md index 38334fd70..b8c5a4d2d 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,6 @@ This will perform the transformation called `thumbnail`, which you can define to do a number of different things, such as resizing, cropping, drawing, masking, etc. -Same result you can get from cli command: - -``` jinja -app/console liip:imagine:cache:resolve relative/path/to/image.jpg thumbnail -```` - This bundle integrates the standalone PHP "[Imagine library](https://github.com/avalanche123/Imagine)". [![Build Status](https://secure.travis-ci.org/liip/LiipImagineBundle.png)](http://travis-ci.org/liip/LiipImagineBundle) @@ -114,6 +108,12 @@ $runtimeConfig = array( ``` +Also you can resolve image url from console: +```jinja +app/console liip:imagine:cache:resolve images/dream.jpg images/dream2.jpg --filters=thumbnail_web_path --filters=thumbnail_default +``` +Where only paths required parameter. They are separated by space. If you omit filters option will be applied all available filters. + If you need to access filtered image URL in your controller: ``` php diff --git a/Tests/Functional/Command/ResolveCacheTest.php b/Tests/Functional/Command/ResolveCacheTest.php index 4a3bd8433..a6d254ad5 100644 --- a/Tests/Functional/Command/ResolveCacheTest.php +++ b/Tests/Functional/Command/ResolveCacheTest.php @@ -38,10 +38,11 @@ public function testShouldResolveWithEmptyCache() { $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); - $output = $this->executeConsole(new ResolveCacheCommand(), array( - 'path' => 'images/cats.jpeg', - 'filters' => array('thumbnail_web_path') - )); + $output = $this->executeConsole( + new ResolveCacheCommand(), + array('paths' => array('images/cats.jpeg')), + array('filters' => array('thumbnail_web_path')) + ); $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); @@ -54,33 +55,71 @@ public function testShouldResolveWithCacheExists() 'anImageContent' ); - $output = $this->executeConsole(new ResolveCacheCommand(), array( - 'path' => 'images/cats.jpeg', - 'filters' => array('thumbnail_web_path') - )); + $output = $this->executeConsole( + new ResolveCacheCommand(), + array('paths' => array('images/cats.jpeg')), + array('filters' => array('thumbnail_web_path')) + ); + + $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + } + + public function testShouldResolveWithFewPaths() + { + $output = $this->executeConsole( + new ResolveCacheCommand(), + array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')), + array('filters' => array('thumbnail_web_path')) + ); + + $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); + } + + public function testShouldResolveWithFewPathsAndPartiallyFullCache() + { + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent' + ); + + $output = $this->executeConsole( + new ResolveCacheCommand(), + array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')), + array('filters' => array('thumbnail_web_path')) + ); $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); } - public function testShouldResolveWithFewFiltersInParams() + public function testShouldResolveWithFewPathsAndFewFilters() { - $output = $this->executeConsole(new ResolveCacheCommand(), array( - 'path' => 'images/cats.jpeg', - 'filters' => array('thumbnail_web_path', 'thumbnail_default') - )); + $output = $this->executeConsole( + new ResolveCacheCommand(), + array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')), + array('filters' => array('thumbnail_web_path', 'thumbnail_default')) + ); $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output); + $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats2.jpeg', $output); } - public function testShouldResolveWithoutFiltersInParams() + public function testShouldResolveWithFewPathsAndWithoutFilters() { - $output = $this->executeConsole(new ResolveCacheCommand(), array( - 'path' => 'images/cats.jpeg', - )); + $output = $this->executeConsole( + new ResolveCacheCommand(), + array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')) + ); $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output); + $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats2.jpeg', $output); } /** diff --git a/Tests/Functional/app/web/images/cats2.jpeg b/Tests/Functional/app/web/images/cats2.jpeg new file mode 100644 index 000000000..685a844b4 Binary files /dev/null and b/Tests/Functional/app/web/images/cats2.jpeg differ