diff --git a/config/rapidez.php b/config/rapidez.php index 3eb24f07a..18270d83b 100644 --- a/config/rapidez.php +++ b/config/rapidez.php @@ -47,6 +47,7 @@ 'category' => Rapidez\Core\Http\Controllers\CategoryController::class, ], + // The fully qualified class names of the models. 'models' => [ 'page' => Rapidez\Core\Models\Page::class, 'attribute' => Rapidez\Core\Models\Attribute::class, @@ -63,11 +64,19 @@ 'block' => Rapidez\Core\Models\Block::class, ], + // The fully qualified class names of the widgets. 'widgets' => [ 'Magento\Cms\Block\Widget\Block' => Rapidez\Core\Widgets\Block::class, 'Magento\CatalogWidget\Block\Product\ProductsList' => Rapidez\Core\Widgets\ProductList::class, ], + // The fully qualified class names of the content variables. + 'content-variables' => [ + Rapidez\Core\ContentVariables\Media::class, + Rapidez\Core\ContentVariables\Store::class, + Rapidez\Core\ContentVariables\Widget::class, + ], + // Localstorage keys that need to be flushed when the cache is cleared. 'flushable_localstorage_keys' => [ 'attributes', diff --git a/resources/views/page/overview.blade.php b/resources/views/page/overview.blade.php index 137eca0f0..3d09f2d69 100644 --- a/resources/views/page/overview.blade.php +++ b/resources/views/page/overview.blade.php @@ -12,7 +12,7 @@ @widget('content', 'pages', ($page->identifier == 'home' ? 'cms' : $page->identifier).'_index_index') @if($page->content)
- {!! $page->content !!} + @content($page->content)
@endif diff --git a/resources/views/widget/productlist.blade.php b/resources/views/widget/productlist.blade.php index bf777f018..837d9aede 100644 --- a/resources/views/widget/productlist.blade.php +++ b/resources/views/widget/productlist.blade.php @@ -1 +1 @@ - + diff --git a/src/ContentVariables/Media.php b/src/ContentVariables/Media.php new file mode 100644 index 000000000..8a82788c3 --- /dev/null +++ b/src/ContentVariables/Media.php @@ -0,0 +1,11 @@ +environment('production')) { + return '
'.__('Widget not implemented (:type).', compact('type')).'
'; + } + + return (new $widgetClass((object)$options))->render(); + }, $content); + } +} diff --git a/src/Models/Page.php b/src/Models/Page.php index 9d2233c8c..7a0ae95f4 100644 --- a/src/Models/Page.php +++ b/src/Models/Page.php @@ -4,12 +4,9 @@ use Rapidez\Core\Models\Scopes\ForCurrentStoreScope; use Rapidez\Core\Models\Scopes\IsActiveScope; -use Rapidez\Core\Models\Traits\HasContentAttributeWithVariables; class Page extends Model { - use HasContentAttributeWithVariables; - protected $table = 'cms_page'; protected $primaryKey = 'page_id'; diff --git a/src/Models/Traits/HasContentAttributeWithVariables.php b/src/Models/Traits/HasContentAttributeWithVariables.php deleted file mode 100644 index bb2d008d9..000000000 --- a/src/Models/Traits/HasContentAttributeWithVariables.php +++ /dev/null @@ -1,55 +0,0 @@ -$method($content); - } - } - - return $content; - } - - protected function processMediaeUrl(string $content): string - { - return preg_replace('/{{media url=("|"|\')(.*?)("|"|\')}}/m', config('rapidez.media_url').'/${2}', $content); - } - - protected function processStoreUrl(string $content): string - { - return preg_replace('/{{store (url|direct_url)=("|"|\')(.*?)("|"|\')}}/m', '/${3}', $content); - } - - protected function processWidgets(string $content): string - { - return preg_replace_callback('/{{widget type="(.*?)" (.*?)}}/m', function ($matches) { - $content = ''; - [$full, $type, $parameters] = $matches; - preg_match_all('/(.*?)="(.*?)"/m', $parameters, $parameters, PREG_SET_ORDER); - foreach ($parameters as $parameter) { - [$full, $parameter, $value] = $parameter; - $options[trim($parameter)] = trim($value); - } - - if (!isset($type)) { - return ''; - } - - $parseClass = config('rapidez.widgets.'.$type); - - if (!class_exists($parseClass) && !app()->environment('production')) { - return 'Widget not implemented.'; - } - $content .= (new $parseClass($options))->render(); - - return $content; - }, $content); - } -} diff --git a/src/Models/Widget.php b/src/Models/Widget.php index 1b3a05b22..0ef52c268 100644 --- a/src/Models/Widget.php +++ b/src/Models/Widget.php @@ -20,14 +20,4 @@ protected static function booted() $builder->join('widget_instance_page', 'widget_instance_page.instance_id', '=', 'widget_instance.instance_id'); }); } - - public function getContentAttribute(): string - { - $widget = config('rapidez.widgets.'.$this->instance_type); - if (!class_exists($widget) && !app()->environment('production')) { - return '
'.__('The ":type" widget type is not supported.', ['type' => $this->instance_type]).'
'; - } - - return (new $widget($this->widget_parameters->block_id))->render(); - } } diff --git a/src/Rapidez.php b/src/Rapidez.php index 640e4c5b9..9704c45a7 100644 --- a/src/Rapidez.php +++ b/src/Rapidez.php @@ -9,8 +9,26 @@ public function config(string $path, $default = null): ?string return config('rapidez.models.config')::getCachedByPath($path, $default); } - public function getContent($content) + public function content($content) { - return $this->getContentAttribute($content); + foreach (config('rapidez.content-variables') as $parser) { + $content = (new $parser)($content); + } + + return $content; + } + + public function fancyMagentoSyntaxDecoder(string $encodedString): object + { + $mapping = [ + '{' => '^[', + '}' => '^]', + '"' => '`', + '\\' => '|', + '<' => '^(', + '>' => '^)', + ]; + + return json_decode(str_replace(array_values($mapping), array_keys($mapping), $encodedString)); } } diff --git a/src/RapidezServiceProvider.php b/src/RapidezServiceProvider.php index 553552227..f4bdcfe3c 100644 --- a/src/RapidezServiceProvider.php +++ b/src/RapidezServiceProvider.php @@ -97,7 +97,7 @@ protected function bootBladeComponents(): self Blade::component('placeholder', PlaceholderComponent::class); Blade::directive('content', function ($expression) { - return "render($expression) ?>"; + return ""; }); Blade::directive('widget', function ($expression) { @@ -107,7 +107,7 @@ protected function bootBladeComponents(): self Blade::directive('block', function ($expression) { $blockModel = config('rapidez.models.block'); - return ""; + return ""; }); Blade::directive('config', function ($expression) { @@ -137,7 +137,6 @@ protected function registerBindings(): self { $this->app->bind('rapidez', Rapidez::class); $this->app->bind('widget-directive', WidgetDirective::class); - $this->app->bind('content-directive', ContentDirective::class); return $this; } diff --git a/src/VariableParsers.php b/src/VariableParsers.php deleted file mode 100644 index 0903dc141..000000000 --- a/src/VariableParsers.php +++ /dev/null @@ -1,67 +0,0 @@ -$method($content); - } - } - - return $content; - } - - protected function processMediaeUrl(string $content): string - { - return preg_replace('/{{media url=("|"|\')(.*?)("|"|\')}}/m', config('rapidez.media_url').'/${2}', $content); - } - - protected function processStoreUrl(string $content): string - { - return preg_replace('/{{store (url|direct_url)=("|"|\')(.*?)("|"|\')}}/m', '/${3}', $content); - } - - protected function processWidgets(string $content): string - { - return preg_replace_callback('/{{widget type="(.*?)" (.*?)}}/m', function ($matches) { - $content = ''; - [$full, $type, $parameters] = $matches; - preg_match_all('/(.*?)="(.*?)"/m', $parameters, $parameters, PREG_SET_ORDER); - foreach ($parameters as $parameter) { - [$full, $parameter, $value] = $parameter; - $options[trim($parameter)] = trim($value); - } - - if (!isset($type)) { - return ''; - } - - $parseClass = config('rapidez.widgets.'.$type); - - if (!class_exists($parseClass) && !app()->environment('production')) { - return 'Widget not implemented.'; - } - $content .= (new $parseClass($options))->render(); - - return $content; - }, $content); - } - - public static function fancyMagentoSyntaxDecoder(string $encodedString): object - { - $mapping = [ - '{' => '^[', - '}' => '^]', - '"' => '`', - '\\' => '|', - '<' => '^(', - '>' => '^)', - ]; - - return json_decode(str_replace(array_values($mapping), array_keys($mapping), $encodedString)); - } -} diff --git a/src/ViewDirectives/ContentDirective.php b/src/ViewDirectives/ContentDirective.php deleted file mode 100644 index 10900942b..000000000 --- a/src/ViewDirectives/ContentDirective.php +++ /dev/null @@ -1,13 +0,0 @@ -getContent($content); - } -} diff --git a/src/ViewDirectives/WidgetDirective.php b/src/ViewDirectives/WidgetDirective.php index 45138a2dc..64acf3592 100644 --- a/src/ViewDirectives/WidgetDirective.php +++ b/src/ViewDirectives/WidgetDirective.php @@ -3,6 +3,7 @@ namespace Rapidez\Core\ViewDirectives; use Illuminate\Support\Facades\Cache; +use Rapidez\Core\RapidezFacade as Rapidez; class WidgetDirective { @@ -27,7 +28,15 @@ function () use ($location, $type, $handle, $entities) { } foreach ($widgets->get() as $widget) { - $html .= $widget->content; + $widgetClass = config('rapidez.widgets.'.$widget->instance_type); + + if (!class_exists($widgetClass)) { + if (!app()->environment('production')) { + $html .= '
'.__('Widget not implemented (:type).', ['type' => $widget->instance_type]).'
'; + } + } else { + $html .= (new $widgetClass($widget->widget_parameters))->render(); + } } return $html; diff --git a/src/Widgets/Block.php b/src/Widgets/Block.php index 3166ef7f0..602727b29 100644 --- a/src/Widgets/Block.php +++ b/src/Widgets/Block.php @@ -2,19 +2,21 @@ namespace Rapidez\Core\Widgets; +use Rapidez\Core\RapidezFacade as Rapidez; + class Block { public String $blockId; public function __construct($vars) { - $this->blockId = is_array($vars) ? $vars['block_id'] : json_decode($vars)->block_id; + $this->blockId = is_object($vars) ? $vars->block_id : json_decode($vars)->block_id; } public function render() { $blockModel = config('rapidez.models.block'); - return $blockModel::find($this->blockId)->content; + return Rapidez::content($blockModel::find($this->blockId)->content); } } diff --git a/src/Widgets/ProductList.php b/src/Widgets/ProductList.php index 9d5519abb..95a7a8af4 100644 --- a/src/Widgets/ProductList.php +++ b/src/Widgets/ProductList.php @@ -3,18 +3,17 @@ namespace Rapidez\Core\Widgets; use Illuminate\Support\Collection; -use Rapidez\Core\Rapidez; +use Rapidez\Core\RapidezFacade as Rapidez; class ProductList { - protected Collection $conditions; protected $condition; - protected Collection $options; + protected object $options; public function __construct($options) { - $this->conditions = collect(Rapidez::fancyMagentoSyntaxDecoder($options['conditions_encoded'])); - $this->condition = $this->conditions->first(function ($condition) { + $conditions = collect(Rapidez::fancyMagentoSyntaxDecoder($options->conditions_encoded)); + $this->condition = $conditions->first(function ($condition) { return $condition->type == 'Magento\CatalogWidget\Model\Rule\Condition\Product'; }); $this->options = $options;