From 6dd28e0f05b7b16e1e669b9af459193c3bce3e96 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sun, 19 May 2024 19:57:02 -0300 Subject: [PATCH] Refact Twig caller engine, improve Version class --- system/Version/autoload.php | 42 ++++++++++++- system/engine/render.php | 14 +++-- system/etc/preferences.json | 15 +---- system/templateEngines/Twig/autoload.php | 80 +++++++++++++++++------- 4 files changed, 112 insertions(+), 39 deletions(-) diff --git a/system/Version/autoload.php b/system/Version/autoload.php index 2f7715e..aed14d4 100644 --- a/system/Version/autoload.php +++ b/system/Version/autoload.php @@ -19,8 +19,15 @@ */ private $framework; - public function __construct($framework, $teste) { + /** + * + * @var \Composer\InstalledVersions + */ + private $composerInstalled; + + public function __construct($framework, $composerInstalled, $teste) { $this->framework = $framework; + $this->composerInstalled = $composerInstalled; } /** @@ -42,6 +49,14 @@ return $this->get(); } + /** + * Alias for get method + * @return string|false + */ + public function getPhacilVersion() { + return $this->get(); + } + /** * * @return string|false @@ -49,4 +64,29 @@ public function getPHPVersion() { return phpversion(); } + + /** @return string[] */ + public function getComposerPackages() { + //return call_user_func(array($this->composerInstalled, 'getInstalledPackages')); + $composerInstalled = $this->composerInstalled; + return $composerInstalled::getInstalledPackages(); + } + + /** + * + * @param mixed $package + * @return string|null + * @throws \Phacil\Framework\Exception\OutOfBoundsException + */ + public function getPackageVersion($package) { + //return call_user_func(array($this->composerInstalled, 'getVersion'), $package); + $composerInstalled = $this->composerInstalled; + try { + return $composerInstalled::getVersion($package); + } catch (\OutOfBoundsException $ob) { + $notFind = new \Phacil\Framework\Exception\OutOfBoundsException($ob->getMessage(), $ob->getCode(), $ob); + $notFind->setObject($ob); + } + return null; + } } \ No newline at end of file diff --git a/system/engine/render.php b/system/engine/render.php index b3bb13e..0fc2de4 100644 --- a/system/engine/render.php +++ b/system/engine/render.php @@ -186,7 +186,11 @@ * @return void */ protected function twig () { - require_once(Config::DIR_SYSTEM() . "templateEngines/Twig/autoload.php"); + /** + * @var \Phacil\Framework\templateEngines\Twig + */ + $twigRegistror = $this->registry->getInstance(\Phacil\Framework\templateEngines\Twig::class); + $twigRegistror::registryTwigVersion(); /** * @var array @@ -196,10 +200,10 @@ 'cache' => Config::DIR_CACHE() . "twig/", 'debug' => Config::DEBUG()?: false ); - $TwigLoaderFilesystem = constant('\TwigLoaderFilesystem'); - $Twig_Environment = constant('\TwigEnvironment'); - $Twig_SimpleFilter = constant('\TwigSimpleFilter'); - $Twig_Extension_Debug = constant('\TwigExtensionDebug'); + $TwigLoaderFilesystem = $twigRegistror::getVar('TwigLoaderFilesystem'); + $Twig_Environment = $twigRegistror::getVar('TwigEnvironment'); + $Twig_SimpleFilter = $twigRegistror::getVar('TwigSimpleFilter'); + $Twig_Extension_Debug = $twigRegistror::getVar('TwigExtensionDebug'); /** * @var \TwigLoaderFilesystem diff --git a/system/etc/preferences.json b/system/etc/preferences.json index 24a741b..a16a926 100644 --- a/system/etc/preferences.json +++ b/system/etc/preferences.json @@ -29,18 +29,9 @@ "Phacil\\Framework\\Api\\Log", "Phacil\\Framework\\Api\\Document" ] - } - } - }, - "type": { - "Phacil\\Framework\\Version": { - "framework": "2.0.0", - "teste": { - "type": "object[]", - "value": [ - "Phacil\\Framework\\Api\\Log", - "Phacil\\Framework\\Api\\Document" - ] + }, + "composerInstalled": { + "object": "Composer\\InstalledVersions" } } } diff --git a/system/templateEngines/Twig/autoload.php b/system/templateEngines/Twig/autoload.php index c2614fe..7c664aa 100644 --- a/system/templateEngines/Twig/autoload.php +++ b/system/templateEngines/Twig/autoload.php @@ -1,28 +1,66 @@ ') == false) { - define('TwigFolderLoad', 'Twig1x'); - define('TwigLoaderFilesystem', 'Twig_Loader_Filesystem'); - define('TwigEnvironment', 'Twig_Environment'); - define('TwigSimpleFilter', 'Twig_SimpleFilter'); - define('TwigExtensionDebug', 'Twig_Extension_Debug'); - - if(!\Phacil\Framework\Registry::checkPreferenceExist($preferenceDIObj)){ - \Phacil\Framework\Registry::addDIPreference($preferenceDIObj, \Phacil\Framework\templateEngines\Twig\Extension\Legacy\Translate::class); +/** + * Copyright © 2024 ExacTI Technology Solutions. All rights reserved. + * GPLv3 General License. + * https://exacti.com.br + * Phacil PHP Framework - https://github.com/exacti/phacil-framework + */ + +namespace Phacil\Framework\templateEngines; + +class Twig { + + static private $TwigFolderLoad; + + static private $TwigLoaderFilesystem; + + static private $TwigEnvironment; + + static private $TwigSimpleFilter; + + static private $TwigExtensionDebug; + + static private $registered; + + public static function registryTwigVersion() { + if(self::$registered) + return; + + $preferenceDIObj = \Phacil\Framework\templateEngines\Twig\Api\Extension\TranslateInterface::class; + if (version_compare(phpversion(), '7.2.5', '>') == false) { + self::define('TwigFolderLoad', 'Twig1x'); + self::define('TwigLoaderFilesystem', 'Twig_Loader_Filesystem'); + self::define('TwigEnvironment', 'Twig_Environment'); + self::define('TwigSimpleFilter', 'Twig_SimpleFilter'); + self::define('TwigExtensionDebug', 'Twig_Extension_Debug'); + + if (!\Phacil\Framework\Registry::checkPreferenceExist($preferenceDIObj)) { + \Phacil\Framework\Registry::addDIPreference($preferenceDIObj, \Phacil\Framework\templateEngines\Twig\Extension\Legacy\Translate::class); + } + } else { + self::define('TwigLoaderFilesystem', '\Twig\Loader\FilesystemLoader'); + self::define('TwigEnvironment', '\Twig\Environment'); + self::define('TwigSimpleFilter', '\Twig\TwigFilter'); + self::define('TwigExtensionDebug', '\Twig\Extension\DebugExtension'); + + if (!\Phacil\Framework\Registry::checkPreferenceExist($preferenceDIObj)) { + \Phacil\Framework\Registry::addDIPreference($preferenceDIObj, 'Phacil\Framework\templateEngines\Twig\Extension\Translate'); + } } - } else { - define('TwigLoaderFilesystem', '\Twig\Loader\FilesystemLoader'); - define('TwigEnvironment', '\Twig\Environment'); - define('TwigSimpleFilter', '\Twig\TwigFilter'); - define('TwigExtensionDebug', '\Twig\Extension\DebugExtension'); - - if (!\Phacil\Framework\Registry::checkPreferenceExist($preferenceDIObj)) { - \Phacil\Framework\Registry::addDIPreference($preferenceDIObj, 'Phacil\Framework\templateEngines\Twig\Extension\Translate'); + + if (self::getVar('TwigFolderLoad')) { + include_once self::getVar('TwigFolderLoad') . "/vendor/autoload.php"; } + + self::$registered = true; + } + + protected static function define($var, $value) { + if(property_exists(__CLASS__, $var)) + self::$$var = $value; } - if(defined('TwigFolderLoad')){ - include_once TwigFolderLoad."/vendor/autoload.php"; + public static function getVar($var) { + return property_exists(__CLASS__, $var) ? self::$$var : null; } } \ No newline at end of file