Skip to content

add CacheManagerInterface.php that will allow CacheManager.php to be … #1351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Cache\CacheManagerInterface;
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -98,6 +99,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setAlias('liip_imagine', new Alias('liip_imagine.'.$config['driver']));
$container->setAlias(CacheManager::class, new Alias('liip_imagine.cache.manager', false));
$container->setAlias(CacheManagerInterface::class, new Alias('liip_imagine.cache.manager', false));
$container->setAlias(DataManager::class, new Alias('liip_imagine.data.manager', false));
$container->setAlias(FilterManager::class, new Alias('liip_imagine.filter.manager', false));

Expand Down
53 changes: 35 additions & 18 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

class CacheManager
class CacheManager implements CacheManagerInterface
{
/**
* @var FilterConfiguration
Expand Down Expand Up @@ -63,7 +63,7 @@ class CacheManager
* Constructs the cache manager to handle Resolvers based on the provided FilterConfiguration.
*
* @param string $defaultResolver
* @param bool $webpGenerate
* @param bool $webpGenerate
*/
public function __construct(
FilterConfiguration $filterConfig,
Expand Down Expand Up @@ -99,15 +99,20 @@ public function addResolver($filter, ResolverInterface $resolver)
* Gets filtered path for rendering in the browser.
* It could be the cached one or an url of filter action.
*
* @param string $path The path where the resolved file is expected
* @param string $path The path where the resolved file is expected
* @param string $filter
* @param string $resolver
* @param int $referenceType
* @param int $referenceType
*
* @return string
*/
public function getBrowserPath($path, $filter, array $runtimeConfig = [], $resolver = null, $referenceType = UrlGeneratorInterface::ABSOLUTE_URL)
{
public function getBrowserPath(
$path,
$filter,
array $runtimeConfig = [],
$resolver = null,
$referenceType = UrlGeneratorInterface::ABSOLUTE_URL
) {
if (!empty($runtimeConfig)) {
$rcPath = $this->getRuntimePath($path, $runtimeConfig);

Expand Down Expand Up @@ -136,15 +141,20 @@ public function getRuntimePath($path, array $runtimeConfig)
/**
* Returns a web accessible URL.
*
* @param string $path The path where the resolved file is expected
* @param string $filter The name of the imagine filter in effect
* @param string $path The path where the resolved file is expected
* @param string $filter The name of the imagine filter in effect
* @param string $resolver
* @param int $referenceType The type of reference to be generated (one of the UrlGenerator constants)
* @param int $referenceType The type of reference to be generated (one of the UrlGenerator constants)
*
* @return string
*/
public function generateUrl($path, $filter, array $runtimeConfig = [], $resolver = null, $referenceType = UrlGeneratorInterface::ABSOLUTE_URL)
{
public function generateUrl(
$path,
$filter,
array $runtimeConfig = [],
$resolver = null,
$referenceType = UrlGeneratorInterface::ABSOLUTE_URL
) {
$params = [
'path' => ltrim($path, '/'),
'filter' => $filter,
Expand Down Expand Up @@ -187,20 +197,25 @@ public function isStored($path, $filter, $resolver = null)
* @param string $filter
* @param string $resolver
*
* @return string The url of resolved image
* @throws NotFoundHttpException if the path can not be resolved
*
* @return string The url of resolved image
*/
public function resolve($path, $filter, $resolver = null)
{
if (false !== mb_strpos($path, '/../') || 0 === mb_strpos($path, '../')) {
throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path));
throw new NotFoundHttpException(
sprintf("Source image was searched with '%s' outside of the defined root path", $path)
);
}

$preEvent = new CacheResolveEvent($path, $filter);
$this->dispatchWithBC($preEvent, ImagineEvents::PRE_RESOLVE);

$url = $this->getResolver($preEvent->getFilter(), $resolver)->resolve($preEvent->getPath(), $preEvent->getFilter());
$url = $this->getResolver($preEvent->getFilter(), $resolver)->resolve(
$preEvent->getPath(),
$preEvent->getFilter()
);

$postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url);
$this->dispatchWithBC($postEvent, ImagineEvents::POST_RESOLVE);
Expand All @@ -209,11 +224,11 @@ public function resolve($path, $filter, $resolver = null)
}

/**
* @see ResolverInterface::store
*
* @param string $path
* @param string $filter
* @param string $resolver
* @see ResolverInterface::store
*
*/
public function store(BinaryInterface $binary, $path, $filter, $resolver = null)
{
Expand Down Expand Up @@ -262,9 +277,9 @@ public function remove($paths = null, $filters = null)
* @param string $filter
* @param string $resolver
*
* @return ResolverInterface
* @throws \OutOfBoundsException If neither a specific nor a default resolver is available
*
* @return ResolverInterface
*/
protected function getResolver($filter, $resolver)
{
Expand All @@ -278,7 +293,9 @@ protected function getResolver($filter, $resolver)
}

if (!isset($this->resolvers[$resolverName])) {
throw new \OutOfBoundsException(sprintf('Could not find resolver "%s" for "%s" filter type', $resolverName, $filter));
throw new \OutOfBoundsException(
sprintf('Could not find resolver "%s" for "%s" filter type', $resolverName, $filter)
);
}

return $this->resolvers[$resolverName];
Expand Down
50 changes: 50 additions & 0 deletions Imagine/Cache/CacheManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Imagine\Cache;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

interface CacheManagerInterface
{
public function addResolver($filter, ResolverInterface $resolver);

public function getBrowserPath(
$path,
$filter,
array $runtimeConfig = [],
$resolver = null,
$referenceType = UrlGeneratorInterface::ABSOLUTE_URL
);

public function getRuntimePath($path, array $runtimeConfig);

public function generateUrl(
$path,
$filter,
array $runtimeConfig = [],
$resolver = null,
$referenceType = UrlGeneratorInterface::ABSOLUTE_URL
);

public function isStored($path, $filter, $resolver = null);

public function resolve($path, $filter, $resolver = null);

public function store(BinaryInterface $binary, $path, $filter, $resolver = null);

public function remove($paths = null, $filters = null);
}