-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBridge] Added bundle name suggestion on wrongly overrided templates paths #26919
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
Conversation
$paths = array_unique($loaderPaths['(None)']); | ||
$bundleNames = array(); | ||
foreach ($paths as $path) { | ||
$relativePath = $path.'/bundles/'; |
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.
You don't need to check all paths because only <twig.default_path>/bundles/...
is taking into account:
symfony/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php
Lines 170 to 172 in 6bbb5bc
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) { | |
$bundleHierarchy[$name][] = $dir; | |
} |
Use the %twig.default_path%
parameter instead.
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.
and <kernel.root_dir>/Resources/<BundleName>/views
should be checked too?
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.
I had the same question but has it's a 4.1 feature, I prefered to use the new Twig override convention. The previous one is no more present in official documentation. Do you think it's necessary?
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.
For your first comment, I had the same idea but I did this choice for two reasons :
- I don't check all paths, just generic ones
- I prefered to use the result of other methods in the command that to duplicate code from another bundle.
Could you please tell me why is better to use the default path parameter?
Can someone give me another point of vue on it?
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.
Sorry for my short explanation, I'll try to be more --vvv
now :)
I don't check all paths, just generic ones
For instance, this is a generic one registered by TwigBundle:
(None) : vendor/symfony/twig-bridge/Resources/views/Form
and shouldn't be taken into account in this process because this path:
vendor/symfony/twig-bridge/Resources/views/Form/bundles/<BundleName>/
doesn't exists either as a Twig's path nor as a directory.
I prefered to use the result of other methods in the command that to duplicate code from another bundle.
My suggestion is about injecting the %twig.default_path%
parameter to the command and check in one of the path convention to override templates: <twig.default_path>/bundles/<BundleName>
. The another one <kernel.root_dir>/Resources/<BundleName>/views
is not promoted anymore but still is available, IMO would be good have this feature here too, so someone using this path can still benefit from it.
Could you please tell me why is better to use the default path parameter?
Because TwigBundle has just two paths convention to override templates where you need looking for, hence isn't necessary other paths.
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.
Done !
a3159d4
to
366d97a
Compare
|
||
public function __construct(Environment $twig, string $projectDir = null) | ||
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array(), string $twigDefaultPath, string $rootDir) |
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.
New parameters must have a default value to prevent a BC.
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.
Sorry for forgetting. I wrote it in the first version and forget it in the second one. Thank you
257aea3
to
94629b8
Compare
Status: Needs Review |
Is something missing? |
@@ -83,6 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |||
$data['tests'] = array_keys($data['tests']); | |||
$data['loader_paths'] = $this->getLoaderPaths(); | |||
$io->writeln(json_encode($data)); | |||
$this->displayAlternatives($this->findWrongBundleOverrides(), $io); |
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'll break the json output, maybe should be included into a new $data['warning']
above?
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.
Done. Thank you
637598d
to
4ad9593
Compare
src/Symfony/Bridge/Twig/CHANGELOG.md
Outdated
@@ -5,6 +5,7 @@ CHANGELOG | |||
----- | |||
|
|||
* add a `workflow_metadata` function | |||
* add bundle name suggestion on wrongly overrided templates paths |
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.
overridden
@@ -82,6 +88,22 @@ protected function execute(InputInterface $input, OutputInterface $output) | |||
} | |||
$data['tests'] = array_keys($data['tests']); | |||
$data['loader_paths'] = $this->getLoaderPaths(); | |||
$wrongBundles = $this->findWrongBundleOverrides(); | |||
if ($wrongBundles) { |
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.
if ($wrongBundles = $this->findWrongBundleOverrides()) {
if ($wrongBundles) { | ||
$data['warnings'] = array(); | ||
|
||
$messages = $this->buildWarningMessages($this->findWrongBundleOverrides()); |
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.
Use $wrongBundles
here?
@@ -82,6 +88,22 @@ protected function execute(InputInterface $input, OutputInterface $output) | |||
} | |||
$data['tests'] = array_keys($data['tests']); | |||
$data['loader_paths'] = $this->getLoaderPaths(); | |||
$wrongBundles = $this->findWrongBundleOverrides(); | |||
if ($wrongBundles) { | |||
$data['warnings'] = array(); |
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.
this var is overridden three lines below
} | ||
|
||
if (\count($bundleNames)) { | ||
$loadedBundles = $this->bundlesMetadata; |
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.
I don't see why we need that variable
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.
Thanks @xabbuh Code updated
d5bd6ec
to
54f66a7
Compare
Something to add? |
src/Symfony/Bridge/Twig/CHANGELOG.md
Outdated
@@ -5,6 +5,7 @@ CHANGELOG | |||
----- | |||
|
|||
* add a `workflow_metadata` function | |||
* add bundle name suggestion on wrongly overridden templates paths |
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.
this needs to be an item under a to be added 4.2.0
section
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.
Updated. Thank you
$alternatives = array(); | ||
$bundleNames = array(); | ||
|
||
if ($this->rootDir) { |
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.
I think we need to account for $this->projectDir
being null
here too.
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.
Done for this one and the next if too. Thank you
9d534ee
to
c687afc
Compare
@@ -82,6 +88,19 @@ protected function execute(InputInterface $input, OutputInterface $output) | |||
} | |||
$data['tests'] = array_keys($data['tests']); | |||
$data['loader_paths'] = $this->getLoaderPaths(); | |||
if ($wrongBundles = $this->findWrongBundleOverrides()) { | |||
$messages = $this->buildWarningMessages($wrongBundles); | |||
$data['warnings'] = array_reduce( |
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.
I am not sure I understand for what we need array_reduce()
here. $messages
already is just an array of strings, isn't it?
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.
You're right. It was just dead code. Done
array_walk( | ||
$messages, | ||
function ($message) use ($io) { | ||
$io->warning(trim($message)); |
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.
Can't we just trim in buildWarningMessages()
and then iterate the array here?
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.
Done
Move changelog entry to 4.1.0 to 4.2.0
Check if projectDir is not null before loop
Remove unnecessary loop Trim messages on build rather than on display
Thank you @pmontoya. |
…verrided templates paths (pmontoya, Pascal Montoya) This PR was merged into the 4.2-dev branch. Discussion ---------- [TwigBridge] Added bundle name suggestion on wrongly overrided templates paths | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26898 | License | MIT Report unknown folders in templates/bundles in debug:twig and make suggestions if any Developped with @bricejulia Commits ------- acfb325 refs #26898 Remove unnecessary loop Trim messages on build rather than on display da42b3e refs #26898 Check if projectDir is not null before loop da0c589 refs #26898 Move changelog entry to 4.1.0 to 4.2.0 7d9467a Add an entry on json export format bab9d99 Use %twig.default_path% parameter and search in old folder structure too 1758de2 [TwigBridge] Added bundle name suggestion on wrongly overrided templates paths
Report unknown folders in templates/bundles in debug:twig and make suggestions if any
Developped with @bricejulia