Skip to content
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

[1.0][resolver] Decouple WebPathResolver from http request. Simplify its logic. #320

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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

This file was deleted.

1 change: 0 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function getConfigTreeBuilder()
->end()
->scalarNode('web_root')->defaultValue('%kernel.root_dir%/../web')->end()
->scalarNode('data_root')->defaultValue('%liip_imagine.web_root%')->end()
->scalarNode('cache_mkdir_mode')->defaultValue(0777)->end()
->scalarNode('cache_prefix')->defaultValue('/media/cache')->end()
->scalarNode('cache')->defaultValue('web_path')->end()
->scalarNode('cache_base_path')->defaultValue('')->end()
Expand Down
1 change: 0 additions & 1 deletion DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('liip_imagine.cache_prefix', $cachePrefix);
$container->setParameter('liip_imagine.web_root', $config['web_root']);
$container->setParameter('liip_imagine.data_root', $config['data_root']);
$container->setParameter('liip_imagine.cache_mkdir_mode', $config['cache_mkdir_mode']);
$container->setParameter('liip_imagine.formats', $config['formats']);
$container->setParameter('liip_imagine.cache.resolver.default', $config['cache']);

Expand Down
19 changes: 1 addition & 18 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class CacheManager
*/
protected $router;

/**
* @var string
*/
protected $webRoot;

/**
* @var string
*/
Expand All @@ -40,14 +35,12 @@ class CacheManager
*
* @param FilterConfiguration $filterConfig
* @param RouterInterface $router
* @param string $webRoot
* @param string $defaultResolver
*/
public function __construct(FilterConfiguration $filterConfig, RouterInterface $router, $webRoot, $defaultResolver = null)
public function __construct(FilterConfiguration $filterConfig, RouterInterface $router, $defaultResolver = null)
{
$this->filterConfig = $filterConfig;
$this->router = $router;
$this->webRoot = realpath($webRoot);
$this->defaultResolver = $defaultResolver;
}

Expand All @@ -68,16 +61,6 @@ public function addResolver($filter, ResolverInterface $resolver)
}
}

/**
* Returns the configured web root path.
*
* @return string
*/
public function getWebRoot()
{
return $this->webRoot;
}

