Skip to content

Commit

Permalink
Add default resizer and default resizer adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Aug 27, 2016
1 parent eed9e7f commit c22af10
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 38 deletions.
34 changes: 29 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function getConfigTreeBuilder()
$this->addModelSection($node);
$this->addBuzzSection($node);
$this->addResizerSection($node);
$this->addAdapterSection($node);

return $treeBuilder;
}
Expand Down Expand Up @@ -326,12 +327,12 @@ private function addProvidersSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->defaultValue('sonata.media.provider.image')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.simple')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.default')->end()
->scalarNode('filesystem')->defaultValue('sonata.media.filesystem.local')->end()
->scalarNode('cdn')->defaultValue('sonata.media.cdn.server')->end()
->scalarNode('generator')->defaultValue('sonata.media.generator.default')->end()
->scalarNode('thumbnail')->defaultValue('sonata.media.thumbnail.format')->end()
->scalarNode('adapter')->defaultValue('sonata.media.adapter.image.gd')->end()
->scalarNode('adapter')->defaultValue('sonata.media.adapter.image.default')->end()
->arrayNode('allowed_extensions')
->prototype('scalar')->end()
->defaultValue(array('jpg', 'png', 'jpeg'))
Expand All @@ -352,7 +353,7 @@ private function addProvidersSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->defaultValue('sonata.media.provider.youtube')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.simple')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.default')->end()
->scalarNode('filesystem')->defaultValue('sonata.media.filesystem.local')->end()
->scalarNode('cdn')->defaultValue('sonata.media.cdn.server')->end()
->scalarNode('generator')->defaultValue('sonata.media.generator.default')->end()
Expand All @@ -365,7 +366,7 @@ private function addProvidersSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->defaultValue('sonata.media.provider.dailymotion')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.simple')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.default')->end()
->scalarNode('filesystem')->defaultValue('sonata.media.filesystem.local')->end()
->scalarNode('cdn')->defaultValue('sonata.media.cdn.server')->end()
->scalarNode('generator')->defaultValue('sonata.media.generator.default')->end()
Expand All @@ -377,7 +378,7 @@ private function addProvidersSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->defaultValue('sonata.media.provider.vimeo')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.simple')->end()
->scalarNode('resizer')->defaultValue('sonata.media.resizer.default')->end()
->scalarNode('filesystem')->defaultValue('sonata.media.filesystem.local')->end()
->scalarNode('cdn')->defaultValue('sonata.media.cdn.server')->end()
->scalarNode('generator')->defaultValue('sonata.media.generator.default')->end()
Expand Down Expand Up @@ -480,6 +481,29 @@ private function addResizerSection(ArrayNodeDefinition $node)
->end()
->end()
->end()
->arrayNode('resizers')
->addDefaultsIfNotSet()
->children()
->scalarNode('default')->defaultValue('sonata.media.resizer.simple')->end()
->end()
->end()
->end()
;
}

/**
* @param ArrayNodeDefinition $node
*/
private function addAdapterSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('adapters')
->addDefaultsIfNotSet()
->children()
->scalarNode('default')->defaultValue('sonata.media.adapter.image.gd')->end()
->end()
->end()
->end()
;
}
Expand Down
48 changes: 47 additions & 1 deletion DependencyInjection/SonataMediaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand Down Expand Up @@ -151,14 +152,16 @@ public function load(array $configs, ContainerBuilder $container)
$this->configureExtra($container, $config);
$this->configureBuzz($container, $config);
$this->configureProviders($container, $config);
$this->configureAdapters($container, $config);
$this->configureResizers($container, $config);
$this->configureClassesToCompile();
}

/**
* @param ContainerBuilder $container
* @param array $config
*/
public function configureProviders(ContainerBuilder $container, $config)
public function configureProviders(ContainerBuilder $container, array $config)
{
$container->getDefinition('sonata.media.provider.image')
->replaceArgument(5, array_map('strtolower', $config['providers']['image']['allowed_extensions']))
Expand Down Expand Up @@ -545,4 +548,47 @@ public function configureClassesToCompile()
'Sonata\\MediaBundle\\Twig\\Node\\ThumbnailNode',
));
}

/**
* @param ContainerBuilder $container
* @param array $config
*/
private function configureAdapters(ContainerBuilder $container, array $config)
{
foreach (array('gd', 'imagick', 'gmagick') as $adapter) {
if ($container->hasParameter('sonata.media.adapter.image.'.$adapter.'.class')) {
$container->register('sonata.media.adapter.image.'.$adapter, $container->getParameter('sonata.media.adapter.image.'.$adapter.'.class'));
}
}
$container->setAlias('sonata.media.adapter.image.default', $config['adapters']['default']);
}

