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

refactor(core): remove twig from the core #414

This commit is contained in:
Awilum
2020-03-15 11:44:35 +03:00
parent 61ee361e55
commit 0384d0ae22
20 changed files with 1 additions and 1122 deletions

View File

@@ -8,7 +8,7 @@
"authors": [
{
"name": "Sergey Romanenko",
"email": "hello@romanenko.digital",
"email": "support@flextype.org",
"homepage": "http://digital.flextype.org"
}
],
@@ -38,7 +38,6 @@
"flextype-components/text" : "1.1.2",
"slim/slim": "~3.12.3",
"slim/twig-view": "~2.5.0",
"slim/flash": "~0.4.0",
"slim/csrf": "~0.8.3",

View File

@@ -104,12 +104,6 @@ $app = new App([
'outputBuffering' => $registry->get('flextype.output_buffering'),
'responseChunkSize' => $registry->get('flextype.response_chunk_size'),
'httpVersion' => $registry->get('flextype.http_version'),
'twig' => [
'charset' => $registry->get('flextype.twig.charset'),
'debug' => $registry->get('flextype.twig.debug'),
'cache' => $registry->get('flextype.twig.cache') ? PATH['cache'] . '/twig' : false,
'auto_reload' => $registry->get('flextype.twig.auto_reload'),
],
'images' => [
'driver' => $registry->get('flextype.image.driver'),
],

View File

@@ -38,10 +38,7 @@ use Slim\Csrf\Guard;
use Slim\Flash\Messages;
use Slim\Http\Environment;
use Slim\Http\Uri;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;
use Thunder\Shortcode\ShortcodeFacade;
use Twig\Extension\DebugExtension;
use function date;
use function ucfirst;
use function extension_loaded;
@@ -216,42 +213,6 @@ $flextype['entries'] = static function ($container) {
return new Entries($container);
};
/**
* Add view service to Flextype container
*/
$flextype['view'] = static function ($container) {
// Get twig settings
$twigSettings = $container->get('settings')['twig'];
// Create Twig View
$view = new Twig(PATH['site'], $twigSettings);
// Instantiate
$router = $container->get('router');
$uri = Uri::createFromEnvironment(new Environment($_SERVER));
// Add Twig Extension
$view->addExtension(new TwigExtension($router, $uri));
// Add Twig Debug Extension
$view->addExtension(new DebugExtension());
// Load Flextype Twig extensions from directory /flextype/twig/ based on settings.twig.extensions array
$twig_extensions = $container['registry']->get('flextype.twig.extensions');
foreach ($twig_extensions as $twig_extension) {
$twig_extension_class_name = $twig_extension . 'TwigExtension';
$twig_extension_class_name_with_namespace = 'Flextype\\' . $twig_extension . 'TwigExtension';
if (file_exists(ROOT_DIR . '/flextype/twig/' . $twig_extension_class_name . '.php')) {
$view->addExtension(new $twig_extension_class_name_with_namespace($container));
}
}
// Return view
return $view;
};
/**
* Add themes service to Flextype container
*/

View File

@@ -1,46 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Assets\Assets;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
class AssetsTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
return [
'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

@@ -1,39 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
class CacheTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
return [
'cache' => $this->flextype['cache'],
];
}
}

View File

@@ -1,79 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
use Twig_SimpleFunction;
class CsrfTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
// CSRF token name and value
$csrfNameKey = $this->flextype->csrf->getTokenNameKey();
$csrfValueKey = $this->flextype->csrf->getTokenValueKey();
$csrfName = $this->flextype->csrf->getTokenName();
$csrfValue = $this->flextype->csrf->getTokenValue();
return [
'csrf' => [
'keys' => [
'name' => $csrfNameKey,
'value' => $csrfValueKey,
],
'name' => $csrfName,
'value' => $csrfValue,
],
];
}
public function getName()
{
return 'slim/csrf';
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('csrf', [$this, 'csrf'], ['is_safe' => ['html']]),
];
}
/**
* CSRF
*/
public function csrf() : string
{
return '<input type="hidden" name="' . $this->flextype->csrf->getTokenNameKey() . '" value="' . $this->flextype->csrf->getTokenName() . '">' .
'<input type="hidden" name="' . $this->flextype->csrf->getTokenValueKey() . '" value="' . $this->flextype->csrf->getTokenValue() . '">';
}
}

View File

