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

lazy twig extension: handle asset versioning, added imagine_filter_cache, better test coverage #1397

Merged
merged 2 commits into from
Oct 28, 2021

Conversation

dbu
Copy link
Member

@dbu dbu commented Oct 20, 2021

Q A
Branch? 2.x
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? -
Fixed tickets fix #1392, #1291, #1240, #1172
License MIT
Doc PR ✔️

While looking over things, i came up with a different approach that is less invasive than parse_url and as a bonus actually nicely handles asset versioning. this accidentally even provides a simplistic solution for #168 (with just one global version, which could be helpful when changing filter sets)

@coveralls
Copy link

coveralls commented Oct 20, 2021

Coverage Status

Coverage decreased (-0.7%) to 85.501% when pulling 13a32e4 on sync-lazy-twig into c7d67fb on 2.x.

@dbu dbu mentioned this pull request Oct 20, 2021
@dbu dbu force-pushed the sync-lazy-twig branch 3 times, most recently from 1d46052 to 242d0bf Compare October 20, 2021 16:16
@dbu
Copy link
Member Author

dbu commented Oct 20, 2021

@Schyzophrenic @GKFX you looked into this in the past, so maybe you have some feedback on this approach?

@Schyzophrenic
Copy link

@dbu I had a very brief look at this. I could be wrong but if you clean the path of everything after the ? I assume you won't be able to handle the http queries of a distant host such as an image provider where the parameters are critical to serve the right image.
I recall someone (you?) mentioned this could lead to security risks and that further discussions are required to decide whether this should be supported though.
It looks cleaner than before otherwise.

@dbu
Copy link
Member Author

dbu commented Oct 21, 2021

i inject the configured version string and check if the url ends with exactly that string. if it does not, i don't touch the url.

@dbu dbu changed the title add imagine_filter_cache to lazy twig extension and add tests lazy twig extension: handle asset versioning, added imagine_filter_cache, better test coverage Oct 21, 2021
@Schyzophrenic
Copy link

i inject the configured version string and check if the url ends with exactly that string. if it does not, i don't touch the url.

So I was actually thinking of a URL like this one: https://images2.productserve.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Awww.cdiscount.com%2Fpdt2%2F9%2F2%2F5%2F1%2F700x700%2FNIN4902370537925.jpg&feedId=38753&k=421fa9c064b91b3719c999b1a900cfb30d7e23de
In that case, you don't know in advance how the version will actually look like.
I am not very familiar with this though, so I could say something irrelevant...

@dbu
Copy link
Member Author

dbu commented Oct 21, 2021

that should now work again when using twig.mode: lazy. (i am not changing the legacy twig extension anymore.)

@Schyzophrenic
Copy link

that should now work again when using twig.mode: lazy. (i am not changing the legacy twig extension anymore.)

You're right I see it now! Thanks for the precision

return;
}

$versionStrategyDefinition = $container->getDefinition('assets._version__default');
Copy link
Member Author

Choose a reason for hiding this comment

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

getting the default symfony versioning service to copy the configuration from it, if available.

is there a better solution for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see another solution

*/
private function str_ends_with(string $haystack, string $needle): bool
{
return mb_substr($haystack, -mb_strlen($needle)) === $needle;
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to depend on mbstring for that (As UTF-8 is self-synchronizing, you won't do a mess when using non-mbstring functions to look for the end of the string being `?%%s')

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, here we probably don't strictly need it. we use mbstring in a couple of places already. i add ext-mbstring to composer.json and leave it here as well (php-cs-fixer also wants to have the mbstring variants, would need to disable that rule otherwise, and that could mess in other places where we do need it)

return;
}

$versionStrategyDefinition = $container->getDefinition('assets._version__default');
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see another solution

}

$versionStrategyDefinition = $container->getDefinition('assets._version__default');
if (!is_a($versionStrategyDefinition->getClass(), StaticVersionStrategy::class, true)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if you want to be safe, you should probably resolve parameters in the class name here (the DI component still supports them, even though it is considered a bad extension point and so core services stopped using that pattern). As that code is meant to detect custom strategies too, you cannot assume that it is a core definition.

Copy link
Member Author

@dbu dbu Oct 27, 2021

Choose a reason for hiding this comment

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

do i need the thing below, or on this line $container->resolveEnvPlaceholders($versionStrategyDefinition->getClass())?

Copy link
Member Author

Choose a reason for hiding this comment

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

or both?

Copy link
Member Author

Choose a reason for hiding this comment

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

i experimented with this and saw that parameters are resolved when we get to this compiler pass. probably indeed PassConfig::TYPE_BEFORE_REMOVING

}
$version = $versionStrategyDefinition->getArgument(0);
$format = $versionStrategyDefinition->getArgument(1);
$format = $container->resolveEnvPlaceholders($format);
Copy link
Member Author

Choose a reason for hiding this comment

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

@stof is this what you meant with resolving the parameters?

Copy link
Contributor

Choose a reason for hiding this comment

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

no. I said parameters, not env placeholders (env placeholders cannot be validated at compile-time, as their value can change at runtime by definition).

I meant $container->getParameterBag()->resolveValue($format) (although you might not need it as it is a BEFORE_REMOVING pass, and so parameters might already have been resolved by the DI component, to be checked)

Copy link
Member Author

@dbu dbu Oct 28, 2021

Choose a reason for hiding this comment

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

to debug, i changed the FrameworkExtension to set the format with a parameter and the assets.php service definition to use a class name parameter.

both get resolved before this compiler pass runs. so i only removed the wrong resolveEnvPlaceholders i added but don't add the parameter bag resolving.

@dbu dbu force-pushed the sync-lazy-twig branch 2 times, most recently from 4818440 to f144a0b Compare October 28, 2021 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants