diff --git a/src/flextype/Foundation/Entries/Fields/ParsersField.php b/src/flextype/Foundation/Entries/Fields/ParsersField.php index 4beb0385..fecf4840 100644 --- a/src/flextype/Foundation/Entries/Fields/ParsersField.php +++ b/src/flextype/Foundation/Entries/Fields/ParsersField.php @@ -32,13 +32,13 @@ function processParsersField(): void if ($parserName == 'markdown') { if (arrays(flextype('entries')->getStorage('fetch.data'))->has($field)) { flextype('entries')->setStorage('fetch.data.'.$field, - flextype('markdown')->parse(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); + flextype('parsers')->markdown()->parse(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); } } if ($parserName == 'shortcode') { if (arrays(flextype('entries')->getStorage('fetch.data'))->has($field)) { flextype('entries')->setStorage('fetch.data.'.$field, - flextype('shortcode')->process(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); + flextype('parsers')->shortcode()->process(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); } } } diff --git a/src/flextype/Support/Parsers/Parsers.php b/src/flextype/Support/Parsers/Parsers.php new file mode 100644 index 00000000..5f8a30de --- /dev/null +++ b/src/flextype/Support/Parsers/Parsers.php @@ -0,0 +1,37 @@ +shortcode = $shortcode; + throw new Exception('Cannot clone a Flextype.'); } /** - * Get Shortcode instance - * - * @access public + * Shortcode should not be restorable from strings. */ - public function getInstance() + public function __wakeup(): void { - return $this->shortcode; + throw new Exception('Cannot unserialize a Flextype.'); + } + + /** + * Shortcode construct + * + * @param + */ + protected function __construct() + { + return new ShortcodeFacade(); + } + + /** + * Returns Shortcode Instance + * + * @param + */ + public static function getInstance(): Shortcode + { + $cls = static::class; + if (! isset(self::$instances[$cls])) { + self::$instances[$cls] = new static(); + } + + return self::$instances[$cls]; } /** @@ -49,7 +77,7 @@ class Shortcode */ public function addHandler(string $name, callable $handler) { - return $this->shortcode->addHandler($name, $handler); + return $this->addHandler($name, $handler); } /** @@ -62,7 +90,7 @@ class Shortcode */ public function addEventHandler(string $name, callable $handler) { - return $this->shortcode->addEventHandler($name, $handler); + return $this->addEventHandler($name, $handler); } /** @@ -74,7 +102,7 @@ class Shortcode */ public function parse(string $input) { - return $this->shortcode->parse($input); + return $this->parse($input); } /** @@ -94,13 +122,13 @@ class Shortcode return $dataFromCache; } - $data = $this->shortcode->process($input); + $data = $this->process($input); flextype('cache')->set($key, $data); return $data; } - return $this->shortcode->process($input); + return $this->process($input); } /** diff --git a/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php b/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php index 8fd886b0..709d4882 100644 --- a/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php @@ -11,7 +11,7 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"] if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.entries.enabled')) { - flextype('shortcode')->addHandler('entries_fetch', static function (ShortcodeInterface $s) { + flextype('parsers')->shortcode()->addHandler('entries_fetch', static function (ShortcodeInterface $s) { return arrays(flextype('entries')->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); }); } diff --git a/src/flextype/Support/Parsers/Shortcodes/RawShortcode.php b/src/flextype/Support/Parsers/Shortcodes/RawShortcode.php index 6b6d6cdc..8f79e72e 100644 --- a/src/flextype/Support/Parsers/Shortcodes/RawShortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes/RawShortcode.php @@ -13,9 +13,9 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [raw] if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.raw.enabled')) { - flextype('shortcode')->addHandler('raw', static function (ShortcodeInterface $s) { + flextype('parsers')->shortcode()->addHandler('raw', static function (ShortcodeInterface $s) { return $s->getContent(); }); - flextype('shortcode')->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['raw'])); + flextype('parsers')->shortcode()->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['raw'])); } diff --git a/src/flextype/Support/Parsers/Shortcodes/RegistryShortcode.php b/src/flextype/Support/Parsers/Shortcodes/RegistryShortcode.php index f3756472..0d4cc169 100644 --- a/src/flextype/Support/Parsers/Shortcodes/RegistryShortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes/RegistryShortcode.php @@ -11,7 +11,7 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [registry_get name="item-name" default="default-value"] if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.registry.enabled')) { - flextype('shortcode')->addHandler('registry_get', static function (ShortcodeInterface $s) { + flextype('parsers')->shortcode()->addHandler('registry_get', static function (ShortcodeInterface $s) { return flextype('registry')->get($s->getParameter('name'), $s->getParameter('default')); }); } diff --git a/src/flextype/Support/Parsers/Shortcodes/UrlShortcode.php b/src/flextype/Support/Parsers/Shortcodes/UrlShortcode.php index 5a5e2b7e..8f920d5a 100644 --- a/src/flextype/Support/Parsers/Shortcodes/UrlShortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes/UrlShortcode.php @@ -12,7 +12,7 @@ use Slim\Http\Uri; // Shortcode: [url] if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.url.enabled')) { - flextype('shortcode')->addHandler('url', static function () { + flextype('parsers')->shortcode()->addHandler('url', static function () { if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') { return flextype('registry')->get('flextype.settings.url'); } diff --git a/src/flextype/dependencies.php b/src/flextype/dependencies.php index 0389a5d9..016ba5a8 100644 --- a/src/flextype/dependencies.php +++ b/src/flextype/dependencies.php @@ -14,16 +14,10 @@ use Bnf\Slim3Psr15\CallableResolver; use Cocur\Slugify\Slugify; use Flextype\Foundation\Cors; use Flextype\Foundation\Entries\Entries; -use Flextype\Foundation\Media\MediaFiles; -use Flextype\Foundation\Media\MediaFilesMeta; -use Flextype\Foundation\Media\MediaFolders; -use Flextype\Foundation\Media\MediaFoldersMeta; +use Flextype\Foundation\Media\Media; use Flextype\Foundation\Plugins; -use Flextype\Support\Parsers\Markdown; -use Flextype\Support\Parsers\Shortcode; -use Flextype\Support\Serializers\Frontmatter; -use Flextype\Support\Serializers\Json; -use Flextype\Support\Serializers\Yaml; +use Flextype\Support\Parsers\Parsers; +use Flextype\Support\Serializers\Serializers; use Intervention\Image\ImageManager; use League\Event\Emitter; use League\Flysystem\Adapter\Local; @@ -47,10 +41,8 @@ use League\Glide\Responses\SlimResponseFactory; use League\Glide\ServerFactory; use Monolog\Handler\StreamHandler; use Monolog\Logger; -use ParsedownExtra; use Phpfastcache\Drivers\Apcu\Config; use Phpfastcache\Helper\Psr16Adapter as Cache; -use Thunder\Shortcode\ShortcodeFacade; use function date; use function extension_loaded; @@ -222,42 +214,21 @@ flextype()->container()['cache'] = static function () { }; /** - * Add shortcode parser service to Flextype container + * Add parsers service to Flextype container */ -flextype()->container()['shortcode'] = static function () { - return new Shortcode(new ShortcodeFacade()); +flextype()->container()['parsers'] = static function () { + return new Parsers(); }; -/** - * Add markdown parser service to Flextype container - */ -flextype()->container()['markdown'] = static function () { - return new Markdown(new ParsedownExtra()); -}; - -flextype('markdown')->getInstance()->setBreaksEnabled(flextype('registry')->get('flextype.settings.markdown.auto_line_breaks')); -flextype('markdown')->getInstance()->setUrlsLinked(flextype('registry')->get('flextype.settings.markdown.auto_url_links')); -flextype('markdown')->getInstance()->setMarkupEscaped(flextype('registry')->get('flextype.settings.markdown.escape_markup')); +//flextype('parsers')->markdown()->getInstance()->setBreaksEnabled(flextype('registry')->get('flextype.settings.markdown.auto_line_breaks')); +//flextype('parsers')->markdown()->getInstance()->setUrlsLinked(flextype('registry')->get('flextype.settings.markdown.auto_url_links')); +//flextype('parsers')->markdown()->getInstance()->setMarkupEscaped(flextype('registry')->get('flextype.settings.markdown.escape_markup')); /** - * Add json serializer service to Flextype container + * Add serializer service to Flextype container */ -flextype()->container()['json'] = static function () { - return new Json(); -}; - -/** - * Add yaml serializer service to Flextype container - */ -flextype()->container()['yaml'] = static function () { - return new Yaml(); -}; - -/** - * Add frontmatter serializer service to Flextype container - */ -flextype()->container()['frontmatter'] = static function () { - return new Frontmatter(); +flextype()->container()['serializers'] = static function () { + return new Serializers(); }; /** @@ -323,31 +294,10 @@ flextype()->container()['entries'] = static function () { }; /** - * Add media folders service to Flextype container + * Add media service to Flextype container */ -flextype()->container()['media.folders'] = static function () { - return new MediaFolders(); -}; - -/** - * Add media files service to Flextype container - */ -flextype()->container()['media.files'] = static function () { - return new MediaFiles(); -}; - -/** - * Add media folders meta service to Flextype container - */ -flextype()->container()['media.folders.meta'] = static function () { - return new MediaFoldersMeta(); -}; - -/** - * Add media files meta service to Flextype container - */ -flextype()->container()['media.files.meta'] = static function () { - return new MediaFilesMeta(); +flextype()->container()['media'] = static function () { + return new Media(); }; /** diff --git a/tests/Support/Parsers/ShortcodeTest.php b/tests/Support/Parsers/ShortcodeTest.php index 21ac5abe..a0408d5f 100644 --- a/tests/Support/Parsers/ShortcodeTest.php +++ b/tests/Support/Parsers/ShortcodeTest.php @@ -8,34 +8,34 @@ use Thunder\Shortcode\Events; use Thunder\Shortcode\Shortcode\ShortcodeInterface; test('test getInstance() method', function () { - $this->assertInstanceOf(ShortcodeFacade::class, flextype('shortcode')->getInstance()); + $this->assertInstanceOf(ShortcodeFacade::class, flextype('parsers')->shortcode()->getInstance()); }); test('test addHandler() method', function () { - $this->assertInstanceOf(ShortcodeFacade::class, flextype('shortcode')->addHandler('foo', static function() { return ''; })); + $this->assertInstanceOf(ShortcodeFacade::class, flextype('parsers')->shortcode()->addHandler('foo', static function() { return ''; })); }); test('test addEventHandler() method', function () { - flextype('shortcode')->addHandler('barz', static function () { + flextype('parsers')->shortcode()->addHandler('barz', static function () { return 'Barz'; }); - flextype('shortcode')->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['barz'])); - $this->assertEquals('Barz', flextype('shortcode')->process('[barz]')); + flextype('parsers')->shortcode()->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['barz'])); + $this->assertEquals('Barz', flextype('parsers')->shortcode()->process('[barz]')); }); test('test parse() method', function () { - $this->assertInstanceOf(ShortcodeFacade::class, flextype('shortcode')->addHandler('bar', static function() { return ''; })); - $this->assertTrue(is_array(flextype('shortcode')->parse('[bar]'))); - $this->assertTrue(is_object(flextype('shortcode')->parse('[bar]')[0])); + $this->assertInstanceOf(ShortcodeFacade::class, flextype('parsers')->shortcode()->addHandler('bar', static function() { return ''; })); + $this->assertTrue(is_array(flextype('parsers')->shortcode()->parse('[bar]'))); + $this->assertTrue(is_object(flextype('parsers')->shortcode()->parse('[bar]')[0])); }); test('test process() method', function () { - $this->assertInstanceOf(ShortcodeFacade::class, flextype('shortcode')->addHandler('zed', static function() { return 'Zed'; })); - $this->assertEquals('Zed', flextype('shortcode')->process('[zed]')); - $this->assertEquals('fòôBàřZed', flextype('shortcode')->process('fòôBàř[zed]')); + $this->assertInstanceOf(ShortcodeFacade::class, flextype('parsers')->shortcode()->addHandler('zed', static function() { return 'Zed'; })); + $this->assertEquals('Zed', flextype('parsers')->shortcode()->process('[zed]')); + $this->assertEquals('fòôBàřZed', flextype('parsers')->shortcode()->process('fòôBàř[zed]')); }); test('test getCacheID() method', function () { - $this->assertNotEquals(flextype('shortcode')->getCacheID('fòôBàř[bar]'), - flextype('shortcode')->getCacheID('fòôBàř[foo]')); + $this->assertNotEquals(flextype('parsers')->shortcode()->getCacheID('fòôBàř[bar]'), + flextype('parsers')->shortcode()->getCacheID('fòôBàř[foo]')); }); diff --git a/tests/Support/Parsers/Shortcodes/EntriesShortcodeTest.php b/tests/Support/Parsers/Shortcodes/EntriesShortcodeTest.php index 8f52c8db..1a9e4eb3 100644 --- a/tests/Support/Parsers/Shortcodes/EntriesShortcodeTest.php +++ b/tests/Support/Parsers/Shortcodes/EntriesShortcodeTest.php @@ -12,6 +12,6 @@ afterEach(function (): void { test('test entries_fetch shortcode', function () { $this->assertTrue(flextype('entries')->create('foo', ['title' => 'Foo'])); - $this->assertEquals('Foo', flextype('shortcode')->process('[entries_fetch id="foo" field="title"]')); - $this->assertEquals('Bar', flextype('shortcode')->process('[entries_fetch id="foo" field="bar" default="Bar"]')); + $this->assertEquals('Foo', flextype('parsers')->shortcode()->process('[entries_fetch id="foo" field="title"]')); + $this->assertEquals('Bar', flextype('parsers')->shortcode()->process('[entries_fetch id="foo" field="bar" default="Bar"]')); }); diff --git a/tests/Support/Parsers/Shortcodes/RawShortcodeTest.php b/tests/Support/Parsers/Shortcodes/RawShortcodeTest.php index e74f8654..924b7bf3 100644 --- a/tests/Support/Parsers/Shortcodes/RawShortcodeTest.php +++ b/tests/Support/Parsers/Shortcodes/RawShortcodeTest.php @@ -13,5 +13,5 @@ afterEach(function (): void { test('test raw shortcode', function () { $this->assertTrue(flextype('entries')->create('foo', ['title' => 'Foo'])); $this->assertEquals('[entries_fetch id="foo" field="title"]', - flextype('shortcode')->process('[raw][entries_fetch id="foo" field="title"][/raw]')); + flextype('parsers')->shortcode()->process('[raw][entries_fetch id="foo" field="title"][/raw]')); }); diff --git a/tests/Support/Parsers/Shortcodes/RegistryShortcodeTest.php b/tests/Support/Parsers/Shortcodes/RegistryShortcodeTest.php index 13e9b213..e8810acd 100644 --- a/tests/Support/Parsers/Shortcodes/RegistryShortcodeTest.php +++ b/tests/Support/Parsers/Shortcodes/RegistryShortcodeTest.php @@ -4,7 +4,7 @@ declare(strict_types=1); test('test registry_get shortcode', function () { $this->assertEquals('Flextype', - flextype('shortcode')->process('[registry_get name="flextype.manifest.name"]')); + flextype('parsers')->shortcode()->process('[registry_get name="flextype.manifest.name"]')); $this->assertEquals('default-value', - flextype('shortcode')->process('[registry_get name="item-name" default="default-value"]')); + flextype('parsers')->shortcode()->process('[registry_get name="item-name" default="default-value"]')); }); diff --git a/tests/Support/Parsers/Shortcodes/UrlShortcodeTest.php b/tests/Support/Parsers/Shortcodes/UrlShortcodeTest.php index ba1f1e41..bfab3fde 100644 --- a/tests/Support/Parsers/Shortcodes/UrlShortcodeTest.php +++ b/tests/Support/Parsers/Shortcodes/UrlShortcodeTest.php @@ -3,8 +3,8 @@ declare(strict_types=1); test('test registry_get shortcode', function () { - $this->assertStringContainsString('http', flextype('shortcode')->process('[url]')); + $this->assertStringContainsString('http', flextype('parsers')->shortcode()->process('[url]')); flextype('registry')->set('flextype.settings.url', 'https://flextype.org'); - $this->assertStringContainsString('https://flextype.org', flextype('shortcode')->process('[url]')); + $this->assertStringContainsString('https://flextype.org', flextype('parsers')->shortcode()->process('[url]')); });