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

Allow to configure the imagine class #1615

Merged
merged 5 commits into from
Jul 16, 2018
Merged

Allow to configure the imagine class #1615

merged 5 commits into from
Jul 16, 2018

Conversation

Toflar
Copy link
Member

@Toflar Toflar commented Jul 11, 2018

Right now you cannot configure the desired Imagine class. This has two major drawbacks:

  1. If e.g. Imagick is installed on your system but it's broken somehow, you are lost. You cannot switch to Gd without adding another compiler pass.
  2. If you add your own Imagine implementation you cannot use that without adding another compiler pass.

This PR allows you to configure the class very easily:

contao:
    image:
        imagine_service: 'contao.image.imagine.gd'

@leofeyer leofeyer added this to the 4.6.0 milestone Jul 11, 2018
// Will throw an exception if the PHP implementation is not available
try {
new $class();
} catch (RuntimeException $e) {
Copy link
Member

Choose a reason for hiding this comment

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

What's the benefit of catching instead of letting it fail?

Copy link
Member

Choose a reason for hiding this comment

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

It will fall back to return Imagine::class; // see #616

@@ -145,6 +147,9 @@ function (string $value): string {
->arrayNode('imagine_options')
->addDefaultsIfNotSet()
->children()
->scalarNode('class')
Copy link
Member

Choose a reason for hiding this comment

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

The imagine_options array gets directly passed to setImagineOptions() https://github.com/contao/image/blob/dc9e2dc78533c6da1d7d0cf010b0b89b2b5ffd06/src/ResizeOptionsInterface.php#L29 so we should probably not add the class name here.

IMO it would be better to use contao.image.imagine_class instead.

Copy link
Member

@aschempp aschempp Jul 11, 2018

Choose a reason for hiding this comment

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

I agree

@ausi
Copy link
Member

ausi commented Jul 11, 2018

I’m not sure if this is the correct approach. How about keeping the existing compiler pass, removing the class from the services.yml in

class: Imagine\Gd\Imagine
and checking for ->getClass() === null?

public function process(ContainerBuilder $container): void
{
    $definition = $container->getDefinition('contao.image.imagine');
    
    if (null === $definition->getClass()) {
        $definition->setClass($this->getImagineImplementation());
    }
}

This way you can simply overwrite the used class in the service definition in your installation.

What do you think?

@aschempp
Copy link
Member

@ausi 's suggestion looks reasonable to me

@Toflar
Copy link
Member Author

Toflar commented Jul 12, 2018

Nope, I disagree here. The compiler pass was wrong from day one that's why I changed it. The class you would like to use is a configuration value. It's exactly the right place to do so and it doesn't need any compiler pass at all. But yes, I can change the key to contao.image.imagine_class.

@ausi
Copy link
Member

ausi commented Jul 12, 2018

But what if the custom Imagine class you want to use needs a constructor parameter or other injections?

IMO the services definition ist the right place for replacing services in your application.

@aschempp
Copy link
Member

But what if the custom Imagine class you want to use needs a constructor parameter or other injections?

That means you would need dependency injection and not just set the class as an argument. A completely new (maybe valid) case. Maybe we should generate a service on the fly and assign it as argument to that method?

@Toflar
Copy link
Member Author

Toflar commented Jul 12, 2018

I have a better idea, give me a few minutes :)

@Toflar
Copy link
Member Author

Toflar commented Jul 12, 2018

Okay, so now you can configure any service you like. This will allow to define your own service in the container (that has other arguments) and configure it here. If you don't configure anything at all, the best solution is chosen for you and aliased.
For convenience, I've added gd, imagick and gmagick so it's easy to configure if you want to use one of these three, see the updated initial example.

@@ -142,6 +142,10 @@ function (string $value): string {
->prototype('scalar')->end()
->defaultValue(['jpg', 'jpeg', 'gif', 'png', 'tif', 'tiff', 'bmp', 'svg', 'svgz'])
->end()
->scalarNode('imagine_service')
->info('By default Contao tries to take the best Imagine service out of Gmagick, Imagick and Gd (in this order). If you would like to use a different service, configure the service id here.')
Copy link
Member

Choose a reason for hiding this comment

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

When will this be displayed?

Copy link
Member Author

Choose a reason for hiding this comment

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

config:dump-reference :)

Copy link
Member

Choose a reason for hiding this comment

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

Nice. However we don't have this for any other parameter, therefore I would prefer to remove this from this PR and to create a separate PR which adds descriptions for all parameters. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah, it belongs to this PR. Just add the info in another commit :)

@leofeyer
Copy link
Member

Ok. Let's see what @ausi says and then merge this.

@ausi
Copy link
Member

ausi commented Jul 12, 2018

I still think that the services definition is the right place for replacing services in your application. We do that in Contao for example with the translator service. Every developer that is familiar with Symfony knows how to do that:

services:
    contao.image.imagine:
        class: Imagine\Gd\Imagine

To get knowledge of the configuration value you need to read the documentation first.

My suggestion from #1615 (comment) would solve your issues too, right?

@Toflar
Copy link
Member Author

Toflar commented Jul 12, 2018

I don't like that. I don't want to configure things in my services.yml. I already configure the imagine options in contao.image. So I would need to do things in 2 places.

@ausi
Copy link
Member

ausi commented Jul 12, 2018

But how do you configure which translator class should be used be the Symfony translator for example, or any other service?

@Toflar
Copy link
Member Author

Toflar commented Jul 12, 2018

No idea, it's not part of my problem at the moment 😄 The way this PR implements the service you choose is the same as it's done in thousands of bundles. I don't see the issue here. There's always many ways to achieve the same thing but this one is the one I like best.

@aschempp
Copy link
Member

aschempp commented Jul 12, 2018

That is not the same to me. Overriding the application is one thing, setting configuration is a different thing.

Manually defining your Imagine class is a configuration that makes sense, because people might prefer one over the other. Therefore, there should be a configuration so set it. It's like settings the JPEG quality, you could do that using a compiler pass and change the service argument, but that's just unnecessarily cumbersome. We want people to configure their Imagine service, if they know which one they can use on their server.

Same as with the Doctrine server version. You should configure it, but if you don't the system tries to detect the current version.

Copy link
Member

@ausi ausi left a comment

Choose a reason for hiding this comment

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

K, I’m convinced then 🙃

@leofeyer leofeyer merged commit 89a9201 into contao:master Jul 16, 2018
@leofeyer
Copy link
Member

Thank you @Toflar.

@Toflar Toflar deleted the configurable-imagine-class branch July 16, 2018 07:04
leofeyer pushed a commit that referenced this pull request Apr 2, 2020
…#1615)

Description
-----------

See https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage

Commits
-------

08212655 Add Google Analytics ga.js Cookies to StripCookiesSubscriber
3716fcde Update StripCookiesSubscriber.php
a05ff52c Update StripCookiesSubscriber.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants