Skip to content

[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

Merged
merged 6 commits into from
Jun 19, 2018

Conversation

pmontoya
Copy link

@pmontoya pmontoya commented Apr 13, 2018

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

$paths = array_unique($loaderPaths['(None)']);
$bundleNames = array();
foreach ($paths as $path) {
$relativePath = $path.'/bundles/';
Copy link
Member

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:

if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
$bundleHierarchy[$name][] = $dir;
}

Use the %twig.default_path% parameter instead.

Copy link
Member

@yceruto yceruto Apr 13, 2018

Choose a reason for hiding this comment

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

Copy link
Author

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?

Copy link
Author

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?

Copy link
Member

@yceruto yceruto Apr 15, 2018

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.

Copy link
Author

Choose a reason for hiding this comment

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

Done !

@nicolas-grekas nicolas-grekas changed the title [TwigBridge] Added bundle name suggestion on wrongly overrided templa… [TwigBridge] Added bundle name suggestion on wrongly overrided templates paths Apr 14, 2018
@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Apr 14, 2018
@pmontoya pmontoya force-pushed the feature-26898 branch 6 times, most recently from a3159d4 to 366d97a Compare April 16, 2018 05:08

public function __construct(Environment $twig, string $projectDir = null)
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array(), string $twigDefaultPath, string $rootDir)
Copy link
Contributor

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.

Copy link
Author

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

@pmontoya pmontoya force-pushed the feature-26898 branch 2 times, most recently from 257aea3 to 94629b8 Compare April 16, 2018 07:32
@pmontoya
Copy link
Author

Status: Needs Review

@pmontoya
Copy link
Author

pmontoya commented May 2, 2018

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);
Copy link
Member

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?

Copy link
Author

Choose a reason for hiding this comment

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

Done. Thank you

@pmontoya pmontoya force-pushed the feature-26898 branch 2 times, most recently from 637598d to 4ad9593 Compare May 3, 2018 15:41
@@ -5,6 +5,7 @@ CHANGELOG
-----

* add a `workflow_metadata` function
* add bundle name suggestion on wrongly overrided templates paths
Copy link
Member

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) {
Copy link
Member

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());
Copy link
Member

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();
Copy link
Member

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;
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @xabbuh Code updated

@pmontoya pmontoya force-pushed the feature-26898 branch 2 times, most recently from d5bd6ec to 54f66a7 Compare May 14, 2018 10:07
@pmontoya
Copy link
Author

pmontoya commented Jun 5, 2018

Something to add?

@@ -5,6 +5,7 @@ CHANGELOG
-----

* add a `workflow_metadata` function
* add bundle name suggestion on wrongly overridden templates paths
Copy link
Member

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

Copy link
Author

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) {
Copy link
Member

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.

Copy link
Author

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

@pmontoya pmontoya force-pushed the feature-26898 branch 2 times, most recently from 9d534ee to c687afc Compare June 13, 2018 12:23
@@ -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(
Copy link
Member

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?

Copy link
Author

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));
Copy link
Member

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?

Copy link
Author

Choose a reason for hiding this comment

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

Done

Pascal Montoya and others added 6 commits June 18, 2018 12:27
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
@nicolas-grekas
Copy link
Member

Thank you @pmontoya.

@nicolas-grekas nicolas-grekas merged commit acfb325 into symfony:master Jun 19, 2018
nicolas-grekas added a commit that referenced this pull request Jun 19, 2018
…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
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants