Skip to content

Commit

Permalink
Merge pull request #566 from adam187/set-factories
Browse files Browse the repository at this point in the history
use setFactory service definition method for symfony >= 2.6 (when possible)
  • Loading branch information
makasim committed Jun 4, 2015
2 parents e9a2fa1 + 0177d6f commit 24c977c
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 32 deletions.
15 changes: 13 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ php:
- 5.5
- 5.6
- hhvm

env:
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION="~2.3||~3.0" SYMFONY_DEPRECATIONS_HELPER=weak

before_script:
- yes '' | pecl -q install -f mongo
- composer self-update
- sh -c 'if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;'
- composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source
- composer install --dev --prefer-source

Expand All @@ -27,9 +28,19 @@ matrix:
- env: SYMFONY_VERSION=dev-master
- php: hhvm
include:
- php: 5.5
env: SYMFONY_VERSION=2.3.*
- php: 5.5
env: SYMFONY_VERSION=2.4.*
- php: 5.5
env: SYMFONY_VERSION=2.5.*
- php: 5.5
env: SYMFONY_VERSION=2.6.*
- php: 5.5
env: SYMFONY_VERSION=2.7.* SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.5
env: SYMFONY_VERSION=2.8.* SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.5
env: SYMFONY_VERSION=3.0.* SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.5
env: SYMFONY_VERSION=dev-master
11 changes: 8 additions & 3 deletions DependencyInjection/Factory/Resolver/AwsS3ResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ class AwsS3ResolverFactory implements ResolverFactoryInterface
*/
public function create(ContainerBuilder $container, $resolverName, array $config)
{
$awsS3ClientId = 'liip_imagine.cache.resolver.'.$resolverName.'.client';
$awsS3ClientDefinition = new Definition('Aws\S3\S3Client');
$awsS3ClientDefinition->setFactoryClass('Aws\S3\S3Client');
$awsS3ClientDefinition->setFactoryMethod('factory');
if (method_exists($awsS3ClientDefinition, 'setFactory')) {
$awsS3ClientDefinition->setFactory(array('Aws\S3\S3Client', 'factory'));
} else {
// to be removed when dependency on Symfony DependencyInjection is bumped to 2.6
$awsS3ClientDefinition->setFactoryClass('Aws\S3\S3Client');
$awsS3ClientDefinition->setFactoryMethod('factory');
}
$awsS3ClientDefinition->addArgument($config['client_config']);
$awsS3ClientId = 'liip_imagine.cache.resolver.'.$resolverName.'.client';
$container->setDefinition($awsS3ClientId, $awsS3ClientDefinition);

$resolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.aws_s3');
Expand Down
25 changes: 25 additions & 0 deletions DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('imagine.xml');

$this->setFactories($container);

if (interface_exists('Imagine\Image\Metadata\MetadataReaderInterface')) {
$container->getDefinition('liip_imagine.'.$config['driver'])->addMethodCall('setMetadataReader', array(new Reference('liip_imagine.meta_data.reader')));
} else {
Expand Down Expand Up @@ -113,4 +115,27 @@ protected function loadLoaders(array $config, ContainerBuilder $container)
$factory->create($container, $loaderName, $loaderConfig[$factoryName]);
}
}

/**
* @param ContainerBuilder $container
*/
private function setFactories(ContainerBuilder $container)
{
$factories = array(
'liip_imagine.mime_type_guesser' => array('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser', 'getInstance'),
'liip_imagine.extension_guesser' => array('Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser', 'getInstance'),
);

foreach ($factories as $service => $factory) {
$definition = $container->getDefinition($service);
if (method_exists($definition, 'setFactory')) {
// to be inlined in imagine.xml when dependency on Symfony DependencyInjection is bumped to 2.6
$definition->setFactory($factory);
} else {
// to be removed when dependency on Symfony DependencyInjection is bumped to 2.6
$definition->setFactoryClass($factory[0]);
$definition->setFactoryMethod($factory[1]);
}
}
}
}
23 changes: 22 additions & 1 deletion Form/Type/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
Expand All @@ -14,6 +15,9 @@
*/
class ImageType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['image_path'] = $options['image_path'];
Expand All @@ -24,7 +28,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['link_attr'] = $options['link_attr'];
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array(
'image_path',
Expand All @@ -39,11 +46,25 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'file';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'liip_imagine_image';
Expand Down
29 changes: 22 additions & 7 deletions Imagine/Cache/Resolver/CacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Liip\ImagineBundle\Binary\BinaryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\HttpKernel\Kernel;

class CacheResolver implements ResolverInterface
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(Cache $cache, ResolverInterface $cacheResolver, arra
$optionsResolver = new OptionsResolver();
}

$this->setDefaultOptions($optionsResolver);
$this->configureOptions($optionsResolver);
$this->options = $optionsResolver->resolve($options);
}

Expand Down Expand Up @@ -222,18 +223,32 @@ protected function saveToCache($cacheKey, $content)
return false;
}

protected function setDefaultOptions(OptionsResolverInterface $resolver)
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'global_prefix' => 'liip_imagine.resolver_cache',
'prefix' => get_class($this->resolver),
'index_key' => 'index',
));

