88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\CMS\Shortcodes\Widget;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Webwizo\Shortcodes\Facades\Shortcode;
|
|
|
|
class BladeExtender extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap the application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
// ignore running from artisan
|
|
if (App::runningInConsole()) {
|
|
return;
|
|
}
|
|
|
|
view()->share('currentUrl', \Request::url());
|
|
|
|
$this->extendBlade();
|
|
|
|
Shortcode::register('widget', Widget::class);
|
|
}
|
|
|
|
/**
|
|
* Register the application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Creates pattern for matching Blade directives
|
|
*
|
|
* @param $function
|
|
*
|
|
* @return string
|
|
*/
|
|
private function createPattern($function)
|
|
{
|
|
return '/(?<!\w)(\s*)@' . $function . '\(([^)]+)*\)/';
|
|
}
|
|
|
|
/**
|
|
* Create new Blade directives
|
|
*/
|
|
private function extendBlade()
|
|
{
|
|
// extend Blade "@extends" macro - with this macro allows to use "hierarchy" in templates
|
|
Blade::extend(function ($view) {
|
|
$pattern = $this->createPattern('extendVisitor');
|
|
$code = '@extends(\App\CMS\Helpers\Template::name($2))';
|
|
|
|
return preg_replace($pattern, $code, $view);
|
|
});
|
|
|
|
$macros = [
|
|
'upload' => 'echo \App\CMS\Helpers\Path::upload($2);',
|
|
'thumbnail' => 'echo \App\CMS\Helpers\Path::thumbnail($2);',
|
|
'gravatar' => 'echo \App\CMS\Helpers\Gravatar::img($2);',
|
|
'asset' => 'echo \App\CMS\Helpers\Path::asset($2);',
|
|
'date' => 'echo \App\CMS\Helpers\Date::date($2);',
|
|
'customDate' => 'echo \App\CMS\Helpers\Date::dateFormatted($2);',
|
|
'monthName' => 'echo \App\CMS\Helpers\Date::monthName($2);',
|
|
'widgetArea' => '\App\CMS\Helpers\WidgetAreas::render($2);',
|
|
'widgetPreview' => 'echo \App\CMS\Helpers\Widget::render($2)'
|
|
];
|
|
|
|
foreach ($macros as $macro => $code) {
|
|
Blade::extend(function ($view) use ($macro, $code) {
|
|
$pattern = $this->createPattern($macro);
|
|
$code = '<?php ' . $code . ' ?>';
|
|
|
|
return preg_replace($pattern, $code, $view);
|
|
});
|
|
}
|
|
}
|
|
}
|