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

[FrameworkBundle] Fix a regression in handling absolute template paths #17894

Merged
merged 1 commit into from Mar 3, 2016

Conversation

jakzal
Copy link
Contributor

@jakzal jakzal commented Feb 22, 2016

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #17777 #17683
License MIT
Doc PR -

Regression introduced by #15272.

@@ -56,7 +56,7 @@ public function parse($name)
throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
}

if (!preg_match('/^(?:([^:]*):)?(?:([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) {
if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || $this->isAbsolute($name)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two groups replaced with one here. This will only match "Foo:Bar:" or "".

@jakzal
Copy link
Contributor Author

jakzal commented Feb 22, 2016

@francoispluchino @wesleylancel would you mind checking if this fixes your issues please?

@francoispluchino
Copy link
Contributor

@jakzal It's OK for me! Thank you for this PR.

@jakzal
Copy link
Contributor Author

jakzal commented Feb 22, 2016

I just confirmed it also fixes the issue reported by @Congelli501. Waiting for @wesleylancel to either confirm his case's fixed, or to provide a failing scenario.

@fabpot
Copy link
Member

fabpot commented Feb 23, 2016

Not really related to this PR, but loading templates via an absolute path looks terrible.

@francoispluchino
Copy link
Contributor

@fabpot I agree with you if you load a template from a Bundle or App Resources, but when the template file is generated dynamically, and it is not in a Bundle we have no other choice.

Personally, I have this problem with the mail templates that are accessible via a custom stream, with the email generator that provided me the absolute path of the template. So, dynamically add the template file before loading the template is not very subtle, Because the templating.locator will search the template, while the path is already known ...

Of course, I am interested by best practices, if you have any.

@jakzal
Copy link
Contributor Author

jakzal commented Feb 24, 2016

Back to the topic... I confirmed wesleylancel's problem still persists. I'll work on a fix this afternoon.

@jakzal
Copy link
Contributor Author

jakzal commented Feb 29, 2016

@hcomnetworkers can you try this patch please?

status: needs review

@hcomnetworkers
Copy link

@jakzal I have tested your patch and it works for #17958, thank you!

@jakzal
Copy link
Contributor Author

jakzal commented Mar 1, 2016

Several people confirmed this fixes the issue for them.

status: reviewed

@@ -72,4 +72,9 @@ public function parse($name)

return $this->cache[$name] = $template;
}

private function isAbsolute($path)
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename to isAbsolutePath() as this is the name we are using elsewhere.

Also, I'd prefer to use a more restrictive approach like done elsewhere:

    private function isAbsolutePath($file)
    {
        return $file[0] === '/' || $file[0] === '\\'
            || (strlen($file) > 3 && ctype_alpha($file[0])
                && $file[1] === ':'
                && ($file[2] === '\\' || $file[2] === '/')
            );
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This won't work with windows paths which are "normalised" for some reasons here:

$name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', str_replace('\\', '/', $name)));

C:\\path\\to\\section\\name:foo.html.php becomes C:path/to/section/name:foo.html.php. I really don't know what's this for.

@jakzal jakzal force-pushed the template-name-parser-regression branch from de3d770 to fe1f5d2 Compare March 3, 2016 07:43
@jakzal jakzal force-pushed the template-name-parser-regression branch from fe1f5d2 to d8c493f Compare March 3, 2016 08:16
@fabpot
Copy link
Member

fabpot commented Mar 3, 2016

Thank you @jakzal.

@fabpot fabpot merged commit d8c493f into symfony:2.3 Mar 3, 2016
fabpot added a commit that referenced this pull request Mar 3, 2016
…mplate paths (jakzal)

This PR was merged into the 2.3 branch.

Discussion
----------

[FrameworkBundle] Fix a regression in handling absolute template paths

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17777 #17683
| License       | MIT
| Doc PR        | -

Regression introduced by #15272.

Commits
-------

d8c493f [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
@stof
Copy link
Member

stof commented Mar 3, 2016

but when the template file is generated dynamically, and it is not in a Bundle we have no other choice.

The right way to do this would be to come up with your own convention about template names and a template loader able to resolve it. See https://github.com/Behat/Borg/blob/2d8422d006b32cff929a0e475acb473258911baf/src/Integration/Symfony/Documentation/Twig/DocumentationLoader.php for an example doing it

@fabpot @jakzal what do you think about deprecating the possibility to use absolute paths ? AFAIK, this was never intended to work. It was just a side-effect of the initial implementation.

@fabpot
Copy link
Member

fabpot commented Mar 3, 2016

@stof Indeed, it works by chance. We should definitely deprecate this. see #17999

@jakzal
Copy link
Contributor Author

jakzal commented Mar 4, 2016

@stof indeed looks like many cases were supported unintentionally. Since there was no test cases for them I wasn't sure if it was intended. I'll send a PR to deprecated absolute paths.

@jakzal jakzal deleted the template-name-parser-regression branch March 4, 2016 09:04
@fabpot fabpot mentioned this pull request Mar 13, 2016
@@ -56,7 +56,7 @@ public function parse($name)
throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
}

if (!preg_match('/^(?:([^:]*):)?(?:([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) {
Copy link

Choose a reason for hiding this comment

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

That breaks FooBundle:Post/file.html.twig pattern

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.

None yet

8 participants