Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBundle] Enhance the twig not found exception #27535

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

behnoushnorouzi
Copy link
Contributor

@behnoushnorouzi behnoushnorouzi commented Jun 7, 2018

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

Improve error message to make it clear to developers what mistake they have made.

try {
return parent::findTemplate($template, $throw);
} catch (LoaderError $e) {
if ($e instanceof \Twig_Error_Loader && preg_match('/^([^:]*?)(?:Bundle)?:([^:]*+):(.+\.[^\.]+\.[^\.]+)$/', $template, $m) && 0 !== strpos($template, '@')) {
Copy link
Member

Choose a reason for hiding this comment

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

the instanceof will always be true. Twig\Error\LoaderError is a class alias of Twig_Error_Loader.

try {
return parent::findTemplate($template, $throw);
} catch (LoaderError $e) {
if ($e instanceof \Twig_Error_Loader && preg_match('/^([^:]*?)(?:Bundle)?:([^:]*+):(.+\.[^\.]+\.[^\.]+)$/', $template, $m) && 0 !== strpos($template, '@')) {
Copy link
Member

Choose a reason for hiding this comment

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

You should switch the regex match in the strpos. Checking the first char is faster.

Btw, you could optimize it even more:

if ('' !== $template && '@' !== $template[0] && preg_match(...)) {

Using strpos to check only the first char is not the fastest way.

@behnoushnorouzi behnoushnorouzi force-pushed the twig-enhance-not-found-exception branch 2 times, most recently from c832758 to 82d1db4 Compare June 7, 2018 15:11
@nicolas-grekas nicolas-grekas added this to the next milestone Jun 7, 2018

class NativeFilesystemLoaderTest extends TestCase
{
const DS = DIRECTORY_SEPARATOR;
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this const and use the PHP one instead.

try {
return parent::findTemplate($template, $throw);
} catch (LoaderError $e) {
if ('' === $template || '@' === $template[0] || !preg_match('/^([^:]*?)(?:Bundle)?:([^:]*+):(.+\.[^\.]+\.[^\.]+)$/', $template, $m)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use named patterns to make the code using $m more easily understandable?

@behnoushnorouzi
Copy link
Contributor Author

behnoushnorouzi commented Jun 8, 2018

Thank you for the review it's done.

Status: needs review

use Twig\Error\LoaderError;
use Twig\Loader\FilesystemLoader;

class NativeFilesystemLoader extends FilesystemLoader
Copy link
Member

Choose a reason for hiding this comment

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

you may want to add yourself as the author of this class :)

@behnoushnorouzi behnoushnorouzi force-pushed the twig-enhance-not-found-exception branch from a3b665c to a5a4d48 Compare June 8, 2018 13:07
@Lewiscowles1986
Copy link

Should this emit a notice or warning if used in production?

It seems like a development pattern, and can be switched out for the extended class. Once the suggestion is implemented it seems like switching to the standard class might be more helpful.

@nicolas-grekas nicolas-grekas changed the title Enhance the twig not found exception [TwigBundle] Enhance the twig not found exception Jun 9, 2018
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 9, 2018

@Lewiscowles1986 good idea, we could use the new class only in debug mode. Could be done in ExtensionPass based on the kernel.debug parameter. I'd also suggest marking the class @internal.

@stof
Copy link
Member

stof commented Jun 14, 2018

We don't even need to do it in a compiler pass. The debug parameter can be accessed in the DI extension (the kernel.* params can be accessed there safely, as they are set early)

@behnoushnorouzi behnoushnorouzi force-pushed the twig-enhance-not-found-exception branch 2 times, most recently from a5e0738 to 554b0b1 Compare June 14, 2018 12:18
Enhance the twig not found exception
@behnoushnorouzi behnoushnorouzi force-pushed the twig-enhance-not-found-exception branch from 554b0b1 to 32988b4 Compare June 14, 2018 12:22
@behnoushnorouzi
Copy link
Contributor Author

Thank you for the review it's done.
Status: needs review

@nicolas-grekas
Copy link
Member

Thank you @behnoushnorouzi.

@nicolas-grekas nicolas-grekas merged commit 32988b4 into symfony:master Jun 19, 2018
nicolas-grekas added a commit that referenced this pull request Jun 19, 2018
…noushnorouzi)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[TwigBundle] Enhance the twig not found exception

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

Improve error message to make it clear to developers what mistake they have made.

Commits
-------

32988b4 Enhance the twig not found exception
@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.

9 participants