From 5e5aa9739494224187578f4ea23e836c3caa3ebd Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 14 Apr 2022 01:15:07 -0300 Subject: [PATCH] Registry instances Removed global variable and use static getInstance to access instanced registry objects --- app/common/View/home.twig | 4 +++ system/engine/abstracthelper.php | 46 +++++++++++++++++++++------- system/engine/action.php | 3 +- system/engine/classes.php | 4 +-- system/engine/controller.php | 21 +++++++++---- system/engine/model.php | 37 ++++++++++++++++++----- system/engine/registry.php | 10 +++++++ system/engine/render.php | 6 ++-- system/login/autoload.php | 10 +++++-- system/system.php | 51 ++++++++++++++++++++++++++++++++ 10 files changed, 159 insertions(+), 33 deletions(-) create mode 100644 app/common/View/home.twig diff --git a/app/common/View/home.twig b/app/common/View/home.twig new file mode 100644 index 0000000..21d2ca3 --- /dev/null +++ b/app/common/View/home.twig @@ -0,0 +1,4 @@ +{{ header}} +

{{ variable }}

+ +{{footer}} \ No newline at end of file diff --git a/system/engine/abstracthelper.php b/system/engine/abstracthelper.php index 8e9715f..365b45d 100644 --- a/system/engine/abstracthelper.php +++ b/system/engine/abstracthelper.php @@ -6,7 +6,6 @@ * Phacil PHP Framework - https://github.com/exacti/phacil-framework */ - namespace Phacil\Framework; /** @package Phacil\Framework */ @@ -23,22 +22,47 @@ abstract class AbstractHelper { * @return void */ public function __construct(Registry $registry = NULL) { - if(!$registry){ - + if (!$registry) { /** - * @global \Phacil\Framework\startEngineExacTI $engine + * @var \Phacil\Framework\startEngineExacTI $engine */ - global $engine; - $registry = $engine->registry; + $registry = \Phacil\Framework\Registry::getInstance(); } - $this->registry = $registry; + $this->registry = &$registry; } - - public function __get($key) { + + /** @return void */ + final private function __getRegistryClass() + { + $this->registry = \Phacil\Framework\startEngineExacTI::getRegistry(); + } + + /** + * + * @param mixed $key + * @return mixed + */ + public function __get($key) + { + if (!$this->registry) { + $this->__getRegistryClass(); + } + return $this->registry->get($key); } - - public function __set($key, $value) { + + /** + * + * @param mixed $key + * @param mixed $value + * @return void + */ + public function __set($key, $value) + { + if (!$this->registry) { + $this->__getRegistryClass(); + } + $this->registry->set($key, $value); } } diff --git a/system/engine/action.php b/system/engine/action.php index 82c106f..527e573 100644 --- a/system/engine/action.php +++ b/system/engine/action.php @@ -135,7 +135,8 @@ final class Action implements ActionInterface { * * @since 1.0.1 * - * @package Phacil\Framework */ + * @package Phacil\Framework + */ final class ActionSystem implements ActionInterface { use ActionTrait; diff --git a/system/engine/classes.php b/system/engine/classes.php index c104ea6..4898798 100644 --- a/system/engine/classes.php +++ b/system/engine/classes.php @@ -19,7 +19,8 @@ namespace Phacil\Framework; * @uses Classes()->exists('class') to check if a class exists. * * @package Phacil\Framework - * @since 1.5.0 */ + * @since 1.5.0 + */ final class Classes { /** @@ -68,7 +69,6 @@ final class Classes { } } - return($pegaClass); } diff --git a/system/engine/controller.php b/system/engine/controller.php index 85d2e97..2c9b828 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -131,13 +131,16 @@ abstract class Controller { if (!$registry) { /** - * @global \Phacil\Framework\startEngineExacTI $engine + * @var \Phacil\Framework\startEngineExacTI $engine */ - global $engine; - - $registry =& $engine->registry; + $registry = \Phacil\Framework\Registry::getInstance(); } - $this->registry =& $registry; + $this->registry = &$registry; + } + + /** @return void */ + final private function __getRegistryClass(){ + $this->registry = \Phacil\Framework\startEngineExacTI::getRegistry(); } /** @@ -147,6 +150,10 @@ abstract class Controller { * @final */ final public function __get($key) { + if (!$this->registry) { + $this->__getRegistryClass(); + } + return $this->registry->get($key); } @@ -158,6 +165,10 @@ abstract class Controller { * @final */ final public function __set($key, $value) { + if(!$this->registry) { + $this->__getRegistryClass(); + } + $this->registry->set($key, $value); } diff --git a/system/engine/model.php b/system/engine/model.php index 5f535ab..d325d25 100644 --- a/system/engine/model.php +++ b/system/engine/model.php @@ -23,23 +23,46 @@ abstract class Model { * @return void */ public function __construct(Registry $registry = NULL) { - if(!$registry){ - + if (!$registry) { + /** - * @global \Phacil\Framework\startEngineExacTI $engine + * @var \Phacil\Framework\startEngineExacTI $engine */ - global $engine; - - $registry = $engine->registry; + $registry = \Phacil\Framework\Registry::getInstance(); } - $this->registry = $registry; + $this->registry = &$registry; + } + + /** @return void */ + final private function __getRegistryClass() + { + $this->registry = \Phacil\Framework\startEngineExacTI::getRegistry(); } + /** + * + * @param mixed $key + * @return mixed + */ public function __get($key) { + if (!$this->registry) { + $this->__getRegistryClass(); + } + return $this->registry->get($key); } + /** + * + * @param mixed $key + * @param mixed $value + * @return void + */ public function __set($key, $value) { + if (!$this->registry) { + $this->__getRegistryClass(); + } + $this->registry->set($key, $value); } } diff --git a/system/engine/registry.php b/system/engine/registry.php index 04c3a73..ff4b6c5 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -73,4 +73,14 @@ final class Registry { unset($this->$key); } } + + /** + * Try to obtain an iniciated engine instance + * + * @return \Phacil\Framework\Registry + * @since 2.0.0 + */ + static public function getInstance() { + return \Phacil\Framework\startEngineExacTI::getRegistry(); + } } diff --git a/system/engine/render.php b/system/engine/render.php index 0ed80ce..e94f021 100644 --- a/system/engine/render.php +++ b/system/engine/render.php @@ -84,11 +84,9 @@ if (!$registry) { /** - * @global \Phacil\Framework\startEngineExacTI $engine + * @var \Phacil\Framework\Registry $registry */ - global $engine; - - $registry = &$engine->registry; + $registry = \Phacil\Framework\Registry::getInstance(); } $this->registry = &$registry; diff --git a/system/login/autoload.php b/system/login/autoload.php index 5accd86..7514066 100644 --- a/system/login/autoload.php +++ b/system/login/autoload.php @@ -41,10 +41,14 @@ class Login implements \Phacil\Framework\Login\Interfaces\Login { */ public function __construct($authorizedUsers, \Phacil\Framework\Registry $registry = null){ $this->MM_authorizedUsers = $authorizedUsers; - if(!$registry){ - global $engine; - $registry =& $engine->registry; + if (!$registry) { + + /** + * @var \Phacil\Framework\Registry + */ + $registry = \Phacil\Framework\Registry::getInstance(); } + $this->registry = &$registry; $this->engine =& $registry; } diff --git a/system/system.php b/system/system.php index 9463374..38a8831 100644 --- a/system/system.php +++ b/system/system.php @@ -43,6 +43,12 @@ final class startEngineExacTI { */ public $registry; + /** + * + * @var \Phacil\Framework\Registry + */ + static private $RegistryAlt; + /** * System pre actions loader * @@ -51,6 +57,19 @@ final class startEngineExacTI { */ private $preActions = false; + /** + * Composer object + * + * @var \Composer\Autoload\ClassLoader|false + */ + private $composer = false; + + /** + * + * @var \Phacil\Framework\startEngineExacTI + */ + static private $instance; + /** * @return void * @throws Exception @@ -77,6 +96,28 @@ final class startEngineExacTI { // Registry $this->registry = new Registry(); + + self::$RegistryAlt = &$this->registry; + + if($this->composer) { + $this->registry->set('composer', $this->composer); + } + } + + /** + * + * @return \Phacil\Framework\startEngineExacTI + */ + static public function getInstance() { + return self::$instance; + } + + /** + * @param \Phacil\Framework\startEngineExacTI $instance + * @return void + */ + static public function setInstance(startEngineExacTI $instance) { + self::$instance = &$instance; } /** @@ -178,6 +219,10 @@ final class startEngineExacTI { //require_once (DIR_SYSTEM.'database/autoload.php'); require_once (DIR_SYSTEM.'engine/autoload.php'); + + if(isset($autoloadComposer)) { + $this->composer = &$autoloadComposer; + } } /** @@ -318,6 +363,11 @@ final class startEngineExacTI { return (isset($this->registry->$key)) ?: NULL; } + /** @return \Phacil\Framework\Registry */ + static public function getRegistry() { + return self::$instance->registry; + } + } /** @@ -333,6 +383,7 @@ $engine = new startEngineExacTI(); // Registry /** @var \Phacil\Framework\startEngineExacTI $engine */ $engine->engine = $engine; +$engine::setInstance($engine); // Loader /**