first commit
This commit is contained in:
50
app/Providers/AppServiceProvider.php
Normal file
50
app/Providers/AppServiceProvider.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user