-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Moves default and user-configured overridden bundle path registration to a CompilerPass #30360
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81e291a
Moves default overriden bnudle path registration to a CompilerPass
alterphp c9b9db2
Fix typo in class name
alterphp f3873ae
Fix existing tests
alterphp 15b1e0a
Add tests for default overridden bundle paths
alterphp 9a8a379
Fix expectedDeprecation order in test
alterphp 14b5dc8
Move twig user-configured paths definition to OverriddenBundlePathPass
alterphp 49ede3f
Code style fix
alterphp 1b91241
Use type hinting
alterphp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/OverriddenBundlePathPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\Config\Resource\FileExistenceResource; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Registers the default and user-configured overriding bundles paths. | ||
*/ | ||
final class OverriddenBundlePathPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$twigLoaderFilesystemId = 'twig.loader.native_filesystem'; | ||
if (false === $container->hasDefinition($twigLoaderFilesystemId)) { | ||
return; | ||
} | ||
$twigLoaderFilesystemDefinition = $container->getDefinition($twigLoaderFilesystemId); | ||
|
||
$twigDefaultPath = $container->getParameter('twig.default_path'); | ||
|
||
// Adds Twig default_path relative overriden path on top | ||
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) { | ||
$defaultOverrideBundlePath = $container->getParameterBag()->resolveValue($twigDefaultPath).'/bundles/'.$name; | ||
|
||
if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) { | ||
@trigger_error(sprintf('Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultOverrideBundlePath), E_USER_DEPRECATED); | ||
|
||
$twigLoaderFilesystemDefinition->addMethodCall( | ||
'prependPath', | ||
[$dir, $this->normalizeBundleName($name)] | ||
); | ||
} | ||
$container->addResource(new FileExistenceResource($dir)); | ||
|
||
if (file_exists($defaultOverrideBundlePath)) { | ||
$twigLoaderFilesystemDefinition->addMethodCall( | ||
'prependPath', | ||
[$defaultOverrideBundlePath, $this->normalizeBundleName($name)] | ||
); | ||
} | ||
$container->addResource(new FileExistenceResource($defaultOverrideBundlePath)); | ||
} | ||
|
||
// Adds user-configured namespaced paths | ||
foreach ($container->getParameter('twig.namespaced_user_configured_paths') as $namespace => $paths) { | ||
foreach ($paths as $path) { | ||
$twigLoaderFilesystemDefinition->addMethodCall( | ||
'prependPath', | ||
[$path, $namespace] | ||
); | ||
} | ||
} | ||
} | ||
|
||
private function normalizeBundleName(string $name): string | ||
{ | ||
if ('Bundle' === substr($name, -6)) { | ||
$name = substr($name, 0, -6); | ||
} | ||
|
||
return $name; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @alterphp, re-thinking this in another way... instead, what about moving this registration (as late as possible) into the compiler pass and call to
addPath
always?I mean, G1 and G2 as before in the same place, and only G3 into the compiler pass, thus other bundles would
addPath
before G3 keeping the priority/order and also less changes than now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me some minutes to understand ... and it sounds far better than what I have done until now. I'm gonna take the time to refactor this.
Thanks for review !