From fb1e4a84650b864f35820e7fcd44d898aa997a15 Mon Sep 17 00:00:00 2001 From: nrg Date: Mon, 18 Jun 2018 14:41:55 +0200 Subject: [PATCH 1/4] added ability to inject custom drivers --- .../Compiler/DriverCompilerPass.php | 37 +++++++++++++++++++ DependencyInjection/Configuration.php | 9 +---- DependencyInjection/LiipImagineExtension.php | 4 +- LiipImagineBundle.php | 2 + Resources/config/imagine.xml | 18 +++++++-- Tests/LiipImagineBundleTest.php | 8 ++-- 6 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 DependencyInjection/Compiler/DriverCompilerPass.php diff --git a/DependencyInjection/Compiler/DriverCompilerPass.php b/DependencyInjection/Compiler/DriverCompilerPass.php new file mode 100644 index 000000000..176d9946b --- /dev/null +++ b/DependencyInjection/Compiler/DriverCompilerPass.php @@ -0,0 +1,37 @@ +getParameter('liip_imagine.driver_service'); + + if (!$container->hasDefinition($liipImagineDriver)) { + throw new InvalidConfigurationException(sprintf( + "Specified driver '%s' is not defined.", $liipImagineDriver + )); + } + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0bfa4fc4f..082c9990f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -104,14 +104,7 @@ public function getConfigTreeBuilder() $rootNode ->fixXmlConfig('filter_set', 'filter_sets') ->children() - ->scalarNode('driver')->defaultValue('gd') - ->validate() - ->ifTrue(function ($v) { - return !in_array($v, ['gd', 'imagick', 'gmagick'], true); - }) - ->thenInvalid('Invalid imagine driver specified: %s') - ->end() - ->end() + ->scalarNode('driver')->defaultValue('gd')->end() ->scalarNode('cache')->defaultValue('default')->end() ->scalarNode('cache_base_path')->defaultValue('')->end() ->scalarNode('data_loader')->defaultValue('default')->end() diff --git a/DependencyInjection/LiipImagineExtension.php b/DependencyInjection/LiipImagineExtension.php index 365907cd0..0eab4f2f6 100644 --- a/DependencyInjection/LiipImagineExtension.php +++ b/DependencyInjection/LiipImagineExtension.php @@ -81,9 +81,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('enqueue.xml'); } - $container->getDefinition('liip_imagine.'.$config['driver'])->addMethodCall('setMetadataReader', [ - new Reference('liip_imagine.meta_data.reader'), - ]); + $container->setParameter('liip_imagine.driver_service', 'liip_imagine.' . $config['driver']); $container->setAlias('liip_imagine', new Alias('liip_imagine.'.$config['driver'])); $container->setAlias(CacheManager::class, new Alias('liip_imagine.cache.manager', false)); diff --git a/LiipImagineBundle.php b/LiipImagineBundle.php index 90f56e4ec..44072bdb4 100644 --- a/LiipImagineBundle.php +++ b/LiipImagineBundle.php @@ -13,6 +13,7 @@ use Enqueue\Bundle\DependencyInjection\Compiler\AddTopicMetaPass; use Liip\ImagineBundle\Async\Topics; +use Liip\ImagineBundle\DependencyInjection\Compiler\DriverCompilerPass; use Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass; use Liip\ImagineBundle\DependencyInjection\Compiler\LoadersCompilerPass; use Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass; @@ -38,6 +39,7 @@ public function build(ContainerBuilder $container) { parent::build($container); + $container->addCompilerPass(new DriverCompilerPass()); $container->addCompilerPass(new LoadersCompilerPass()); $container->addCompilerPass(new FiltersCompilerPass()); $container->addCompilerPass(new PostProcessorsCompilerPass()); diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index ef2b9a488..223932166 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -83,11 +83,23 @@ - + + + + + - + + + + + - + + + + + diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index 169a197f8..e4a8a82a2 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -46,7 +46,7 @@ public function testAddLoadersCompilerPassOnBuild() ->with('liip_imagine') ->will($this->returnValue($this->createLiipImagineExtensionMock())); $containerMock - ->expects($this->at(0)) + ->expects($this->at(1)) ->method('addCompilerPass') ->with($this->isInstanceOf(LoadersCompilerPass::class)); @@ -63,7 +63,7 @@ public function testAddFiltersCompilerPassOnBuild() ->with('liip_imagine') ->will($this->returnValue($this->createLiipImagineExtensionMock())); $containerMock - ->expects($this->at(1)) + ->expects($this->at(2)) ->method('addCompilerPass') ->with($this->isInstanceOf(FiltersCompilerPass::class)); @@ -80,7 +80,7 @@ public function testAddPostProcessorsCompilerPassOnBuild() ->with('liip_imagine') ->will($this->returnValue($this->createLiipImagineExtensionMock())); $containerMock - ->expects($this->at(2)) + ->expects($this->at(3)) ->method('addCompilerPass') ->with($this->isInstanceOf(PostProcessorsCompilerPass::class)); @@ -97,7 +97,7 @@ public function testAddResolversCompilerPassOnBuild() ->with('liip_imagine') ->will($this->returnValue($this->createLiipImagineExtensionMock())); $containerMock - ->expects($this->at(3)) + ->expects($this->at(4)) ->method('addCompilerPass') ->with($this->isInstanceOf(ResolversCompilerPass::class)); From d1274a2213ef492646082c8571cffd54fcb05f34 Mon Sep 17 00:00:00 2001 From: nrg Date: Mon, 18 Jun 2018 14:52:28 +0200 Subject: [PATCH 2/4] fixed static tests --- DependencyInjection/Compiler/DriverCompilerPass.php | 4 ++-- DependencyInjection/LiipImagineExtension.php | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Compiler/DriverCompilerPass.php b/DependencyInjection/Compiler/DriverCompilerPass.php index 176d9946b..e5fade9ba 100644 --- a/DependencyInjection/Compiler/DriverCompilerPass.php +++ b/DependencyInjection/Compiler/DriverCompilerPass.php @@ -22,8 +22,8 @@ class DriverCompilerPass extends AbstractCompilerPass { /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function process(ContainerBuilder $container) { $liipImagineDriver = $container->getParameter('liip_imagine.driver_service'); diff --git a/DependencyInjection/LiipImagineExtension.php b/DependencyInjection/LiipImagineExtension.php index 0eab4f2f6..c5ada9f2d 100644 --- a/DependencyInjection/LiipImagineExtension.php +++ b/DependencyInjection/LiipImagineExtension.php @@ -18,7 +18,6 @@ use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; class LiipImagineExtension extends Extension @@ -81,7 +80,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('enqueue.xml'); } - $container->setParameter('liip_imagine.driver_service', 'liip_imagine.' . $config['driver']); + $container->setParameter('liip_imagine.driver_service', 'liip_imagine.'.$config['driver']); $container->setAlias('liip_imagine', new Alias('liip_imagine.'.$config['driver'])); $container->setAlias(CacheManager::class, new Alias('liip_imagine.cache.manager', false)); From 86bdf30dd5f70727de6ef1395306ada98490128e Mon Sep 17 00:00:00 2001 From: nrg Date: Mon, 18 Jun 2018 15:23:26 +0200 Subject: [PATCH 3/4] fixed static tests --- Binary/Locator/FileSystemInsecureLocator.php | 6 +++--- Binary/Locator/FileSystemLocator.php | 2 +- .../Factory/Loader/FileSystemLoaderFactory.php | 2 +- Tests/AbstractTest.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Binary/Locator/FileSystemInsecureLocator.php b/Binary/Locator/FileSystemInsecureLocator.php index 0073d1c95..12a90a277 100644 --- a/Binary/Locator/FileSystemInsecureLocator.php +++ b/Binary/Locator/FileSystemInsecureLocator.php @@ -21,9 +21,9 @@ class FileSystemInsecureLocator extends FileSystemLocator */ protected function generateAbsolutePath(string $root, string $path): ?string { - if (false === mb_strpos($path, '..'.DIRECTORY_SEPARATOR) && - false === mb_strpos($path, DIRECTORY_SEPARATOR.'..') && - false !== file_exists($absolute = $root.DIRECTORY_SEPARATOR.$path) + if (false === mb_strpos($path, '..'.\DIRECTORY_SEPARATOR) && + false === mb_strpos($path, \DIRECTORY_SEPARATOR.'..') && + false !== file_exists($absolute = $root.\DIRECTORY_SEPARATOR.$path) ) { return $absolute; } diff --git a/Binary/Locator/FileSystemLocator.php b/Binary/Locator/FileSystemLocator.php index e7eb1256d..4b3dd8288 100644 --- a/Binary/Locator/FileSystemLocator.php +++ b/Binary/Locator/FileSystemLocator.php @@ -60,7 +60,7 @@ public function locate(string $path): string */ protected function generateAbsolutePath(string $root, string $path): ?string { - if (false !== $absolute = realpath($root.DIRECTORY_SEPARATOR.$path)) { + if (false !== $absolute = realpath($root.\DIRECTORY_SEPARATOR.$path)) { return $absolute; } diff --git a/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php b/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php index f0f36e152..2f511f726 100644 --- a/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php +++ b/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php @@ -127,7 +127,7 @@ private function getBundleResourcePaths(ContainerBuilder $container) } return array_map(function ($path) { - return $path.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'public'; + return $path.\DIRECTORY_SEPARATOR.'Resources'.\DIRECTORY_SEPARATOR.'public'; }, $paths); } diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php index c70fc2c84..fbd43993b 100644 --- a/Tests/AbstractTest.php +++ b/Tests/AbstractTest.php @@ -50,8 +50,8 @@ abstract class AbstractTest extends TestCase protected function setUp() { - $this->fixturesPath = realpath(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'); - $this->temporaryPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'liip_imagine_test'; + $this->fixturesPath = realpath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'); + $this->temporaryPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'liip_imagine_test'; $this->filesystem = new Filesystem(); if ($this->filesystem->exists($this->temporaryPath)) { From e3d1476a05360a765429eb24ccc6aea08bbe0d77 Mon Sep 17 00:00:00 2001 From: nrg Date: Mon, 25 Jun 2018 16:38:04 +0200 Subject: [PATCH 4/4] removed backslashes from native constants --- Binary/Locator/FileSystemInsecureLocator.php | 6 +++--- Binary/Locator/FileSystemLocator.php | 2 +- .../Factory/Loader/FileSystemLoaderFactory.php | 2 +- Tests/AbstractTest.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Binary/Locator/FileSystemInsecureLocator.php b/Binary/Locator/FileSystemInsecureLocator.php index 12a90a277..0073d1c95 100644 --- a/Binary/Locator/FileSystemInsecureLocator.php +++ b/Binary/Locator/FileSystemInsecureLocator.php @@ -21,9 +21,9 @@ class FileSystemInsecureLocator extends FileSystemLocator */ protected function generateAbsolutePath(string $root, string $path): ?string { - if (false === mb_strpos($path, '..'.\DIRECTORY_SEPARATOR) && - false === mb_strpos($path, \DIRECTORY_SEPARATOR.'..') && - false !== file_exists($absolute = $root.\DIRECTORY_SEPARATOR.$path) + if (false === mb_strpos($path, '..'.DIRECTORY_SEPARATOR) && + false === mb_strpos($path, DIRECTORY_SEPARATOR.'..') && + false !== file_exists($absolute = $root.DIRECTORY_SEPARATOR.$path) ) { return $absolute; } diff --git a/Binary/Locator/FileSystemLocator.php b/Binary/Locator/FileSystemLocator.php index 4b3dd8288..e7eb1256d 100644 --- a/Binary/Locator/FileSystemLocator.php +++ b/Binary/Locator/FileSystemLocator.php @@ -60,7 +60,7 @@ public function locate(string $path): string */ protected function generateAbsolutePath(string $root, string $path): ?string { - if (false !== $absolute = realpath($root.\DIRECTORY_SEPARATOR.$path)) { + if (false !== $absolute = realpath($root.DIRECTORY_SEPARATOR.$path)) { return $absolute; } diff --git a/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php b/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php index 2f511f726..f0f36e152 100644 --- a/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php +++ b/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php @@ -127,7 +127,7 @@ private function getBundleResourcePaths(ContainerBuilder $container) } return array_map(function ($path) { - return $path.\DIRECTORY_SEPARATOR.'Resources'.\DIRECTORY_SEPARATOR.'public'; + return $path.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'public'; }, $paths); } diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php index fbd43993b..c70fc2c84 100644 --- a/Tests/AbstractTest.php +++ b/Tests/AbstractTest.php @@ -50,8 +50,8 @@ abstract class AbstractTest extends TestCase protected function setUp() { - $this->fixturesPath = realpath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'); - $this->temporaryPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'liip_imagine_test'; + $this->fixturesPath = realpath(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'); + $this->temporaryPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'liip_imagine_test'; $this->filesystem = new Filesystem(); if ($this->filesystem->exists($this->temporaryPath)) {