1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-18 19:01:40 +02:00

Flextype Core: Twig Extensions #165

- total refactoring for all core extensions!
This commit is contained in:
Awilum
2019-06-27 02:32:08 +03:00
parent d72a544a01
commit 5b07b54897
10 changed files with 122 additions and 34 deletions

View File

@@ -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);

View File

@@ -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
*/

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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
*/

View File

@@ -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 [

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);
}