1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 06:06:45 +02:00

feat(parsers): standardise parsers container names with macroable ability for Parsers API. #519

This commit is contained in:
Awilum
2020-12-18 15:11:32 +03:00
parent 6efce42e25
commit 788e51555d
3 changed files with 67 additions and 25 deletions

View File

@@ -12,31 +12,73 @@ namespace Flextype\Support\Parsers;
use function flextype;
use function strings;
use ParsedownExtra;
class Markdown
{
/**
* Markdown
* The Markdown's instance is stored in a static field. This field is an
* array, because we'll allow our Markdown to have subclasses. Each item in
* this array will be an instance of a specific Markdown's subclass.
*
* @var array
*/
private $markdown;
private static $instances = [];
/**
* Markdown facade
*/
private $markdownFacade = null;
/**
* Constructor
*
* @access public
* Markdown should not be cloneable.
*/
public function __construct($markdown)
protected function __clone()
{
$this->markdown = $markdown;
throw new Exception('Cannot clone a Markdown.');
}
/**
* Get Markdown instance
*
* @access public
* Markdown should not be restorable from strings.
*/
public function getInstance()
public function __wakeup(): void
{
return $this->markdown;
throw new Exception('Cannot unserialize a Markdown.');
}
/**
* Markdown construct
*
* @param
*/
protected function __construct()
{
$this->markdownFacade = new ParsedownExtra();
}
/**
* Markdown facade
*
* @param
*/
public function facade(): ParsedownExtra
{
return $this->markdownFacade;
}
/**
* Returns Markdown Instance
*
* @param
*/
public static function getInstance(): Markdown
{
$cls = static::class;
if (! isset(self::$instances[$cls])) {
self::$instances[$cls] = new static();
}
return self::$instances[$cls];
}
/**
@@ -56,23 +98,24 @@ class Markdown
return $dataFromCache;
}
$data = $this->_parse($input);
$data = $this->facade()->text($input);
flextype('cache')->set($key, $data);
return $data;
}
return $this->_parse($input);
return $this->facade()->text($input);
}
/**
* @see parse()
* Get Cache ID for markdown.
*
* @param string $input Input.
*
* @return string Cache ID.
*
* @access public
*/
protected function _parse(string $input): string
{
return $this->markdown->text($input);
}
public function getCacheID($input): string
{
return strings('markdown' . $input)->hash()->toString();

View File

@@ -13,7 +13,6 @@ use Atomastic\Macroable\Macroable;
use Flextype\Support\Parsers\Markdown;
use Flextype\Support\Parsers\Shortcode;
use ParsedownExtra;
class Parsers
{
@@ -24,13 +23,13 @@ class Parsers
*/
public function markdown(): Markdown
{
return new Markdown(new ParsedownExtra());
return Markdown::getInstance();
}
/**
* Create a Shortcode instance.
*/
public function shortcode()
public function shortcode(): Shortcode
{
return Shortcode::getInstance();
}

View File

@@ -36,7 +36,7 @@ class Shortcode
*/
protected function __clone()
{
throw new Exception('Cannot clone a Flextype.');
throw new Exception('Cannot clone a Shortcode.');
}
/**
@@ -44,7 +44,7 @@ class Shortcode
*/
public function __wakeup(): void
{
throw new Exception('Cannot unserialize a Flextype.');
throw new Exception('Cannot unserialize a Shortcode.');
}
/**