From 5b07b5489799833240b5e1c7c5d73d8a905dac10 Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 27 Jun 2019 02:32:08 +0300 Subject: [PATCH] Flextype Core: Twig Extensions #165 - total refactoring for all core extensions! --- flextype/twig/AssetsTwigExtension.php | 20 +++++++---- flextype/twig/CsrfTwigExtension.php | 8 ++++- flextype/twig/EmitterTwigExtension.php | 41 ++++++++++++++++++----- flextype/twig/EntriesTwigExtension.php | 33 ++++++++++++++---- flextype/twig/FlashTwigExtension.php | 2 +- flextype/twig/GlobalVarsTwigExtension.php | 3 ++ flextype/twig/I18nTwigExtension.php | 5 ++- flextype/twig/JsonParserTwigExtension.php | 8 ++++- flextype/twig/ShortcodesTwigExtension.php | 5 ++- flextype/twig/SnippetsTwigExtension.php | 31 +++++++++++++---- 10 files changed, 122 insertions(+), 34 deletions(-) diff --git a/flextype/twig/AssetsTwigExtension.php b/flextype/twig/AssetsTwigExtension.php index 66f08aef..526078fc 100644 --- a/flextype/twig/AssetsTwigExtension.php +++ b/flextype/twig/AssetsTwigExtension.php @@ -14,26 +14,32 @@ namespace Flextype; use Flextype\Component\Assets\Assets; -class AssetsTwigExtension extends \Twig_Extension +class AssetsTwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { /** - * Callback for twig. - * - * @return array + * Register Global variables in an extension */ - public function getFunctions() + public function getGlobals() { return [ - new \Twig_SimpleFunction('assets_add', [$this, 'add']), - new \Twig_SimpleFunction('assets_get', [$this, 'get']), + 'assets' => new AssetsTwig() ]; } +} +class AssetsTwig +{ + /** + * Add Asset + */ public function add(string $asset_type, string $asset, string $namespace, int $priority = 1) : void { Assets::add($asset_type, $asset, $namespace, $priority); } + /** + * Get Asset + */ public function get(string $asset_type, string $namespace) : array { return Assets::get($asset_type, $namespace); diff --git a/flextype/twig/CsrfTwigExtension.php b/flextype/twig/CsrfTwigExtension.php index 62367b6b..99984041 100644 --- a/flextype/twig/CsrfTwigExtension.php +++ b/flextype/twig/CsrfTwigExtension.php @@ -20,11 +20,17 @@ class CsrfTwigExtension extends \Twig_Extension implements \Twig_Extension_Globa */ protected $csrf; + /** + * Constructor + */ public function __construct(\Slim\Csrf\Guard $csrf) { $this->csrf = $csrf; } + /** + * Register Global variables in an extension + */ public function getGlobals() { // CSRF token name and value @@ -51,7 +57,7 @@ class CsrfTwigExtension extends \Twig_Extension implements \Twig_Extension_Globa } /** - * Callback for twig. + * Returns a list of functions to add to the existing list. * * @return array */ diff --git a/flextype/twig/EmitterTwigExtension.php b/flextype/twig/EmitterTwigExtension.php index a4163821..92e985db 100644 --- a/flextype/twig/EmitterTwigExtension.php +++ b/flextype/twig/EmitterTwigExtension.php @@ -12,7 +12,7 @@ namespace Flextype; -class EmitterTwigExtension extends \Twig_Extension +class EmitterTwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { /** * Flextype Dependency Container @@ -28,19 +28,44 @@ class EmitterTwigExtension extends \Twig_Extension } /** - * Callback for twig. - * - * @return array + * Register Global variables in an extension */ - public function getFunctions() + public function getGlobals() { return [ - new \Twig_SimpleFunction('emitter_emit', [$this, 'emit']), + 'emmiter' => new EmitterTwig($this->flextype) ]; } +} - public function emit(string $event) +class EmitterTwig +{ + /** + * Flextype Dependency Container + */ + private $flextype; + + /** + * Constructor + */ + public function __construct($flextype) { - $this->flextype['emitter']->emit($event); + $this->flextype = $flextype; + } + + /** + * Emitting event + */ + public function emmit($event) + { + return $this->flextype['emitter']->emit($event); + } + + /** + * Emitting events in batches + */ + public function emitBatch(array $events) + { + return $this->flextype['emitter']->emitBatch($events); } } diff --git a/flextype/twig/EntriesTwigExtension.php b/flextype/twig/EntriesTwigExtension.php index 821e41cc..d2c9aac4 100644 --- a/flextype/twig/EntriesTwigExtension.php +++ b/flextype/twig/EntriesTwigExtension.php @@ -12,7 +12,7 @@ namespace Flextype; -class EntriesTwigExtension extends \Twig_Extension +class EntriesTwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { /** * Flextype Dependency Container @@ -28,23 +28,42 @@ class EntriesTwigExtension extends \Twig_Extension } /** - * Callback for twig. - * - * @return array + * Register Global variables in an extension */ - public function getFunctions() + public function getGlobals() { return [ - new \Twig_SimpleFunction('entries_fetch', [$this, 'fetch']), - new \Twig_SimpleFunction('entries_fetch_all', [$this, 'fetchAll']), + 'entries' => new EntriesTwig($this->flextype) ]; } +} +class EntriesTwig +{ + /** + * Flextype Dependency Container + */ + private $flextype; + + /** + * Constructor + */ + public function __construct($flextype) + { + $this->flextype = $flextype; + } + + /** + * Fetch single entry + */ public function fetch(string $entry) { return $this->flextype['entries']->fetch($entry); } + /** + * Fetch all entries + */ public function fetchAll(string $entry, string $order_by = 'date', string $order_type = 'DESC', int $offset = null, int $length = null, bool $recursive = false) : array { return $this->flextype['entries']->fetchAll($entry, $order_by, $order_type, $offset, $length, $recursive); diff --git a/flextype/twig/FlashTwigExtension.php b/flextype/twig/FlashTwigExtension.php index 9ea53bef..f60b0973 100644 --- a/flextype/twig/FlashTwigExtension.php +++ b/flextype/twig/FlashTwigExtension.php @@ -30,7 +30,7 @@ class FlashTwigExtension extends \Twig_Extension } /** - * Callback for twig. + * Returns a list of functions to add to the existing list. * * @return array */ diff --git a/flextype/twig/GlobalVarsTwigExtension.php b/flextype/twig/GlobalVarsTwigExtension.php index 7386a5e1..0a341f2f 100644 --- a/flextype/twig/GlobalVarsTwigExtension.php +++ b/flextype/twig/GlobalVarsTwigExtension.php @@ -27,6 +27,9 @@ class GlobalVarsTwigExtension extends \Twig_Extension implements \Twig_Extension $this->flextype = $flextype; } + /** + * Register Global variables in an extension + */ public function getGlobals() { return [ diff --git a/flextype/twig/I18nTwigExtension.php b/flextype/twig/I18nTwigExtension.php index 65a5abec..ee394944 100644 --- a/flextype/twig/I18nTwigExtension.php +++ b/flextype/twig/I18nTwigExtension.php @@ -17,7 +17,7 @@ use Flextype\Component\I18n\I18n; class I18nTwigExtension extends \Twig_Extension { /** - * Callback for twig. + * Returns a list of functions to add to the existing list. * * @return array */ @@ -28,6 +28,9 @@ class I18nTwigExtension extends \Twig_Extension ]; } + /** + * Translate string + */ public function tr(string $translate, string $locale = null, array $values = []) : string { return I18n::find($translate, $locale, $values); diff --git a/flextype/twig/JsonParserTwigExtension.php b/flextype/twig/JsonParserTwigExtension.php index a9cfe3bd..27449f59 100644 --- a/flextype/twig/JsonParserTwigExtension.php +++ b/flextype/twig/JsonParserTwigExtension.php @@ -15,7 +15,7 @@ namespace Flextype; class JsonParserTwigExtension extends \Twig_Extension { /** - * Callback for twig. + * Returns a list of functions to add to the existing list. * * @return array */ @@ -27,11 +27,17 @@ class JsonParserTwigExtension extends \Twig_Extension ]; } + /** + * Encode JSON + */ public function encode($input, int $encode_options = 0, int $encode_depth = 512) : string { return JsonParser::encode($input, $encode_options, $encode_depth); } + /** + * Decode JSON + */ public function decode(string $input, bool $decode_assoc = true, int $decode_depth = 512, int $decode_options = 0) { return JsonParser::decode($input, $decode_assoc, $decode_depth, $decode_options); diff --git a/flextype/twig/ShortcodesTwigExtension.php b/flextype/twig/ShortcodesTwigExtension.php index f50e353c..b2d3c099 100644 --- a/flextype/twig/ShortcodesTwigExtension.php +++ b/flextype/twig/ShortcodesTwigExtension.php @@ -29,7 +29,7 @@ class ShortcodesTwigExtension extends \Twig_Extension } /** - * Callback for twig. + * Returns a list of filters to add to the existing list. * * @return array */ @@ -40,6 +40,9 @@ class ShortcodesTwigExtension extends \Twig_Extension ]; } + /** + * Shorcode process + */ public function shortcode(string $value) : string { return $this->flextype->shortcodes->process($value); diff --git a/flextype/twig/SnippetsTwigExtension.php b/flextype/twig/SnippetsTwigExtension.php index 4ef42fa1..0e27890b 100644 --- a/flextype/twig/SnippetsTwigExtension.php +++ b/flextype/twig/SnippetsTwigExtension.php @@ -12,7 +12,7 @@ namespace Flextype; -class SnippetsTwigExtension extends \Twig_Extension +class SnippetsTwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { /** * Flextype Dependency Container @@ -28,18 +28,35 @@ class SnippetsTwigExtension extends \Twig_Extension } /** - * Callback for twig. - * - * @return array + * Register Global variables in an extension */ - public function getFunctions() + public function getGlobals() { return [ - new \Twig_SimpleFunction('snippets_exec', [$this, 'exec']) + 'snippets' => new SnippetsTwig($this->flextype) ]; } +} - public function exec(string $id) +class SnippetsTwig +{ + /** + * Flextype Dependency Container + */ + private $flextype; + + /** + * Constructor + */ + public function __construct($flextype) + { + $this->flextype = $flextype; + } + + /** + * Execute snippet + */ + public function exec($id) { return $this->flextype['snippets']->exec($id); }