Created
April 16, 2015 10:00
-
-
Save yuuan/5db33ac52c14592e11ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* `@param` で渡された変数が宣言されていないときに、 | |
* `debug` モードならエラーを出し、そうでなければ宣言して null を代入する | |
* | |
*/ | |
Blade::extend(function($view, $compiler) | |
{ | |
$pattern = $compiler->createMatcher('param'); | |
return preg_replace_callback($pattern, function($matches) | |
{ | |
$directive = trim($matches[0]); | |
$param = trim(trim($matches[2], '()')); | |
return <<<"EOT" | |
<?php | |
if (! array_key_exists(ltrim('{$param}', '$'), get_defined_vars())) | |
{ | |
if (Config::get('app.debug')) | |
{ | |
throw new InvalidArgumentException('`{$directive}` is invalid.'); | |
} | |
else | |
{ | |
{$param} = isset({$param}) ? {$param} : null; | |
} | |
} | |
?> | |
EOT; | |
}, $view); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment