1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-09 06:36:52 +02:00

feat(core): Improve Flextype Application class #463

This commit is contained in:
Awilum
2020-08-24 01:47:59 +03:00
parent 9227999b18
commit 1d693703be
3 changed files with 75 additions and 25 deletions

View File

@@ -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": {

View File

@@ -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;
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Flextype\App\Foundation\Flextype;
if (! function_exists('flextype')) {
/**
* Get the available Flextype Application instance
* or try to get Dependency Injection Container if $container is not null.
*/
function flextype($container_name = null, $container = [])
{
if (is_null($container_name)) {
return Flextype::getInstance($container);
}
return Flextype::getInstance($container)->container($container_name);
}
}