From cb12c0bde23b1404d903e373d655ccb63ab5134c Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 8 Feb 2024 23:04:10 -0300 Subject: [PATCH] Registry improvements --- system/engine/registry.php | 7 +++++-- .../Twig/Api/Extension/TranslateInterface.php | 1 + .../Twig/Extension/Legacy/Translate.php | 11 ++++++++++- system/templateEngines/Twig/Extension/Translate.php | 12 +++++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/system/engine/registry.php b/system/engine/registry.php index 0826fc0..ef58d9d 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -336,6 +336,9 @@ final class Registry { $originalClass = $class; $class = self::checkPreference($class); $originalClass = $originalClass == $class ? null : $originalClass; + + if($class === get_class($this)) return $this; + $refClass = new ReflectionClass($class); try { @@ -344,9 +347,9 @@ final class Registry { if (!$refClass->getConstructor() && !$argsToInject) { if ($refClass->hasMethod('getInstance') && $refClass->getMethod('getInstance')->isStatic()) { - return $refClass->getMethod('getInstance')->invoke(null); + return self::setAutoInstance($refClass->getMethod('getInstance')->invoke(null), ($originalClass ?: $class)); } - return $refClass->newInstanceWithoutConstructor(); + return self::setAutoInstance($refClass->newInstanceWithoutConstructor(), ($originalClass ?: $class)); } } catch (\Exception $th) { diff --git a/system/templateEngines/Twig/Api/Extension/TranslateInterface.php b/system/templateEngines/Twig/Api/Extension/TranslateInterface.php index c782b15..a90d42c 100644 --- a/system/templateEngines/Twig/Api/Extension/TranslateInterface.php +++ b/system/templateEngines/Twig/Api/Extension/TranslateInterface.php @@ -12,6 +12,7 @@ namespace Phacil\Framework\templateEngines\Twig\Api\Extension; * Twig translate extension interface * * @package Phacil\Framework\templateEngines\Twig\Api\Extension + * @since 2.0.0 */ interface TranslateInterface { diff --git a/system/templateEngines/Twig/Extension/Legacy/Translate.php b/system/templateEngines/Twig/Extension/Legacy/Translate.php index 833bc82..9617d3e 100644 --- a/system/templateEngines/Twig/Extension/Legacy/Translate.php +++ b/system/templateEngines/Twig/Extension/Legacy/Translate.php @@ -13,6 +13,15 @@ use Phacil\Framework\templateEngines\Twig\Extension\Legacy\TokenParser; class Translate extends \Twig_Extension implements TranslateInterface { + /** + * + * @var \Phacil\Framework\templateEngines\Twig\Extension\Legacy\TokenParser + */ + private $tokenParser; + + public function __construct(TokenParser $tokenParser){ + $this->tokenParser = $tokenParser; + } /** * {@inheritdoc} @@ -20,7 +29,7 @@ class Translate extends \Twig_Extension implements TranslateInterface public function getTokenParsers() { return array( - new TokenParser(), + $this->tokenParser, ); } diff --git a/system/templateEngines/Twig/Extension/Translate.php b/system/templateEngines/Twig/Extension/Translate.php index 4feda14..ca9b977 100644 --- a/system/templateEngines/Twig/Extension/Translate.php +++ b/system/templateEngines/Twig/Extension/Translate.php @@ -13,6 +13,16 @@ use Phacil\Framework\templateEngines\Twig\Extension\TokenParser; class Translate extends \Twig\Extension\AbstractExtension implements TranslateInterface { + /** + * + * @var \Phacil\Framework\templateEngines\Twig\Extension\Legacy\TokenParser + */ + private $tokenParser; + + public function __construct(TokenParser $tokenParser) + { + $this->tokenParser = $tokenParser; + } /** * {@inheritdoc} @@ -20,7 +30,7 @@ class Translate extends \Twig\Extension\AbstractExtension implements TranslateIn public function getTokenParsers() { return array( - new TokenParser(), + $this->tokenParser, ); }