Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading