Skip to content

Commit

Permalink
create imagine_filter_cache Twig filter
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jun 15, 2021
1 parent 7a1779b commit 0e71655
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Resources/doc/basic-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ when the first page request is served. The transformed image is then cached
for subsequent requests. The final cached image path would be similar to
``/media/cache/my_thumb/relative/path/to/image.jpg``.

.. tip::

You can :doc:`prepare the cache in advance <commands>` and use the ``imagine_filter_cache`` filter to always
return a link to the final cached image.

.. note::

Using the ``dev`` environment you might find that images are not properly
Expand Down
1 change: 1 addition & 0 deletions Templating/FilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getFilters()
{
return [
new TwigFilter('imagine_filter', [$this, 'filter']),
new TwigFilter('imagine_filter_cache', [$this, 'filterCache']),
];
}
}
18 changes: 18 additions & 0 deletions Templating/FilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ public function filter($path, $filter, array $config = [], $resolver = null, $re
return $this->cache->getBrowserPath(parse_url($path, PHP_URL_PATH), $filter, $config, $resolver, $referenceType);
}

/**
* Gets the cache path for the image and filter to apply.
*/
public function filterCache(
string $path,
string $filter,
array $config = [],
?string $resolver = null
): string {
$path = parse_url($path, PHP_URL_PATH);

if (!empty($config)) {
$path = $this->cache->getRuntimePath($path, $config);
}

return $this->cache->resolve($path, $filter, $resolver);
}

/**
* {@inheritdoc}
*/
Expand Down
51 changes: 50 additions & 1 deletion Tests/Templating/AbstractFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,56 @@ public function testInvokeFilterMethod(): void
->with($expectedInputPath, $expectedFilter)
->willReturn($expectedCachePath);

$this->assertSame($expectedCachePath, $this->createTemplatingMock($manager)->filter($expectedInputPath, $expectedFilter));
$actualPath = $this->createTemplatingMock($manager)->filter($expectedInputPath, $expectedFilter);

$this->assertSame($expectedCachePath, $actualPath);
}

public function testInvokeFilterCacheMethod(): void
{
$expectedFilter = 'thumbnail';
$expectedInputPath = 'thePathToTheImage';
$expectedCachePath = 'thePathToTheCachedImage';

$manager = $this->createCacheManagerMock();
$manager
->expects($this->once())
->method('resolve')
->with($expectedInputPath, $expectedFilter)
->willReturn($expectedCachePath);

$actualPath = $this->createTemplatingMock($manager)->filterCache($expectedInputPath, $expectedFilter);

$this->assertSame($expectedCachePath, $actualPath);
}

public function testInvokeFilterCacheMethodWithRuntimeConfig(): void
{
$expectedFilter = 'thumbnail';
$expectedInputPath = 'thePathToTheImage';
$expectedCachePath = 'thePathToTheCachedImage';
$expectedRuntimeConfig = [
'thumbnail' => [
'size' => [100, 100],
],
];
$expectedRuntimeConfigPath = 'thePathToTheImageWithRuntimeConfig';

$manager = $this->createCacheManagerMock();
$manager
->expects($this->once())
->method('getRuntimePath')
->with($expectedInputPath, $expectedRuntimeConfig)
->willReturn($expectedRuntimeConfigPath);
$manager
->expects($this->once())
->method('resolve')
->with($expectedRuntimeConfigPath, $expectedFilter)
->willReturn($expectedCachePath);

$actualPath = $this->createTemplatingMock($manager)->filterCache($expectedInputPath, $expectedFilter, $expectedRuntimeConfig);

$this->assertSame($expectedCachePath, $actualPath);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Tests/Templating/FilterExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FilterExtensionTest extends AbstractFilterTest
{
public function testAddsFilterMethodToFiltersList(): void
{
$this->assertCount(1, $this->createTemplatingMock()->getFilters());
$this->assertCount(2, $this->createTemplatingMock()->getFilters());
}

public function testInstanceOfTwigFilter(): void
Expand Down

0 comments on commit 0e71655

Please sign in to comment.