/**
* @param ContainerBuilder $container
* @param array $config
*/
private function configureResizers(ContainerBuilder $container, array $config)
{
if ($container->hasParameter('sonata.media.resizer.simple.class')) {
$class = $container->getParameter('sonata.media.resizer.simple.class');
$definition = new Definition($class, array(
new Reference('sonata.media.adapter.image.default'),
'%sonata.media.resizer.simple.adapter.mode%',
new Reference('sonata.media.metadata.proxy'),
));
$container->setDefinition('sonata.media.resizer.simple', $definition);
}

if ($container->hasParameter('sonata.media.resizer.square.class')) {
$class = $container->getParameter('sonata.media.resizer.square.class');
$definition = new Definition($class, array(
new Reference('sonata.media.adapter.image.default'),
'%sonata.media.resizer.square.adapter.mode%',
new Reference('sonata.media.metadata.proxy'),
));
$container->setDefinition('sonata.media.resizer.square', $definition);
}

$container->setAlias('sonata.media.resizer.default', $config['resizers']['default']);
}
}
20 changes: 18 additions & 2 deletions Resizer/SquareResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
class SquareResizer implements ResizerInterface
{
/**
* ImagineInterface.
* @var ImagineInterface
*/
protected $adapter;

/**
* string.
* @var string
*/
protected $mode;

Expand All @@ -50,6 +50,22 @@ public function __construct(ImagineInterface $adapter, $mode, MetadataBuilderInt
$this->metadata = $metadata;
}

/**
* {@inheritdoc}
*/
public function getAdapter()
{
return $this->adapter;
}

/**
* {@inheritdoc}
*/
public function setAdapter(ImagineInterface $adapter)
{
$this->adapter = $adapter;
}

/**
* {@inheritdoc}
*/
Expand Down
17 changes: 3 additions & 14 deletions Resources/config/media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
<parameters>
<parameter key="sonata.media.resizer.simple.class">Sonata\MediaBundle\Resizer\SimpleResizer</parameter>
<parameter key="sonata.media.resizer.square.class">Sonata\MediaBundle\Resizer\SquareResizer</parameter>
<parameter key="sonata.media.adapter.image.gd.class">Imagine\Gd\Imagine</parameter>
<parameter key="sonata.media.adapter.image.imagick.class">Imagine\Imagick\Imagine</parameter>
<parameter key="sonata.media.adapter.image.gmagick.class">Imagine\Gmagick\Imagine</parameter>
</parameters>
<services>
<!-- image manipulation service -->
<service id="sonata.media.adapter.image.gd" class="Imagine\Gd\Imagine"/>
<service id="sonata.media.adapter.image.imagick" class="Imagine\Imagick\Imagine"/>
<service id="sonata.media.adapter.image.gmagick" class="Imagine\Gmagick\Imagine"/>
<service id="sonata.media.resizer.simple" class="%sonata.media.resizer.simple.class%">
<argument type="service" id="sonata.media.adapter.image.gd"/>
<argument>%sonata.media.resizer.simple.adapter.mode%</argument>
<argument type="service" id="sonata.media.metadata.proxy"/>
</service>
<service id="sonata.media.resizer.square" class="%sonata.media.resizer.square.class%">
<argument type="service" id="sonata.media.adapter.image.gd"/>
<argument>%sonata.media.resizer.square.adapter.mode%</argument>
<argument type="service" id="sonata.media.metadata.proxy"/>
</service>
<!-- CDN abstraction service -->
<service id="sonata.media.cdn.server" class="Sonata\MediaBundle\CDN\Server">
<argument/>
Expand Down
46 changes: 31 additions & 15 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,45 @@
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests;
namespace Sonata\MediaBundle\Tests\DependencyInjection;

use PHPUnit_Framework_TestCase;
use Sonata\MediaBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends \PHPUnit_Framework_TestCase
class ConfigurationTest extends PHPUnit_Framework_TestCase
{
public function testAdminFormat()
/**
* @var array
*/
protected $config;

public function setUp()
{
$processor = new Processor();
parent::setUp();

$config = $processor->processConfiguration(new Configuration(), array(
array(
'db_driver' => 'foo',
$configs = array(
'sonata_media' => array(
'db_driver' => 'doctrine_orm',
'default_context' => 'default',
),
));

$this->assertNotNull($config['admin_format']);
$this->assertSame(200, $config['admin_format']['width']);
$this->assertSame(false, $config['admin_format']['height']);
$this->assertSame(90, $config['admin_format']['quality']);
$this->assertSame('jpg', $config['admin_format']['format']);
$this->assertSame(true, $config['admin_format']['constraint']);
);
$processor = new Processor();
$configuration = new Configuration();
$this->config = $processor->processConfiguration($configuration, $configs);
}

public function testResizers()
{
$this->assertArrayHasKey('resizers', $this->config);
$this->assertArrayHasKey('default', $this->config['resizers']);
$this->assertEquals('sonata.media.resizer.simple', $this->config['resizers']['default']);
}

public function testAdapters()
{
$this->assertArrayHasKey('adapters', $this->config);
$this->assertArrayHasKey('default', $this->config['adapters']);
$this->assertEquals('sonata.media.adapter.image.gd', $this->config['adapters']['default']);
}
}
Loading

0 comments on commit c22af10

Please sign in to comment.