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

Fix service definitions for mime and extension guessers #1379

Merged
merged 2 commits into from
Oct 6, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) /~https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Changes the `liip_imagine.mime_type_guesser` and `liip_imagine.extension_guesser` services to be aliases of the
* `mime_types` service provided by the FrameworkBundle when available.
*
* This compiler pass can be removed when dropping support for Symfony 4.2 and earlier.
*
* @internal
*/
final class MaybeSetMimeServicesAsAliasesCompilerPass extends AbstractCompilerPass
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Purposefully final and internal to make it clear this is a B/C layer that can (and should) go away when older Symfony version support is dropped.

{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('mime_types')) {
$container->removeDefinition('liip_imagine.mime_type_guesser');
$container->removeDefinition('liip_imagine.extension_guesser');
Copy link
Member

Choose a reason for hiding this comment

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

could this be a BC break if people customized one of these services?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but as is these definitions already are invalid when symfony/http-foundation:>=5.0 is installed as the Symfony\Component\HttpFoundation\File\MimeType namespace just does not exist anymore. So, there are 3 realistic (IMO) options:

  1. This PR, which has a potential B/C break in changing a service definition to a service alias (and, I guess the same B/C concerns as the next item...)

  2. Change the service definitions to use the right classes conditionally directly in the container extension class (which, depending on the B/C policy could also be a break because it will change the classes provided by these services and cause existing typehints or instanceof checks to fail if they aren't expecting the counterparts from the symfony/mime component)

  3. Introduce new services that always point to the new classes and deprecate the existing services, which adds a lot of maintenance burden in having the bundle try to resolve to the correct services based on the active Symfony version, and if anyone is trying to use these services in their own applications they have to go through the effort of finding the right service for their Symfony version

IMO, I'd say this PR is the "best" option moving forward.

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the clarification. yeah, lets do it this way. i will add a note in UPGRADE.md


$container->setAlias('liip_imagine.mime_type_guesser', 'mime_types');
$container->setAlias('liip_imagine.extension_guesser', 'mime_types');

$message = 'Replaced the "%s" and "%s" service definitions with aliases to "%s"';

$this->log($container, $message, 'liip_imagine.mime_type_guesser', 'liip_imagine.extension_guesser', 'mime_types');
}
}
}
2 changes: 2 additions & 0 deletions LiipImagineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Liip\ImagineBundle\DependencyInjection\Compiler\DriverCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\LoadersCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\MaybeSetMimeServicesAsAliasesCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\NonFunctionalFilterExceptionPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\PostProcessorsCompilerPass;
Expand Down Expand Up @@ -47,6 +48,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new PostProcessorsCompilerPass());
$container->addCompilerPass(new ResolversCompilerPass());
$container->addCompilerPass(new MetadataReaderCompilerPass());
$container->addCompilerPass(new MaybeSetMimeServicesAsAliasesCompilerPass());

if (class_exists(AddTopicMetaPass::class)) {
$container->addCompilerPass(AddTopicMetaPass::create()
Expand Down