diff --git a/system/engine/abstracthelper.php b/system/engine/abstracthelper.php index e2b937c..693fde6 100644 --- a/system/engine/abstracthelper.php +++ b/system/engine/abstracthelper.php @@ -43,6 +43,16 @@ abstract class AbstractHelper { $this->registry = \Phacil\Framework\startEngineExacTI::getRegistry(); } + /** + * + * @return object + */ + static public function getInstance() + { + $class = get_called_class(); + return \Phacil\Framework\Registry::getAutoInstance((new $class())); + } + /** * * @param mixed $key diff --git a/system/engine/controller.php b/system/engine/controller.php index 3c3efe2..62e0b7b 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -133,6 +133,15 @@ abstract class Controller { $this->registry = \Phacil\Framework\startEngineExacTI::getRegistry(); } + /** + * + * @return object + */ + static public function getInstance() { + $class = get_called_class(); + return \Phacil\Framework\Registry::getAutoInstance((new $class())); + } + /** * * @param string $key diff --git a/system/engine/model.php b/system/engine/model.php index d90fcd6..cf454dd 100644 --- a/system/engine/model.php +++ b/system/engine/model.php @@ -46,6 +46,17 @@ abstract class Model { { $this->registry = \Phacil\Framework\startEngineExacTI::getRegistry(); } + + + /** + * + * @return object + */ + static public function getInstance() + { + $class = get_called_class(); + return \Phacil\Framework\Registry::getAutoInstance((new $class())); + } /** * diff --git a/system/engine/registry.php b/system/engine/registry.php index ff4b6c5..269010a 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -22,6 +22,13 @@ final class Registry { */ private $data = array(); + /** + * Instances objects + * + * @var array + */ + private $instances = []; + /** * Original route for childs * @var string @@ -34,13 +41,30 @@ final class Registry { */ public $route; + /** + * AutoInstances Loaded + * + * @var array + */ + static private $autoInstances = []; + + /** + * Magic method to return engine instances + * + * @param string $key + * @return mixed + */ + public function __get($key) { + return $this->get($key); + } + /** * @param string $key * @return mixed */ public function get($key) { - return (isset($this->$key) ? $this->$key : $this->engine->checkRegistry($key)); + return (isset($this->instances[$key]) ? $this->instances[$key] : $this->engine->checkRegistry($key)); } /** @@ -49,7 +73,7 @@ final class Registry { * @return void */ public function set($key, $value) { - $this->$key = $value; + $this->instances[$key] = $value; } /** @@ -57,7 +81,7 @@ final class Registry { * @return bool */ public function has($key) { - return isset($this->$key); + return isset($this->instances[$key]); } /** @@ -69,18 +93,76 @@ final class Registry { * @return void */ public function delete(string $key) { - if (isset($this->$key)) { - unset($this->$key); + if (isset($this->instances[$key])) { + unset($this->instances[$key]); } } /** * Try to obtain an iniciated engine instance * + * @param string|object|null $class (optional) + * @param array $args (optional) + * * @return \Phacil\Framework\Registry * @since 2.0.0 */ - static public function getInstance() { - return \Phacil\Framework\startEngineExacTI::getRegistry(); + static public function getInstance($class = null, $args = []) { + if(!$class) return \Phacil\Framework\startEngineExacTI::getRegistry(); + + $registry = \Phacil\Framework\startEngineExacTI::getRegistry(); + + $return = false; + + $classObj = (is_object($class)) ? get_class($class) : $class; + + if (isset(self::$autoInstances[($classObj)])) return self::$autoInstances[($classObj)]; + + foreach ($registry->instances as $key => $value) { + # code... + if(!is_object($value)) continue; + + if(get_class($value) == $classObj) {$return = $value; break; } + } + + if($return) return $return; + + if(is_string($class)) { + $reflector = new ReflectionClass($class); + return self::setAutoInstance($reflector->newInstanceArgs($args)); + } + + if (is_object($class)) { + return self::getInstance($class); + } + + return null; + } + + /** + * + * @param object $class + * @return object + * @throws \Phacil\Framework\Exception + * @since 2.0.0 + */ + static public function setAutoInstance($class) { + if(!is_object($class)) throw new Exception('Object type is required!'); + + self::$autoInstances[get_class($class)] = $class; + return $class; + } + + /** + * + * @param object $class + * @return object + * @since 2.0.0 + * @throws \Phacil\Framework\Exception + */ + static public function getAutoInstance($class) { + if (!is_object($class)) throw new Exception('Object type is required!'); + + return isset(self::$autoInstances[get_class($class)]) ? self::$autoInstances[get_class($class)] : self::setAutoInstance($class); } }