-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[TwigBundle] Enhance the twig not found exception #27535
Conversation
de2c952
to
b7af45b
Compare
try { | ||
return parent::findTemplate($template, $throw); | ||
} catch (LoaderError $e) { | ||
if ($e instanceof \Twig_Error_Loader && preg_match('/^([^:]*?)(?:Bundle)?:([^:]*+):(.+\.[^\.]+\.[^\.]+)$/', $template, $m) && 0 !== strpos($template, '@')) { |
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.
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, '@')) { |
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 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.
c832758
to
82d1db4
Compare
|
||
class NativeFilesystemLoaderTest extends TestCase | ||
{ | ||
const DS = DIRECTORY_SEPARATOR; |
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 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)) { |
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 we use named patterns to make the code using $m
more easily understandable?
8358ca7
to
a3b665c
Compare
Thank you for the review it's done. Status: needs review |
use Twig\Error\LoaderError; | ||
use Twig\Loader\FilesystemLoader; | ||
|
||
class NativeFilesystemLoader extends FilesystemLoader |
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 may want to add yourself as the author of this class :)
a3b665c
to
a5a4d48
Compare
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. |
@Lewiscowles1986 good idea, we could use the new class only in debug mode. Could be done in |
We don't even need to do it in a compiler pass. The debug parameter can be accessed in the DI extension (the |
a5e0738
to
554b0b1
Compare
Enhance the twig not found exception
554b0b1
to
32988b4
Compare
Thank you for the review it's done. |
Thank you @behnoushnorouzi. |
…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
Improve error message to make it clear to developers what mistake they have made.