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

[WIP][Cache Resolver] Implement relative web path resolver #1233

Open
wants to merge 17 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,67 @@
<?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\DependencyInjection\Factory\Resolver;

use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

abstract class AbstractWebPathResolverFactory extends AbstractResolverFactory
{
/**
* {@inheritdoc}
*/
public function create(ContainerBuilder $container, $resolverName, array $config)
{
$resolverDefinition = $this->getChildResolverDefinition();
$pathResolverDefinition = new ChildDefinition('liip_imagine.util.resolver.prototype.path');
$pathResolverDefinition->replaceArgument(0, $config['web_root']);
$pathResolverDefinition->replaceArgument(1, $config['cache_prefix']);

$pathResolverServiceId = 'liip_imagine.util.resolver.path';
$container->setDefinition($pathResolverServiceId, $pathResolverDefinition);

$resolverDefinition->replaceArgument(1, new Reference($pathResolverServiceId));

$resolverDefinition->addTag(
'liip_imagine.cache.resolver',
[
'resolver' => $resolverName,
]
);

$resolverId = 'liip_imagine.cache.resolver.';
$container->setDefinition($resolverId.$resolverName, $resolverDefinition);

return $resolverId;
}

/**
* {@inheritdoc}
*/
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('web_root')
->defaultValue(SymfonyFramework::getContainerResolvableRootWebPath())
->cannotBeEmpty()
->end()
->scalarNode('cache_prefix')
->defaultValue('media/cache')
->cannotBeEmpty()
->end()
->end();
}
}
@@ -0,0 +1,23 @@
<?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\DependencyInjection\Factory\Resolver;

class RelativeWebPathResolverFactory extends AbstractWebPathResolverFactory
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'relative_web_path';
}
}
42 changes: 1 addition & 41 deletions DependencyInjection/Factory/Resolver/WebPathResolverFactory.php
Expand Up @@ -11,53 +11,13 @@

namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver;

use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class WebPathResolverFactory extends AbstractResolverFactory
class WebPathResolverFactory extends AbstractWebPathResolverFactory
{
/**
* {@inheritdoc}
*/
public function create(ContainerBuilder $container, $resolverName, array $config)
{
$resolverDefinition = $this->getChildResolverDefinition();
$resolverDefinition->replaceArgument(2, $config['web_root']);
$resolverDefinition->replaceArgument(3, $config['cache_prefix']);
$resolverDefinition->addTag('liip_imagine.cache.resolver', [
'resolver' => $resolverName,
]);

$resolverId = 'liip_imagine.cache.resolver.';
$container->setDefinition($resolverId.$resolverName, $resolverDefinition);

return $resolverId;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'web_path';
}

/**
* {@inheritdoc}
*/
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('web_root')
->defaultValue(SymfonyFramework::getContainerResolvableRootWebPath())
->cannotBeEmpty()
->end()
->scalarNode('cache_prefix')
->defaultValue('media/cache')
->cannotBeEmpty()
->end()
->end();
}
}
100 changes: 100 additions & 0 deletions Imagine/Cache/Resolver/AbstractWebPathResolver.php
@@ -0,0 +1,100 @@
<?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\Resolver;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Utility\Path\PathResolverInterface;
use Symfony\Component\Filesystem\Filesystem;

abstract class AbstractWebPathResolver implements ResolverInterface
{
/**
* @var Filesystem
*/
protected $filesystem;

/**
* @var PathResolverInterface
*/
protected $pathResolver;

/**
* @param Filesystem $filesystem
* @param PathResolverInterface $pathResolver
*/
public function __construct(
Filesystem $filesystem,
PathResolverInterface $pathResolver
) {
$this->filesystem = $filesystem;
$this->pathResolver = $pathResolver;
}

/**
* Checks whether the given path is stored within this Resolver.
*
* @param string $path
* @param string $filter
*
* @return bool
*/
public function isStored($path, $filter)
{
return is_file($this->pathResolver->getFilePath($path, $filter));
}

/**
* {@inheritdoc}
*/
public function store(BinaryInterface $binary, $path, $filter)
{
$this->filesystem->dumpFile(
$this->pathResolver->getFilePath($path, $filter),
$binary->getContent()
);
}

/**
* {@inheritdoc}
*/
public function remove(array $paths, array $filters)
{
if (empty($paths) && empty($filters)) {
return;
}

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

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

return;
}

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

/**
* @return PathResolverInterface
*/
protected function getPathResolver()
{
return $this->pathResolver;
}
}
23 changes: 23 additions & 0 deletions Imagine/Cache/Resolver/RelativeWebPathResolver.php
@@ -0,0 +1,23 @@
<?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\Resolver;

class RelativeWebPathResolver extends AbstractWebPathResolver
{
/**
* {@inheritdoc}
*/
public function resolve($path, $filter)
{
return sprintf('/%s', $this->getPathResolver()->getFileUrl($path, $filter));
}
}