Hey domeinznl,
I plugged this in to the previous code we had.
A couple things,
This won't work, it generates the wrong pattern
$pattern = $compiler->createPlainMatcher('@dmz_route');
/(?<!\w)(\s*)@@dmz_route(\s*)/
The other two options in the compiler won't work either, createOpenMatcher or createPlainMatcher
So a custom pattern to match the @dmz_route('string')
$pattern = '/(?<!\w)(\s*)@dmz_route(\s*\((.*)\))/';
/* - should return
array (size=4)
0 => string '@dmz_route('login')' (length=22)
1 => string '' (length=3)
2 => string '('login')' (length=9)
3 => string ''login'' (length=7)
*/
I removed the extra param $compiler, don't think it would have worked as the calling function in the compiler only passes in one var, the view string.
If you need to do that on a closure try this,
$this->blade->getCompiler()->extend(function($view) use ($compiler) { ...
Here is what I got working,
$this->blade->getCompiler()->extend(function($view)
{
// custom regex
$pattern = '/(?<!\w)(\s*)@dmz_route(\s*\((.*)\))/';
/* - should return in a preg_match
array (size=4)
0 => string '@dmz_route('login')' (length=22)
1 => string '' (length=3)
2 => string '('login')' (length=9)
3 => string ''login'' (length=7)
*/
// adjusted to use #3 var
$replace = '<?php global $router; echo $router->generate($3); ?>';
// should replace @dmz_route('login') with
/*
* <?php global $router; echo $router->generate('login'); ?>
*/
return preg_replace($pattern, $replace, $view);
});
Hope that helps
Thank you TerrePorter,
It works just as it should. Do you know where I can find some good articles about regex ? Don't know anything about that.
Greetings, Domeinznl
Glad it worked for your.
Actually I don't know of any good resources, I usually just do searches or look at the preg_match reference. Along with a lot of trial and error. The Reg Coach is very useful in testing regex, http://weitz.de/regex-coach/
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community