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

[Loader] [Locator] FileSystemLocator service must not be shared #875

Merged

Conversation

robfrawley
Copy link
Collaborator

Q A
Branch? 1.0
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #874
License MIT

This PR adds a compiler pass that gathers the Locator services and makes them not shared, causing the Symfony container to instantiate new objects each time they are requested (instead of the default behavior of re-using the first instance created).

This solves a bug introduced in 1.7.2 that causes those who use multiple FileSystemLoader definitions to only have their last path registered. Functional tests have been added to ensure a similar regression does not occur in the future.

Additionally, when the *Locator classes were implemented, exceptions originating from Symfony's OptionsResolver were ignored; this PR adds a try/catch block to the FileSystemLocator::setOptions() method and throws an exception originating from the Liip namespace so users don't have to worry about catching external dependencies' exceptions.


Note: This PR should not be merged into the 2.0 branch. The special handling seen in this PR is only required because the 1.0 branch supports both Symfony <2.8 and >=2.8. These versions require different mechanisms for achieving non-shared services (with <2.8 requiring "prototype" scoping and >=2.8 allowing the service to be simply defined as "not shared"). The 2.0 branch can achieve the same intent of this PR by simpy adding shared="false" to the locator service definitions.

<service shared="false" id="liip_imagine.binary.locator.filesystem" class="%liip_imagine.binary.locator.filesystem.class%" public="false"></service>
<service shared="false" id="liip_imagine.binary.locator.filesystem_insecure" class="%liip_imagine.binary.locator.filesystem_insecure.class%" public="false"></service>

@robfrawley robfrawley added Level: Bug 🐞 This item involves a legitimate regression (bug) to existing functionality. Attn: Critical This issue or PR is critical and should be rushed into a new release ASAP. State: Reviewing This item is being reviewed to determine if it should be accepted. labels Feb 12, 2017
@robfrawley robfrawley added this to the 1.7.3 milestone Feb 12, 2017
@robfrawley robfrawley self-assigned this Feb 12, 2017
@robfrawley robfrawley force-pushed the bugfix-locator-use-liip-namespace-exceptions branch from cfff97d to 3e2a117 Compare February 12, 2017 08:01
@robfrawley robfrawley force-pushed the bugfix-locator-use-liip-namespace-exceptions branch from 3e2a117 to 0695ad0 Compare February 12, 2017 09:09
return new Reference(
sprintf('liip_imagine.binary.locator.%s', $reference),
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
SymfonyFramework::hasDefinitionSharedToggle()
Copy link
Contributor

@sebastianblum sebastianblum Feb 12, 2017

Choose a reason for hiding this comment

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

The third argument (strict) was removed in symfony 3.0.
Should we use an switch like

if (symfony 2.x) {
    return new Reference(
        sprintf('liip_imagine.binary.locator.%s', $reference),
            ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
            false
    );

    return new Reference(sprintf('liip_imagine.binary.locator.%s', $config['locator']);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The reason I left it like this is because that will always be true for Symfony >=2.8, and therefore it doesn't matter that it's ignored. We only care about it when it is false on Symfony <2.8. Otherwise, the extra parameter can be, and is, safely ignored (the PHP interpreter allows and ignores any extra parameters). Do you really think it's necessary to add a switch?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good morning @robfrawley, not an easy question. You are right, the code is fine.
I would prefer a feature switch at the moment and remove it in future (maybe 2.0) when at least symfony 2.8 will be supported.

Copy link
Collaborator Author

@robfrawley robfrawley Feb 13, 2017

Choose a reason for hiding this comment

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

Ok; ammended the code to the following:

$name = sprintf('liip_imagine.binary.locator.%s', $reference);

if (SymfonyFramework::hasDefinitionSharedToggle()) {
    return new Reference($name);
}

return new Reference($name, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);

I didn't use a version toggle, as I want to use the exact mechanism that requires a non-strict reference, which can be found here. Also, I want to avoid a direct dependency on the Symfony kernel in-code for now.

@robfrawley robfrawley force-pushed the bugfix-locator-use-liip-namespace-exceptions branch from ba03e11 to 45c057c Compare February 15, 2017 06:22
@noniagriconomie
Copy link

Hi @robfrawley thank you for this PR that actually fix our problem too

Do you think it is going to be merged soon?

Many thanks :)

Copy link
Collaborator

@alexwilson alexwilson left a comment

Choose a reason for hiding this comment

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

Completely fine with this PR, and loving the feature toggling. Will make it much easier to reduce breakage of BC!

@robfrawley
Copy link
Collaborator Author

robfrawley commented Feb 20, 2017

@noniagriconomie It's excellent to hear that this PR resolves any issues you were encountering with this bundle! With regard to the rollout of this bugfix, I'll merge this PR into master by late tomorrow (UTC-5). I want to give the code a final review and perform some additional cleanup before I merge it. Afterward, you may immediately require the mainline release by using the 1.0.x-dev version string in your project's composer.json file.

As for the next stable release, I am shooting for this coming weekend. There a two additional PRs (which I will be posting shortly) that I would like to include in the upcoming release; those are the main reason for the short delay. At the latest, by Monday 2017-02-27 you will be able to require a stable version constraint and benefit from this fix via the 1.7.3 release.

@antoligy Thanks for allotting the time to provide feedback; it always feels wrong to merge PRs without some form of peer-review. :-) As always, much appreciated.

@noniagriconomie
Copy link

fine, waiting the publication of v1.7.3 then :)

@robfrawley robfrawley added State: Confirmed This item has been confirmed by maintainers as legitimate. and removed State: Reviewing This item is being reviewed to determine if it should be accepted. labels Feb 23, 2017
@robfrawley robfrawley merged commit 7767b32 into liip:1.0 Feb 27, 2017
@lsmith77 lsmith77 removed the State: Confirmed This item has been confirmed by maintainers as legitimate. label Feb 27, 2017
@robfrawley robfrawley mentioned this pull request Mar 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attn: Critical This issue or PR is critical and should be rushed into a new release ASAP. Level: Bug 🐞 This item involves a legitimate regression (bug) to existing functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants