Skip to content

Commit

Permalink
Merge pull request #383 from formapro-forks/resolve-command-few-paths
Browse files Browse the repository at this point in the history
Resolve command few paths
  • Loading branch information
makasim committed Apr 4, 2014
2 parents bab39ad + 94c9854 commit 2f9025e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 37 deletions.
36 changes: 22 additions & 14 deletions Command/ResolveCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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));
}
}
}
}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -114,6 +108,12 @@ $runtimeConfig = array(
<img src="<?php $this['imagine']->filter('/relative/path/to/image.jpg', 'my_thumb', $runtimeConfig) ?>" />
```
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
Expand Down
73 changes: 56 additions & 17 deletions Tests/Functional/Command/ResolveCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
Binary file added Tests/Functional/app/web/images/cats2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2f9025e

Please sign in to comment.