Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
20.00% |
1 / 5 |
CRAP | |
63.33% |
19 / 30 |
BladeExtenderTrait | |
0.00% |
0 / 1 |
|
20.00% |
1 / 5 |
31.25 | |
63.33% |
19 / 30 |
attach | |
0.00% |
0 / 1 |
20.11 | |
65.22% |
15 / 23 |
|||
anonymous function | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
createMatcher | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
createOpenMatcher | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
createPlainMatcher | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
<?php | |
/** | |
* Adds an static function `attach` that if called, will instanciate and execute all functions as blade extends | |
*/ | |
namespace Radic\BladeExtensions\Traits; | |
use Illuminate\Foundation\Application; | |
/** | |
* Adds an static function `attach` that if called, will instanciate and execute all functions as blade extends | |
* | |
* @package Radic\BladeExtensions | |
* @version 2.1.0 | |
* @author Robin Radic | |
* @license MIT License - http://radic.mit-license.org | |
* @copyright 2011-2015, Robin Radic | |
* @link http://robin.radic.nl/blade-extensions | |
* | |
*/ | |
trait BladeExtenderTrait | |
{ | |
/** | |
* An array of methods that should be excluded by attach | |
* | |
* @var array | |
*/ | |
public $blacklist; | |
/** | |
* An array of methodName => directiveReplacement | |
* | |
* @var array | |
*/ | |
public $directives; | |
public $directivesFile; | |
public $overrides; | |
/** | |
* Instanciate and execute all functions as blade extends | |
* | |
* @param Application $app The current application | |
*/ | |
public static function attach(Application $app) | |
{ | |
/** @var \Illuminate\View\Compilers\BladeCompiler $blade */ | |
$blade = $app->make('blade.compiler'); | |
$config = $app->make('config'); | |
$class = new static; | |
if (!isset($class->directivesFile)) { | |
$class->directivesFile = __DIR__ . '/../directives.php'; | |
} | |
$blacklist = isset($class->blacklist) ? $class->blacklist : $config->get('blade_extensions.blacklist'); | |
$directives = isset($class->directives) ? $class->directives : $app->make('files')->getRequire($class->directivesFile); | |
$overrides = isset($class->overrides) ? $class->overrides : $config->get('blade_extensions.overrides', [ ]); | |
foreach ($overrides as $method => $override) { | |
if (! isset($directives[ $method ])) { | |
continue; | |
} | |
if (isset($override[ 'pattern' ])) { | |
$directives[ $method ][ 'pattern' ] = $override[ 'pattern' ]; | |
} | |
if (isset($override[ 'replacement' ])) { | |
$directives[ $method ][ 'replacement' ] = $override[ 'replacement' ]; | |
} | |
} | |
foreach ($directives as $name => $directive) { | |
$method = 'directive' . ucfirst($name); | |
if ((is_array($blacklist) && in_array($name, $blacklist, true)) || ! method_exists($class, $method)) { | |
continue; | |
} | |
$blade->extend(function ($value) use ($class, $method, $directive, $app, $blade) { | |
return $class->$method($value, $directive[ 'pattern' ], $directive[ 'replacement' ], $app, $blade); | |
}); | |
} | |
} | |
/** | |
* Get the regular expression for a generic Blade function. | |
* | |
* @param string $function | |
* @return string | |
*/ | |
public function createMatcher($function) | |
{ | |
return '/(?<!\w)(\s*)@' . $function . '(\s*\(.*\))/'; | |
} | |
/** | |
* Get the regular expression for a generic Blade function. | |
* | |
* @param string $function | |
* @return string | |
*/ | |
public function createOpenMatcher($function) | |
{ | |
return '/(?<!\w)(\s*)@' . $function . '(\s*\(.*)\)/'; | |
} | |
/** | |
* Create a plain Blade matcher. | |
* | |
* @param string $function | |
* @return string | |
*/ | |
public function createPlainMatcher($function) | |
{ | |
return '/(?<!\w)(\s*)@' . $function . '(\s*)/'; | |
} | |
} |