From 632de23a7d0b84d66b624c8f308d6e0fa0a5ee8d Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 1 Aug 2021 10:55:31 +0300 Subject: [PATCH] feat(shortcodes): update Shortcodes parser #199 --- .../Parsers/{Shortcode.php => Shortcodes.php} | 62 +++++++++++++------ 1 file changed, 43 insertions(+), 19 deletions(-) rename src/flextype/Support/Parsers/{Shortcode.php => Shortcodes.php} (68%) diff --git a/src/flextype/Support/Parsers/Shortcode.php b/src/flextype/Support/Parsers/Shortcodes.php similarity index 68% rename from src/flextype/Support/Parsers/Shortcode.php rename to src/flextype/Support/Parsers/Shortcodes.php index 5dc1704613..7918b743bf 100644 --- a/src/flextype/Support/Parsers/Shortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes.php @@ -15,20 +15,16 @@ use function flextype; use function strings; -final class Shortcode +final class Shortcodes { /** - * The Shortcode's instance is stored in a static field. This field is an - * array, because we'll allow our Shortcode to have subclasses. Each item in - * this array will be an instance of a specific Shortcode's subclass. - * - * @var array + * Registry instance */ - private static $instances = []; + private static ?Shortcodes $instance = null; - /** - * Shortcode facade - */ + /** + * Shortcode facade + */ private $shortcodeFacade = null; /** @@ -55,6 +51,18 @@ protected function __construct() $this->shortcodeFacade = new ShortcodeFacade(); } + /** + * Gets the instance via lazy initialization (created on first usage) + */ + public static function getInstance(): Shortcodes + { + if (static::$instance === null) { + static::$instance = new self(); + } + + return static::$instance; + } + /** * Shortcode facade */ @@ -64,16 +72,32 @@ public function facade(): ShortcodeFacade } /** - * Returns Shortcode Instance + * Init Shortcodes */ - public static function getInstance(): Shortcode + public function initShortcodes(): void { - $cls = static::class; - if (! isset(self::$instances[$cls])) { - self::$instances[$cls] = new static(); + $shortcodes = registry()->get('flextype.settings.parsers.shortcodes'); + + if ( + ! isset($shortcodes) || + ! is_array($shortcodes) || + count($shortcodes) <= 0 + ) { + return; } - return self::$instances[$cls]; + foreach ($shortcodes as $shortcode) { + if (! isset($shortcode['path'])) { + continue; + } + + if (! file_exists(ROOT_DIR . $shortcode['path'])) { + + continue; + } + + include_once ROOT_DIR . $shortcode['path']; + } } /** @@ -124,15 +148,15 @@ public function parse(string $input) */ public function process(string $input, bool $cache = true) { - if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) { + if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) { $key = $this->getCacheID($input); - if ($dataFromCache = flextype('cache')->get($key)) { + if ($dataFromCache = cache()->get($key)) { return $dataFromCache; } $data = $this->facade()->process($input); - flextype('cache')->set($key, $data); + cache()->set($key, $data); return $data; }