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

Add default resizer and default resizer adapter #721

Closed
wants to merge 1 commit into from
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
34 changes: 29 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function getConfigTreeBuilder()
$this->addModelSection($node);
$this->addBuzzSection($node);
$this->addResizerSection($node);
$this->addAdapterSection($node);

return $treeBuilder;
}
Expand Down Expand Up @@ -310,12 +311,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 @@ -336,7 +337,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 @@ -349,7 +350,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 @@ -361,7 +362,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 @@ -464,6 +465,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
46 changes: 45 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 @@ -127,14 +128,57 @@ 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();
}

Copy link
Member

Choose a reason for hiding this comment

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

Missing doc block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

/**
* @param ContainerBuilder $container
* @param array $config
*/
public function configureProviders(ContainerBuilder $container, $config)
public function configureAdapters(ContainerBuilder $container, array $config)
Copy link
Member

Choose a reason for hiding this comment

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

Why public? Can we make this at least protected?

{
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
*/
public function configureResizers(ContainerBuilder $container, array $config)
Copy link
Member

Choose a reason for hiding this comment

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

Same here. Why public?

{
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']);
}

/**
* @param ContainerBuilder $container
* @param array $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
1 change: 1 addition & 0 deletions Resizer/ResizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sonata\MediaBundle\Resizer;

use Gaufrette\File;
use Imagine\Image\ImagineInterface;
Copy link
Member

Choose a reason for hiding this comment

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

Not related to this PR

use Sonata\MediaBundle\Model\MediaInterface;

interface ResizerInterface
Expand Down
2 changes: 1 addition & 1 deletion Resizer/SimpleResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class SimpleResizer implements ResizerInterface
{
/**
* @var ImagineInterface
* @var ImagineInterface.
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the .

*/
protected $adapter;

Expand Down
20 changes: 18 additions & 2 deletions Resizer/SquareResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,28 @@
class SquareResizer implements ResizerInterface
{
/**
* ImagineInterface.
* @var ImagineInterface.
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the .

*/
protected $adapter;

/**
Copy link
Member

Choose a reason for hiding this comment

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

Need a new line separation.

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

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

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

Expand Down
20 changes: 3 additions & 17 deletions Resources/config/media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,12 @@
<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
50 changes: 50 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

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

Missing Sonata headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

/*
* This file is part of the Sonata project.
*
* (c) Sonata Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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
{
protected $config;

public function setUp()
{
parent::setUp();

$configs = array(
'sonata_media' => array(
'db_driver' => 'doctrine_orm',
'default_context' => 'default',
),
);
$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