From 1d693703be23e6994ea3fb33bdf6ee4e9e6ccc26 Mon Sep 17 00:00:00 2001 From: Awilum Date: Mon, 24 Aug 2020 01:47:59 +0300 Subject: [PATCH] feat(core): Improve Flextype Application class #463 --- composer.json | 3 +- src/flextype/app/Foundation/Flextype.php | 72 ++++++++++++------- .../app/Support/Helpers/FlextypeHelper.php | 25 +++++++ 3 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 src/flextype/app/Support/Helpers/FlextypeHelper.php diff --git a/composer.json b/composer.json index 2b93f21c..6dc02c5b 100755 --- a/composer.json +++ b/composer.json @@ -77,7 +77,8 @@ ], "files": [ "src/flextype/app/Support/Helpers/FinderHelper.php", - "src/flextype/app/Support/Helpers/CollectionHelper.php" + "src/flextype/app/Support/Helpers/CollectionHelper.php", + "src/flextype/app/Support/Helpers/FlextypeHelper.php" ] }, "require-dev": { diff --git a/src/flextype/app/Foundation/Flextype.php b/src/flextype/app/Foundation/Flextype.php index 90a9c0aa..4e1d7f82 100644 --- a/src/flextype/app/Foundation/Flextype.php +++ b/src/flextype/app/Foundation/Flextype.php @@ -18,50 +18,74 @@ class Flextype extends App * * @var string */ - protected static $version = '0.9.10'; + const VERSION = '0.9.10'; - protected static $instance = null; + /** + * The Flextype's instance is stored in a static field. This field is an + * array, because we'll allow our Flextype to have subclasses. Each item in + * this array will be an instance of a specific Flextype's subclass. + * + * @var array + */ + private static $instances = []; - protected static $container = null; + /** + * Flextype should not be cloneable. + */ + protected function __clone() { } - public function __construct($flextype = []) + /** + * Flextype should not be restorable from strings. + */ + public function __wakeup() { - parent::__construct($flextype); - - // Store instance - self::$instance = $this; - - // Store instance container - self::$container = self::$instance->getContainer(); + throw new \Exception("Cannot unserialize a Flextype."); } /** - * Get Dependency Injection Container. + * Flextype construct * - * @param string $key DI Container key. + * @param ContainerInterface|array $container */ - public function container(?string $key = null) + protected function __construct($container = []) { - if ($key !== null) { - return self::$container[$key]; + parent::__construct($container); + } + + /** + * Get/Set Dependency Injection Container. + * + * @param string|null $name DI Container name. + */ + public function container(?string $name = null) + { + if (is_null($name)) { + return self::getInstance()->getContainer(); } - return self::$container; + return self::getInstance()->getContainer()[$name]; } /** * Returns Flextype Instance + * + * @param ContainerInterface|array $container */ - public static function getInstance() - { - return self::$instance; - } + public static function getInstance($container = []) : Flextype + { + $cls = static::class; + if (!isset(self::$instances[$cls])) { + self::$instances[$cls] = new static($container); + } + + return self::$instances[$cls]; + } /** - * This method will returns the current Flextype version + * Returns the current Flextype version */ - public static function getVersion() : string + public function getVersion() : string { - return self::$version; + return static::VERSION; } } diff --git a/src/flextype/app/Support/Helpers/FlextypeHelper.php b/src/flextype/app/Support/Helpers/FlextypeHelper.php new file mode 100644 index 00000000..0663f42b --- /dev/null +++ b/src/flextype/app/Support/Helpers/FlextypeHelper.php @@ -0,0 +1,25 @@ +container($container_name); + } +}