Skip to content

Commit

Permalink
feat(shortcodes): update Shortcodes parser #199
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Aug 1, 2021
1 parent 221d3b8 commit 632de23
Showing 1 changed file with 43 additions and 19 deletions.
Expand Up @@ -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;

/**
Expand All @@ -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
*/
Expand All @@ -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'];
}
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 632de23

Please sign in to comment.