@@ -1,108 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFunction;
use function strlen;
class DateTwigExtension extends Twig_Extension
{
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('dateformatToMomentJS', [$this, 'dateformatToMomentJS']),
];
}
public function dateformatToMomentJS($php_format)
{
$symbols_matching = [
// Day
'd' => 'DD',
'D' => 'ddd',
'j' => 'D',
'l' => 'dddd',
'N' => 'E',
'S' => 'Do',
'w' => 'd',
'z' => 'DDD',
// Week
'W' => 'W',
// Month
'F' => 'MMMM',
'm' => 'MM',
'M' => 'MMM',
'n' => 'M',
't' => '',
// Year
'L' => '',
'o' => 'GGGG',
'Y' => 'YYYY',
'y' => 'yy',
// Time
'a' => 'a',
'A' => 'A',
'B' => 'SSS',
'g' => 'h',
'G' => 'H',
'h' => 'hh',
'H' => 'HH',
'i' => 'mm',
's' => 'ss',
'u' => '',
// Timezone
'e' => '',
'I' => '',
'O' => 'ZZ',
'P' => 'Z',
'T' => 'z',
'Z' => '',
// Full Date/Time
'c' => '',
'r' => 'llll ZZ',
'U' => 'X',
];
$js_format = '';
$escaping = false;
$len = strlen($php_format);
for ($i = 0; $i < $len; $i++) {
$char = $php_format[$i];
if ($char === '\\') { // PHP date format escaping character
$i++;
if ($escaping) {
$js_format .= $php_format[$i];
} else {
$js_format .= '\'' . $php_format[$i];
}
$escaping = true;
} else {
if ($escaping) {
$js_format .= "'";
$escaping = false;
}
if (isset($symbols_matching[$char])) {
$js_format .= $symbols_matching[$char];
} else {
$js_format .= $char;
}
}
}
return $js_format;
}
}

View File

@@ -1,71 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
class EmitterTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
return [
'emitter' => new EmitterTwig($this->flextype),
];
}
}
class EmitterTwig
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Emitting event
*/
public function emit($event)
{
return $this->flextype['emitter']->emit($event);
}
/**
* Emitting events in batches
*/
public function emitBatch(array $events)
{
return $this->flextype['emitter']->emitBatch($events);
}
}

View File

@@ -1,79 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
class EntriesTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
return [
'entries' => new EntriesTwig($this->flextype),
];
}
}
class EntriesTwig
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Fetch entry(entries)
*/
public function fetch(string $id, $args = null) : array
{
return $this->flextype['entries']->fetch($id, $args);
}
/**
* Fetch single entry
*/
public function fetchSingle(string $id) : array
{
return $this->flextype['entries']->fetch($id);
}
/**
* Fetch entries collection
*/
public function fetchCollection(string $id, array $args = []) : array
{
return $this->flextype['entries']->fetch($id, $args);
}
}

View File

@@ -1,61 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Twig_Extension;
use Twig_SimpleFunction;
use function basename;
use function strrchr;
use function substr;
class FilesystemTwigExtension extends Twig_Extension
{
/**
* Callback for twig.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('filesystem_list_contents', [$this, 'list_contents']),
new Twig_SimpleFunction('filesystem_has', [$this, 'has']),
new Twig_SimpleFunction('filesystem_read', [$this, 'read']),
new Twig_SimpleFunction('filesystem_ext', [$this, 'ext']),
new Twig_SimpleFunction('filesystem_basename', [$this, 'basename']),
];
}
public function list_contents(string $directory = '', bool $recursive = false)
{
return Filesystem::listContents($directory, $recursive);
}
public function has($path)
{
return Filesystem::has($path);
}
public function read($path)
{
return Filesystem::read($path);
}
public function ext($file)
{
return substr(strrchr($file, '.'), 1);
}
public function basename($value, $suffix = '')
{
return basename($value, $suffix);
}
}

View File

@@ -1,56 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFunction;
class FlashTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('flash', [$this, 'getMessages']),
];
}
/**
* Returns Flash messages; If key is provided then returns messages
* for that key.
*
* @return array
*/
public function getMessages(?string $key = null) : array
{
if ($key !== null) {
return $this->flextype['flash']->getMessage($key);
}
return $this->flextype['flash']->getMessages();
}
}

View File

