TanchoToplineCargo/app/Providers/AppServiceProvider.php

52 lines
1.6 KiB
PHP
Raw Normal View History

2024-05-05 04:47:49 +00:00
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use App\Providers\ShortcodeProcessor;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register()
{
$this->app->singleton(ShortcodeProcessor::class, function ($app) {
return new ShortcodeProcessor();
});
}
/**
* Bootstrap any application services.
*/
public function boot()
{
Blade::directive('shortcode', function ($expression) {
list($shortcodeName, $shortcodeAttributes) = explode(',', $expression, 2);
return "<?php echo view('shortcodes.$shortcodeName', $shortcodeAttributes)->render(); ?>";
});
Blade::directive('processshortcodes', function ($expression) {
return "<?php echo app('App\Providers\ShortcodeProcessor')->process($expression); ?>";
});
Blade::directive('customshortcode', function ($expression) {
return "<?php echo app('App\Providers\ShortcodeProcessor')->processShortcodes($expression); ?>";
});
Paginator::useBootstrap();
}
public function processShortcodes($content)
{
return preg_replace_callback('/\[([\w_]+)([^\]]*)\]/', function ($matches) {
$shortcodeName = $matches[1];
$shortcodeAttributes = str::of($matches[2])->trim()->replace("'", "\"")->__toString();
return "@shortcode('$shortcodeName', $shortcodeAttributes)";
}, $content);
}
}