Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
LouTerrailloune committed Oct 14, 2013
2 parents a57cc81 + e298ebb commit c117b88
Show file tree
Hide file tree
Showing 94 changed files with 5,896 additions and 597 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/phpunit.xml
composer.lock
composer.phar
vendor/
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
language: php

php:
- 5.3
- 5.4
- 5.5

env:
- SYMFONY_VERSION=2.0.*
- SYMFONY_VERSION=2.1.*
- SYMFONY_VERSION=2.2.*
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=dev-master

before_script:
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --prefer-source
- composer install --dev --prefer-source

script: phpunit --coverage-text

notifications:
email:
- [email protected]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Changelog
=========

* **2013-09-17**: [PHPCR loader] DoctrinePHPCRLoader is now deprecated, as the
CmfMediaBundle provides a more reliable loader that is already provided as a
service. See http://symfony.com/doc/master/cmf/bundles/media.html#liipimagine
50 changes: 23 additions & 27 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,65 @@

namespace Liip\ImagineBundle\Controller;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Liip\ImagineBundle\Imagine\CachePathResolver;
use Liip\ImagineBundle\Imagine\DataLoader\LoaderInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;

class ImagineController
{
/**
* @var LoaderInterface
* @var DataManager
*/
private $dataLoader;
protected $dataManager;

/**
* @var FilterManager
*/
private $filterManager;
protected $filterManager;

/**
* @var CachePathResolver
* @var CacheManager
*/
private $cachePathResolver;
protected $cacheManager;

/**
* Constructor
* Constructor.
*
* @param LoaderInterface $dataLoader
* @param DataManager $dataManager
* @param FilterManager $filterManager
* @param CachePathResolver $cachePathResolver
* @param CacheManager $cacheManager
*/
public function __construct(LoaderInterface $dataLoader, FilterManager $filterManager, CachePathResolver $cachePathResolver = null)
public function __construct(DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager)
{
$this->dataLoader = $dataLoader;
$this->dataManager = $dataManager;
$this->filterManager = $filterManager;
$this->cachePathResolver = $cachePathResolver;
$this->cacheManager = $cacheManager;
}

/**
* This action applies a given filter to a given image,
* optionally saves the image and
* outputs it to the browser at the same time
* This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
*
* @param Symfony\Component\HttpFoundation\Request $request
* @param Request $request
* @param string $path
* @param string $filter
*
* @return Response
*/
public function filterAction(Request $request, $path, $filter)
{
$targetPath = false;
if ($this->cachePathResolver) {
$targetPath = $this->cachePathResolver->resolve($request, $path, $filter);
if ($targetPath instanceof Response) {
return $targetPath;
}
$targetPath = $this->cacheManager->resolve($request, $path, $filter);
if ($targetPath instanceof Response) {
return $targetPath;
}

$image = $this->dataLoader->find($path);
$image = $this->dataManager->find($filter, $path);
$response = $this->filterManager->get($request, $filter, $image, $path);

if ($targetPath && $response->isSuccessful()) {
$response = $this->cachePathResolver->store($response, $targetPath);
if ($targetPath) {
$response = $this->cacheManager->store($response, $targetPath, $filter);
}

return $response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public function process(ContainerBuilder $container)
$webRoot = $container->getParameter('liip_imagine.web_root');
$cachePrefix = $container->getParameter('liip_imagine.cache_prefix');
$filters = $container->getParameter('liip_imagine.filters');
$mode = $container->getParameter('liip_imagine.cache_mkdir_mode');

foreach ($filters as $filter => $options) {
$dir = isset($options['path'])
? $webRoot.$options['path']
: $webRoot.$cachePrefix.'/'.$filter;

if (!is_dir($dir) && !mkdir($dir, 0777, true)) {
if (!is_dir($dir) && !mkdir($dir, $mode, true)) {
throw new \RuntimeException(sprintf(
'Could not create directory for caching processed '.
'images in "%s"', $dir
Expand Down
22 changes: 21 additions & 1 deletion DependencyInjection/Compiler/LoadersCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,27 @@ public function process(ContainerBuilder $container)
$manager = $container->getDefinition('liip_imagine.filter.manager');

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addLoader', array($tag[0]['filter'], new Reference($id)));
$manager->addMethodCall('addLoader', array($tag[0]['loader'], new Reference($id)));
}
}

$tags = $container->findTaggedServiceIds('liip_imagine.data.loader');

if (count($tags) > 0 && $container->hasDefinition('liip_imagine.data.manager')) {
$manager = $container->getDefinition('liip_imagine.data.manager');

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addLoader', array($tag[0]['loader'], new Reference($id)));
}
}

$tags = $container->findTaggedServiceIds('liip_imagine.cache.resolver');

if (count($tags) > 0 && $container->hasDefinition('liip_imagine.cache.manager')) {
$manager = $container->getDefinition('liip_imagine.cache.manager');

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addResolver', array($tag[0]['resolver'], new Reference($id)));
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Liip\ImagineBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder,
Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
Expand All @@ -28,9 +28,14 @@ public function getConfigTreeBuilder()
->end()
->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')->defaultTrue()->end()
->scalarNode('loader')->defaultNull()->end()
->scalarNode('cache')->defaultValue('web_path')->end()
->scalarNode('cache_base_path')->defaultValue('')->end()
->booleanNode('cache_clearer')->defaultValue(true)->end()
->scalarNode('data_loader')->defaultValue('filesystem')->end()
->scalarNode('controller_action')->defaultValue('liip_imagine.controller:filterAction')->end()
->arrayNode('formats')
->defaultValue(array())
->prototype('scalar')->end()
Expand All @@ -39,11 +44,21 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('name')
->prototype('array')
->fixXmlConfig('filter', 'filters')
->useAttributeAsKey('name')
->children()
->scalarNode('path')->end()
->scalarNode('quality')->defaultValue(100)->end()
->scalarNode('format')->defaultNull()->end()
->scalarNode('cache')->defaultNull()->end()
->scalarNode('data_loader')->defaultNull()->end()
->scalarNode('controller_action')->defaultNull()->end()
->arrayNode('route')
->defaultValue(array())
->useAttributeAsKey('name')
->prototype('array')
->useAttributeAsKey('name')
->prototype('variable')->end()
->end()
->end()
->arrayNode('filters')
->useAttributeAsKey('name')
->prototype('array')
Expand Down
31 changes: 22 additions & 9 deletions DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Liip\ImagineBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Kernel;

class LiipImagineExtension extends Extension
{
Expand All @@ -21,28 +23,39 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('imagine.xml');

if ($config['cache_clearer']) {
$loader->load('cache_clearer.xml');
}

$container->setAlias('liip_imagine', new Alias('liip_imagine.'.$config['driver']));

$cachePrefix = $config['cache_prefix'] ? '/'.trim($config['cache_prefix'], '/') : '';
$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', $config['cache']);
$container->setParameter('liip_imagine.cache.resolver.default', $config['cache']);

foreach ($config['filter_sets'] as $filter => $options) {
if (isset($options['path'])) {
$config['filter_sets'][$filter]['path'] = '/'.trim($options['path'], '/');
}
}
$container->setParameter('liip_imagine.filter_sets', $config['filter_sets']);

if ($container->getParameter('liip_imagine.cache')) {
$controller = $container->getDefinition('liip_imagine.controller');
$controller->addArgument(new Reference('liip_imagine.cache.path.resolver'));
}
$container->setParameter('liip_imagine.data.loader.default', $config['data_loader']);

$container->setParameter('liip_imagine.controller_action', $config['controller_action']);

if (!empty($config['loader'])) {
$controller = $container->getDefinition('liip_imagine.controller');
$controller->replaceArgument(0, new Reference($config['loader']));
if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$container->removeDefinition('liip_imagine.cache.clearer');
}

$container->setParameter('liip_imagine.cache.resolver.base_path', $config['cache_base_path']);

$resources = $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : array();
$resources[] = 'LiipImagineBundle:Form:form_div_layout.html.twig';
$container->setParameter('twig.form.resources', $resources);
}
}
52 changes: 52 additions & 0 deletions Form/Type/ImageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Liip\ImagineBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* ImageType
*
* @author Emmanuel Vella <[email protected]>
*/
class ImageType extends AbstractType
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['image_path'] = $options['image_path'];
$view->vars['image_filter'] = $options['image_filter'];
$view->vars['image_attr'] = $options['image_attr'];
$view->vars['link_url'] = $options['link_url'];
$view->vars['link_filter'] = $options['link_filter'];
$view->vars['link_attr'] = $options['link_attr'];
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(array(
'image_path',
'image_filter',
));

$resolver->setDefaults(array(
'image_attr' => array(),
'link_url' => null,
'link_filter' => null,
'link_attr' => array(),
));
}

public function getParent()
{
return 'file';
}

public function getName()
{
return 'liip_imagine_image';
}
}
45 changes: 45 additions & 0 deletions Imagine/Cache/CacheClearer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Liip\ImagineBundle\Imagine\Cache;

use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;

/**
* Clears the Liip Imagine Bundle cache
*
* @author Josiah <[email protected]>
*/
class CacheClearer implements CacheClearerInterface
{
/**
* @var CacheManager
*/
protected $cacheManager;

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

/**
* Constructor.
*
* @param CacheManager $cacheManager
* @param string $cachePrefix The prefix applied to all cached images.
*/
public function __construct(CacheManager $cacheManager, $cachePrefix)
{
$this->cacheManager = $cacheManager;
$this->cachePrefix = $cachePrefix;
}

/**
* (non-PHPdoc)
* @see Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface::clear()
*/
public function clear($cacheDir)
{
// $cacheDir contains the application cache, which we don't care about
$this->cacheManager->clearResolversCache($this->cachePrefix);
}
}
Loading

0 comments on commit c117b88

Please sign in to comment.