Skip to content

Commit

Permalink
Content directive
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin committed Aug 24, 2021
1 parent c1d5a5f commit edbf945
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 163 deletions.
9 changes: 9 additions & 0 deletions config/rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion resources/views/page/overview.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@widget('content', 'pages', ($page->identifier == 'home' ? 'cms' : $page->identifier).'_index_index')
@if($page->content)
<div class="mb-5 prose prose-green">
{!! $page->content !!}
@content($page->content)
</div>
@endif
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/widget/productlist.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<x-rapidez::productlist field="{{ $condition->attribute }}.keyword" :value="explode(', ', $condition->value)" :size="$options['products_count']"/>
<x-rapidez::productlist field="{{ $condition->attribute }}.keyword" :value="explode(', ', $condition->value)" :size="$options->products_count"/>
11 changes: 11 additions & 0 deletions src/ContentVariables/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rapidez\Core\ContentVariables;

class Media
{
public function __invoke($content)
{
return preg_replace('/{{media url=("|&quot;|\')(.*?)("|&quot;|\')}}/m', config('rapidez.media_url').'/${2}', $content);
}
}
11 changes: 11 additions & 0 deletions src/ContentVariables/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rapidez\Core\ContentVariables;

class Store
{
public function __invoke($content)
{
return preg_replace('/{{store (url|direct_url)=("|&quot;|\')(.*?)("|&quot;|\')}}/m', '/${3}', $content);
}
}
30 changes: 30 additions & 0 deletions src/ContentVariables/Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rapidez\Core\ContentVariables;

class Widget
{
public function __invoke($content)
{
return preg_replace_callback('/{{widget type="(.*?)" (.*?)}}/m', function ($matches) {
[$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 '';
}

$widgetClass = config('rapidez.widgets.'.$type);

if (!class_exists($widgetClass) && !app()->environment('production')) {
return '<hr>'.__('Widget not implemented (:type).', compact('type')).'<hr>';
}

return (new $widgetClass((object)$options))->render();
}, $content);
}
}
3 changes: 0 additions & 3 deletions src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
55 changes: 0 additions & 55 deletions src/Models/Traits/HasContentAttributeWithVariables.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Models/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<hr>'.__('The ":type" widget type is not supported.', ['type' => $this->instance_type]).'<hr>';
}

return (new $widget($this->widget_parameters->block_id))->render();
}
}
22 changes: 20 additions & 2 deletions src/Rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
5 changes: 2 additions & 3 deletions src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function bootBladeComponents(): self
Blade::component('placeholder', PlaceholderComponent::class);

Blade::directive('content', function ($expression) {
return "<?php echo app('content-directive')->render($expression) ?>";
return "<?php echo Rapidez::content($expression) ?>";
});

Blade::directive('widget', function ($expression) {
Expand All @@ -107,7 +107,7 @@ protected function bootBladeComponents(): self
Blade::directive('block', function ($expression) {
$blockModel = config('rapidez.models.block');

return "<?php echo $blockModel::getCachedByIdentifier($expression) ?>";
return "<?php echo Rapidez::content($blockModel::getCachedByIdentifier($expression)) ?>";
});

Blade::directive('config', function ($expression) {
Expand Down Expand Up @@ -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;
}
Expand Down
67 changes: 0 additions & 67 deletions src/VariableParsers.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/ViewDirectives/ContentDirective.php

This file was deleted.

11 changes: 10 additions & 1 deletion src/ViewDirectives/WidgetDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rapidez\Core\ViewDirectives;

use Illuminate\Support\Facades\Cache;
use Rapidez\Core\RapidezFacade as Rapidez;

class WidgetDirective
{
Expand All @@ -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 .= '<hr>'.__('Widget not implemented (:type).', ['type' => $widget->instance_type]).'<hr>';
}
} else {
$html .= (new $widgetClass($widget->widget_parameters))->render();
}
}

return $html;
Expand Down
6 changes: 4 additions & 2 deletions src/Widgets/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 4 additions & 5 deletions src/Widgets/ProductList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit edbf945

Please sign in to comment.