Raffales-LMS/app/Providers/AppServiceProvider.php
2024-04-16 15:43:24 +05:45

51 lines
1.5 KiB
PHP

<?php
namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use App\Providers\ShortcodeProcessor;
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); ?>";
});
}
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);
}
}