Skip to content

Instantly share code, notes, and snippets.

@yuuan
Created April 16, 2015 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuuan/5db33ac52c14592e11ad to your computer and use it in GitHub Desktop.
Save yuuan/5db33ac52c14592e11ad to your computer and use it in GitHub Desktop.
<?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