/**
* Gets a resolver for the given filter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Symfony\Component\Routing\RequestContext;

/**
* NoCacheResolver.
*/
class NoCacheResolver extends WebPathResolver
class NoCacheWebPathResolver implements ResolverInterface
{
/**
* @param RequestContext $requestContext
*/
public function __construct(RequestContext $requestContext)
{
$this->requestContext = $requestContext;
}

/**
* {@inheritDoc}
*/
Expand All @@ -22,7 +28,11 @@ public function isStored($path, $filter)
*/
public function resolve($path, $filter)
{
return $this->getRequest()->getSchemeAndHttpHost().'/'.$path;
return sprintf('%s://%s/%s',
$this->requestContext->getScheme(),
$this->requestContext->getHost(),
ltrim($path, '/')
);
}

/**
Expand All @@ -39,3 +49,4 @@ public function remove(array $paths, array $filters)
{
}
}

2 changes: 1 addition & 1 deletion Imagine/Cache/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isStored($path, $filter);
* @param string $path The path where the original file is expected to be.
* @param string $filter The name of the imagine filter in effect.
*
* @return string The URL of the cached image.
* @return string The ABSOLUTE URL of the cached image.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't yell :-)

*
* @throws NotResolvableException
*/
Expand Down
117 changes: 93 additions & 24 deletions Imagine/Cache/Resolver/WebPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,123 @@

namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

class WebPathResolver extends AbstractFilesystemResolver
use Liip\ImagineBundle\Binary\BinaryInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RequestContext;

class WebPathResolver implements ResolverInterface
{
/**
* If the file has already been cached, we're probably not rewriting
* correctly, hence make a 301 to proper location, so browser remembers.
*
* Strip the base URL of this request from the browserpath to not interfere with the base path.
*
* @var Filesystem
*/
protected $filesystem;

/**
* @var RequestContext
*/
protected $requestContext;

/**
* @var string
*/
protected $webRoot;
/**
* @var string
*/
protected $cachePrefix;

/**
* @param Filesystem $filesystem
* @param RequestContext $requestContext
* @param string $webRootDir
* @param string $cachePrefix
*/
public function __construct(
Filesystem $filesystem,
RequestContext $requestContext,
$webRootDir,
$cachePrefix = 'media/cache'
) {
$this->filesystem = $filesystem;
$this->requestContext = $requestContext;

$this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/');
$this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
$this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix;
}

/**
* {@inheritDoc}
*/
public function resolve($path, $filter)
{
$browserPath = $this->decodeBrowserPath($this->getBrowserPath($path, $filter));
$this->basePath = $this->getRequest()->getBaseUrl();

if ($this->basePath && 0 === strpos($browserPath, $this->basePath)) {
$browserPath = substr($browserPath, strlen($this->basePath));
}
return sprintf('%s://%s/%s',
$this->requestContext->getScheme(),
$this->requestContext->getHost(),
$this->getFileUrl($path, $filter)
);
}

return $this->getRequest()->getBasePath().$browserPath;
/**
* {@inheritDoc}
*/
public function isStored($path, $filter)
{
return $this->filesystem->exists($this->getFilePath($path, $filter));
}

/**
* {@inheritDoc}
*/
public function getBrowserPath($path, $filter, $absolute = false)
public function store(BinaryInterface $binary, $path, $filter)
{
return $this->cacheManager->generateUrl($path, $filter, $absolute);
$this->filesystem->dumpFile(
$this->getFilePath($path, $filter),
$binary->getContent()
);
}

/**
* Decodes the URL encoded browser path.
*
* @param string $browserPath
*
* @return string
* {@inheritDoc}
*/
protected function decodeBrowserPath($browserPath)
public function remove(array $paths, array $filters)
{
//TODO: find out why I need double urldecode to get a valid path
return urldecode(urldecode($browserPath));
if (empty($paths) && empty($filters)) {
return;
}

if (empty($paths)) {
$filtersCacheDir = array();
foreach ($filters as $filter) {
$filtersCacheDir[] = $this->cacheRoot.'/'.$filter;
}

$this->filesystem->remove($filtersCacheDir);

return;
}

foreach ($paths as $path) {
foreach ($filters as $filter) {
$this->filesystem->remove($this->getFilePath($path, $filter));
}
}
}

/**
* {@inheritDoc}
*/
protected function getFilePath($path, $filter)
{
return $this->cacheManager->getWebRoot().$this->resolve($path, $filter);
return $this->webRoot.'/'.$this->getFileUrl($path, $filter);
}

/**
* {@inheritDoc}
*/
protected function getFileUrl($path, $filter)
{
return $this->cachePrefix.'/'.$filter.'/'.$path;
}
}

24 changes: 8 additions & 16 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<!-- Cache resolvers' classes -->

<parameter key="liip_imagine.cache.resolver.web_path.class">Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver</parameter>
<parameter key="liip_imagine.cache.resolver.no_cache.class">Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheResolver</parameter>
<parameter key="liip_imagine.cache.resolver.no_cache_web_path.class">Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheWebPathResolver</parameter>

<!-- Form types -->

Expand All @@ -80,7 +80,6 @@
<service id="liip_imagine.cache.manager" class="%liip_imagine.cache.manager.class%">
<argument type="service" id="liip_imagine.filter.configuration" />
<argument type="service" id="router" />
<argument>%liip_imagine.web_root%</argument>
<argument>%liip_imagine.cache.resolver.default%</argument>
</service>

Expand Down Expand Up @@ -186,24 +185,17 @@

<!-- Cache resolver -->

<service id="liip_imagine.cache.resolver.abstract_filesystem" abstract="true" public="false">
<service id="liip_imagine.cache.resolver.web_path" class="%liip_imagine.cache.resolver.web_path.class%" public="true">
<argument type="service" id="filesystem" />
<call method="setBasePath">
<argument type="string">%liip_imagine.cache.resolver.base_path%</argument>
</call>
<call method="setFolderPermissions">
<argument type="string">%liip_imagine.cache_mkdir_mode%</argument>
</call>
<call method="setRequest">
<argument type="service" id="request" on-invalid="null" strict="false" />
</call>
</service>

<service id="liip_imagine.cache.resolver.web_path" class="%liip_imagine.cache.resolver.web_path.class%" parent="liip_imagine.cache.resolver.abstract_filesystem" public="true">
<argument type="service" id="router.request_context" />
<argument>%liip_imagine.web_root%</argument>
<argument>%liip_imagine.cache_prefix%</argument>

<tag name="liip_imagine.cache.resolver" resolver="web_path" />
</service>

<service id="liip_imagine.cache.resolver.no_cache" class="%liip_imagine.cache.resolver.no_cache.class%" parent="liip_imagine.cache.resolver.abstract_filesystem" public="true">
<service id="liip_imagine.cache.resolver.no_cache_web_path" class="%liip_imagine.cache.resolver.no_cache_web_path.class%" public="true">
<argument type="service" id="router.request_context" />
<tag name="liip_imagine.cache.resolver" resolver="no_cache" />
</service>

Expand Down
7 changes: 0 additions & 7 deletions Resources/doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ liip_imagine:
driver: gd
web_root: %kernel.root_dir%/../web
data_root: %liip_imagine.web_root%
cache_mkdir_mode: 0777
cache_prefix: /media/cache
cache: web_path
cache_clearer: true
Expand Down Expand Up @@ -45,12 +44,6 @@ There are several configuration options available:

default: `%kernel.root_dir%/../web`

- `cache_mkdir_mode` - permissions to set on generated cache directories.
Must be specified as an octal number, which means it should begin with a
leading zero. mode is ignored on Windows.

default: `0777`

- `cache_prefix` - this is also used in the path for image generation, so
as to not clutter your web root with cached images. For example by default,
the images would be written to the `web/media/cache/` directory.
Expand Down
Loading