@@ -1,55 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
use const PHP_VERSION;
class GlobalVarsTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
return [
'PATH_SITE' => PATH['site'],
'PATH_PLUGINS' => PATH['plugins'],
'PATH_ACCOUNTS' => PATH['accounts'],
'PATH_UPLOADS' => PATH['uploads'],
'PATH_TOKENS' => PATH['tokens'],
'PATH_THEMES' => PATH['themes'],
'PATH_ENTRIES' => PATH['entries'],
'PATH_SNIPPETS' => PATH['snippets'],
'PATH_FIELDSETS' => PATH['fieldsets'],
'PATH_CONFIG_DEFAULT' => PATH['config']['default'],
'PATH_CONFIG_SITE' => PATH['config']['site'],
'PATH_CACHE' => PATH['cache'],
'PATH_LOGS' => PATH['logs'],
'FLEXTYPE_VERSION' => FLEXTYPE_VERSION,
'PHP_VERSION' => PHP_VERSION,
'registry' => $this->flextype['registry']->dump(),
];
}
}

View File

@@ -1,50 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\I18n\I18n;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
class I18nTwigExtension extends Twig_Extension
{
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('tr', [$this, 'tr']),
];
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array
*/
public function getFilters() : array
{
return [
new Twig_SimpleFilter('tr', [$this, 'tr']),
];
}
/**
* Translate string
*/
public function tr(string $translate, array $values = [], ?string $locale = null) : string
{
return I18n::find($translate, $values, $locale);
}
}

View File

@@ -1,72 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
class JsonTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('json_decode', [$this, 'decode']),
new Twig_SimpleFunction('json_encode', [$this, 'encode']),
];
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array
*/
public function getFilters() : array
{
return [
new Twig_SimpleFilter('json_decode', [$this, 'decode']),
new Twig_SimpleFilter('json_encode', [$this, 'encode']),
];
}
/**
* Encode JSON
*/
public function encode($input) : string
{
return $this->flextype['parser']->encode($input, 'json');
}
/**
* Decode JSON
*/
public function decode(string $input, bool $cache = true)
{
return $this->flextype['parser']->decode($input, 'json', $cache);
}
}

View File

@@ -1,49 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFilter;
class MarkdownTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array
*/
public function getFilters() : array
{
return [
new Twig_SimpleFilter('markdown', [$this, 'markdown']),
];
}
/**
* Markdown process
*/
public function markdown($input, bool $cache = true) : string
{
return $this->flextype['parser']->decode($input, 'markdown', $cache);
}
}

View File

@@ -1,67 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFunction;
class ParserTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('parser_decode', [$this, 'decode']),
new Twig_SimpleFunction('parser_encode', [$this, 'encode']),
new Twig_SimpleFunction('parser_get_info', [$this, 'getParserInfo']),
];
}
/**
* Get Parser Info
*/
public function getParserInfo(string $parser) : array
{
return $this->flextype['parser']->getParserInfo($parser);
}
/**
* Encode
*/
public function encode($input, string $parser)
{
return $this->flextype['parser']->encode($input, $parser);
}
/**
* Decode
*/
public function decode(string $input, string $parser, bool $cache = true)
{
return $this->flextype['parser']->decode($input, $parser, $cache);
}
}

View File

@@ -1,53 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFilter;
class ShortcodesTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array
*/
public function getFilters() : array
{
return [
new Twig_SimpleFilter('shortcode', [$this, 'shortcode']),
];
}
/**
* Shorcode process
*/
public function shortcode($value) : string
{
if ($value !== null) {
return $this->flextype->shortcodes->process($value);
}
return '';
}
}

View File

@@ -1,63 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;
class SnippetsTwigExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Register Global variables in an extension
*/
public function getGlobals()
{
return [
'snippets' => new SnippetsTwig($this->flextype),
];
}
}
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);
}
}

View File

@@ -1,55 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFunction;
use Slim\Http\Environment;
use Slim\Http\Uri;
class UrlTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Callback for twig.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('url', [$this, 'url'], ['is_safe' => ['html']])
];
}
/**
* Get App URL
*/
public function url() : string
{
if ($this->flextype['registry']->has('flextype.url') && $this->flextype['registry']->get('flextype.url') != '') {
return $this->flextype['registry']->get('flextype.url');
} else {
return Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
}
}

View File

@@ -1,72 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
class YamlTwigExtension extends Twig_Extension
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions() : array
{
return [
new Twig_SimpleFunction('yaml_decode', [$this, 'decode']),
new Twig_SimpleFunction('yaml_encode', [$this, 'encode']),
];
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array
*/
public function getFilters() : array
{
return [
new Twig_SimpleFilter('yaml_decode', [$this, 'decode']),
new Twig_SimpleFilter('yaml_encode', [$this, 'encode']),
];
}
/**
* Encode YAML
*/
public function encode($input) : string
{
return $this->flextype['parser']->encode($input, 'yaml');
}
/**
* Decode YAML
*/
public function decode(string $input, bool $cache = true)
{
return $this->flextype['parser']->decode($input, 'yaml', $cache);
}
}