$resolver->setAllowedTypes(array(
'global_prefix' => 'string',
'prefix' => 'string',
'index_key' => 'string',
));
$allowedTypesList = array(
'global_prefix' => 'string',
'prefix' => 'string',
'index_key' => 'string',
);

if (version_compare(Kernel::VERSION_ID, '20600') >= 0) {
foreach ($allowedTypesList as $option => $allowedTypes) {
$resolver->setAllowedTypes($option, $allowedTypes);
}
} else {
$resolver->setAllowedTypes($allowedTypesList);
}

}

protected function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}
}
4 changes: 0 additions & 4 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,11 @@
<service
id="liip_imagine.mime_type_guesser"
class="Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface"
factory-class="Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser"
factory-method="getInstance"
/>

<service
id="liip_imagine.extension_guesser"
class="Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface"
factory-class="Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser"
factory-method="getInstance"
/>

<service id="liip_imagine.binary.mime_type_guesser" class="%liip_imagine.binary.mime_type_guesser.class%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;

/**
* @covers Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory<extended>
Expand Down Expand Up @@ -107,9 +108,59 @@ public function testCreateS3ClientDefinitionOnCreate()

$clientDefinition = $container->getDefinition('liip_imagine.cache.resolver.theresolvername.client');
$this->assertEquals('Aws\S3\S3Client', $clientDefinition->getClass());
$this->assertEquals(array('theClientConfigKey' => 'theClientConfigVal'), $clientDefinition->getArgument(0));
}

public function testCreateS3ClientDefinitionWithFactoryOnCreate()
{
if (version_compare(Kernel::VERSION_ID, '20600') < 0) {
$this->markTestSkipped('No need to test on symfony < 2.6');
}

$container = new ContainerBuilder();

$resolver = new AwsS3ResolverFactory();

$resolver->create($container, 'theResolverName', array(
'client_config' => array('theClientConfigKey' => 'theClientConfigVal'),
'bucket' => 'aBucket',
'acl' => 'aAcl',
'url_options' => array(),
'get_options' => array(),
'put_options' => array(),
'cache' => false,
'proxies' => array(),
));

$clientDefinition = $container->getDefinition('liip_imagine.cache.resolver.theresolvername.client');
$this->assertEquals(array('Aws\S3\S3Client', 'factory'), $clientDefinition->getFactory());

}

public function testLegacyCreateS3ClientDefinitionWithFactoryOnCreate()
{
if (version_compare(Kernel::VERSION_ID, '20600') >= 0) {
$this->markTestSkipped('No need to test on symfony >= 2.6');
}

$container = new ContainerBuilder();

$resolver = new AwsS3ResolverFactory();

$resolver->create($container, 'theResolverName', array(
'client_config' => array('theClientConfigKey' => 'theClientConfigVal'),
'bucket' => 'aBucket',
'acl' => 'aAcl',
'url_options' => array(),
'get_options' => array(),
'put_options' => array(),
'cache' => false,
'proxies' => array(),
));

$clientDefinition = $container->getDefinition('liip_imagine.cache.resolver.theresolvername.client');
$this->assertEquals('Aws\S3\S3Client', $clientDefinition->getFactoryClass());
$this->assertEquals('factory', $clientDefinition->getFactoryMethod());
$this->assertEquals(array('theClientConfigKey' => 'theClientConfigVal'), $clientDefinition->getArgument(0));
}

public function testWrapResolverWithProxyOnCreateWithoutCache()
Expand Down
46 changes: 46 additions & 0 deletions Tests/DependencyInjection/LiipImagineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\HttpKernel\Kernel;

/**
* @covers Liip\ImagineBundle\DependencyInjection\Configuration
Expand Down Expand Up @@ -62,6 +63,51 @@ public function testCustomRouteRequirements()
$this->assertEquals('value1', $variable1, sprintf('%s parameter is correct', $variable1));
}

/**
* @dataProvider factoriesProvider
*/
public function testFactoriesConfiguration($service, $factory)
{
if (version_compare(Kernel::VERSION_ID, '20600') < 0) {
$this->markTestSkipped('No need to test on symfony < 2.6');
}

$this->createEmptyConfiguration();
$definition = $this->containerBuilder->getDefinition($service);

$this->assertEquals($factory, $definition->getFactory());
}

/**
* @dataProvider factoriesProvider
*/
public function testLegacyFactoriesConfiguration($service, $factory)
{
if (version_compare(Kernel::VERSION_ID, '20600') >= 0) {
$this->markTestSkipped('No need to test on symfony >= 2.6');
}

$this->createEmptyConfiguration();
$definition = $this->containerBuilder->getDefinition($service);

$this->assertEquals($factory[0], $definition->getFactoryClass());
$this->assertEquals($factory[1], $definition->getFactoryMethod());
}

public function factoriesProvider()
{
return array(
array(
'liip_imagine.mime_type_guesser',
array('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser', 'getInstance'),
),
array(
'liip_imagine.extension_guesser',
array('Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser', 'getInstance'),
),
);
}

/**
* @return ContainerBuilder
*/
Expand Down
Loading

0 comments on commit 24c977c

Please sign in to comment.