From 3b265833d87d0a2d3e48c0b45df375f40dbbe482 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Wed, 27 Dec 2023 13:12:24 -0600 Subject: [PATCH 1/2] fix typo and remove $ so gitclip works --- .../optimizations/resolve-cache-images-in-background.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Resources/doc/optimizations/resolve-cache-images-in-background.rst b/Resources/doc/optimizations/resolve-cache-images-in-background.rst index 4229862d..bed73519 100644 --- a/Resources/doc/optimizations/resolve-cache-images-in-background.rst +++ b/Resources/doc/optimizations/resolve-cache-images-in-background.rst @@ -7,7 +7,7 @@ the result. Then it redirects the client to the generated image file. This works without any further tooling. There are some important disadvantages however: -* Applying all the filters to an images can take a lot of time and memory; +* Applying all the filters to an image can take a lot of time and memory; * The images have to be processed by the web server answering web requests. This increases the load on the server and may affect performance; * The resolve controller URL is different from the cached image URL. When the image needs to be @@ -33,7 +33,7 @@ First, `install symfony/messenger`_ with composer: .. code-block:: terminal - $ composer require symfony/messenger + composer require symfony/messenger .. code-block:: yaml @@ -70,7 +70,7 @@ We need to run at least one consumer for the messages: .. code-block:: terminal - $ php bin/console messenger:consume liip_imagine --time-limit=3600 --memory-limit=256M + php bin/console messenger:consume liip_imagine --time-limit=3600 --memory-limit=256M You can run the consumers on a separate machine, as long as it shares the same storage for the cached images. In a cloud system, you could even scale consumers based on the queue size to get @@ -156,7 +156,7 @@ Here's how you can run it: .. code-block:: bash - $ ./bin/console enqueue:consume --setup-broker -vvv + ./bin/console enqueue:consume --setup-broker -vvv Step 4: Send resolve cache message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 80a51e80070f041f4c44354bddeec09d796c39cf Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Fri, 12 Jan 2024 11:11:23 +0100 Subject: [PATCH 2/2] Autowire loaders (#1561) * Automatically tag services, and defaults to their id * Update documentation about automatic service registering --------- Co-authored-by: homersimpsons --- .../Compiler/FiltersCompilerPass.php | 2 +- .../Compiler/LoadersCompilerPass.php | 2 +- .../Compiler/PostProcessorsCompilerPass.php | 2 +- LiipImagineBundle.php | 7 +++++ Resources/doc/data-loaders-custom.rst | 12 +++++++- Resources/doc/filters.rst | 14 ++++++++-- Resources/doc/post-processors.rst | 12 +++++++- Tests/LiipImagineBundleTest.php | 28 +++++++++++-------- 8 files changed, 61 insertions(+), 18 deletions(-) diff --git a/DependencyInjection/Compiler/FiltersCompilerPass.php b/DependencyInjection/Compiler/FiltersCompilerPass.php index 64302793..891eba47 100644 --- a/DependencyInjection/Compiler/FiltersCompilerPass.php +++ b/DependencyInjection/Compiler/FiltersCompilerPass.php @@ -24,7 +24,7 @@ public function process(ContainerBuilder $container): void $manager = $container->getDefinition('liip_imagine.filter.manager'); foreach ($tags as $id => $tag) { - $manager->addMethodCall('addLoader', [$tag[0]['loader'], new Reference($id)]); + $manager->addMethodCall('addLoader', [$tag[0]['loader'] ?? $id, new Reference($id)]); $this->log($container, 'Registered filter loader: %s', $id); } } diff --git a/DependencyInjection/Compiler/LoadersCompilerPass.php b/DependencyInjection/Compiler/LoadersCompilerPass.php index e3b7c898..74642aa2 100644 --- a/DependencyInjection/Compiler/LoadersCompilerPass.php +++ b/DependencyInjection/Compiler/LoadersCompilerPass.php @@ -24,7 +24,7 @@ public function process(ContainerBuilder $container): void $manager = $container->getDefinition('liip_imagine.data.manager'); foreach ($tags as $id => $tag) { - $manager->addMethodCall('addLoader', [$tag[0]['loader'], new Reference($id)]); + $manager->addMethodCall('addLoader', [$tag[0]['loader'] ?? $id, new Reference($id)]); $this->log($container, 'Registered binary loader: %s', $id); } } diff --git a/DependencyInjection/Compiler/PostProcessorsCompilerPass.php b/DependencyInjection/Compiler/PostProcessorsCompilerPass.php index 8d3b46bd..a3b45348 100644 --- a/DependencyInjection/Compiler/PostProcessorsCompilerPass.php +++ b/DependencyInjection/Compiler/PostProcessorsCompilerPass.php @@ -29,7 +29,7 @@ public function process(ContainerBuilder $container): void $manager = $container->getDefinition('liip_imagine.filter.manager'); foreach ($tags as $id => $tag) { - $manager->addMethodCall('addPostProcessor', [$tag[0]['post_processor'], new Reference($id)]); + $manager->addMethodCall('addPostProcessor', [$tag[0]['post_processor'] ?? $id, new Reference($id)]); $this->log($container, 'Registered filter post-processor: %s', $id); } } diff --git a/LiipImagineBundle.php b/LiipImagineBundle.php index 063ae4e8..8e9fd75e 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\Binary\Loader\LoaderInterface as BinaryLoaderInterface; use Liip\ImagineBundle\DependencyInjection\Compiler\AssetsVersionCompilerPass; use Liip\ImagineBundle\DependencyInjection\Compiler\DriverCompilerPass; use Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass; @@ -30,6 +31,8 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; +use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface as LoaderLoaderInterface; +use Liip\ImagineBundle\Imagine\Filter\PostProcessor\PostProcessorInterface; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -67,5 +70,9 @@ public function build(ContainerBuilder $container): void $extension->addLoaderFactory(new FileSystemLoaderFactory()); $extension->addLoaderFactory(new FlysystemLoaderFactory()); $extension->addLoaderFactory(new ChainLoaderFactory()); + + $container->registerForAutoconfiguration(LoaderLoaderInterface::class)->addTag('liip_imagine.filter.loader'); + $container->registerForAutoconfiguration(PostProcessorInterface::class)->addTag('liip_imagine.filter.post_processor'); + $container->registerForAutoconfiguration(BinaryLoaderInterface::class)->addTag('liip_imagine.binary.loader'); } } diff --git a/Resources/doc/data-loaders-custom.rst b/Resources/doc/data-loaders-custom.rst index 7aa23b26..7d83b0da 100644 --- a/Resources/doc/data-loaders-custom.rst +++ b/Resources/doc/data-loaders-custom.rst @@ -25,7 +25,17 @@ path to the image and needs to return an instance of ``BinaryInterface``. to sanitize this parameter in your loader to avoid exposing files outside of your image collections. -You need to `configure a service`_ with your custom loader and tag it with +Register it: automatically +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +By default, your loader will be automatically registered as it implements the ``LoaderInterface``. + +You will be able to reference and use your custom loader in your configuration by using its Fully Qualified Class Name. + +Register it: manually +^^^^^^^^^^^^^^^^^^^^^ + +If you want to give it a different name you need to `configure a service`_ with your custom loader and tag it with ``liip_imagine.binary.loader``. To register ``App\Service\MyCustomDataLoader`` with the name diff --git a/Resources/doc/filters.rst b/Resources/doc/filters.rst index 9c41d6b2..f7d20a08 100644 --- a/Resources/doc/filters.rst +++ b/Resources/doc/filters.rst @@ -53,7 +53,17 @@ The ``LoaderInterface`` has the method ``load``, which is provided an instance of ``ImageInterface`` and an array of options. It must return an ``ImageInterface``. -You need to `configure a service`_ and tag it ``liip_imagine.filter.loader``. +Register it: automatically +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +By default, your filter will be automatically registered as it implements the ``LoaderInterface``. + +You will be able to reference and use your custom filter when defining filter sets in your configuration by using its Fully Qualified Class Name. + +Register it: manually +^^^^^^^^^^^^^^^^^^^^^ + +If you want to give it a different name you need to `configure a service`_ and tag it ``liip_imagine.filter.loader``. To register a filter ``App\Service\MyCustomFilter`` as ``my_custom_filter``, use the following configuration: @@ -124,7 +134,7 @@ to the image, by passing configuration as third parameter to ``applyFilter``: public function filter(int $width, int $height) { $filter = '...'; // Name of the `filter_set` in `config/packages/liip_imagine.yaml` $path = '...'; // Path of the image, relative to `/public/` - + if (!$this->cacheManager->isStored($path, $filter)) { $binary = $this->dataManager->find($filter, $path); diff --git a/Resources/doc/post-processors.rst b/Resources/doc/post-processors.rst index 64b2e017..57fe3d47 100644 --- a/Resources/doc/post-processors.rst +++ b/Resources/doc/post-processors.rst @@ -131,7 +131,17 @@ for your custom post-processor. } } -You need to `configure a service`_ with your custom post-processor and tag it +Register it: automatically +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +By default, your post-processor will be automatically registered as it implements the ``PostProcessorInterface``. + +You will be able to reference and use your custom post-processor in your configuration by using its Fully Qualified Class Name. + +Register it: manually +^^^^^^^^^^^^^^^^^^^^^ + +If you want to give it a different name you need to `configure a service`_ with your custom post-processor and tag it with ``liip_imagine.filter.post_processor``. To register ``App\Service\MyCustomPostProcessor`` with the name diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index b70e40ab..232dd40a 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -32,6 +32,7 @@ use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Liip\ImagineBundle\LiipImagineBundle; use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -49,7 +50,7 @@ public function testSubClassOfBundle(): void public function testAddPasses(): void { $passes = []; - $containerMock = $this->createContainerBuilderMock(); + $containerMock = $this->createMock(ContainerBuilder::class); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') @@ -62,6 +63,10 @@ public function testAddPasses(): void return true; })); + $containerMock + ->expects($this->exactly(3)) + ->method('registerForAutoconfiguration') + ->willReturn($this->createMock(ChildDefinition::class)); $bundle = new LiipImagineBundle(); $bundle->build($containerMock); @@ -95,12 +100,16 @@ public function testAddResolvers(): void return true; })); - $containerMock = $this->createContainerBuilderMock(); + $containerMock = $this->createMock(ContainerBuilder::class); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') ->willReturn($extensionMock); + $containerMock + ->expects($this->exactly(3)) + ->method('registerForAutoconfiguration') + ->willReturn($this->createMock(ChildDefinition::class)); $bundle = new LiipImagineBundle(); $bundle->build($containerMock); @@ -123,12 +132,17 @@ public function testAddLoaders(): void return true; })); - $containerMock = $this->createContainerBuilderMock(); + $containerMock = $this->createMock(ContainerBuilder::class); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') ->willReturn($extensionMock); + $containerMock + ->expects($this->exactly(3)) + ->method('registerForAutoconfiguration') + ->willReturn($this->createMock(ChildDefinition::class)); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); @@ -140,14 +154,6 @@ public function testAddLoaders(): void ], $loaders); } - /** - * @return MockObject|ContainerBuilder - */ - protected function createContainerBuilderMock() - { - return $this->createObjectMock(ContainerBuilder::class, [], false); - } - /** * @return MockObject|LiipImagineExtension */