add clear
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
use common\Config;
|
||||
use common\Progaram;
|
||||
|
||||
/**
|
||||
* Class Container
|
||||
*/
|
||||
class Container
|
||||
{
|
||||
|
||||
/** @var Container */
|
||||
private static $_instance;
|
||||
|
||||
/** @var \common\Config */
|
||||
private $config;
|
||||
|
||||
private $container = [];
|
||||
|
||||
const QQ_SMALL_PROGRAM = 'QQ_SMALL_PROGRAM';
|
||||
const QQ_LITTLE_GAME = 'QQ_LITTLE_GAME';
|
||||
const WX_LITTLE_GAME = 'WX_LITTLE_GAME';
|
||||
const WX_SMALL_PROGRAM = 'WX_SMALL_PROGRAM';
|
||||
const WX_OFFICIAL_ACCOUNT = 'WX_OFFICIAL_ACCOUNT';
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param Config $config
|
||||
* @return mixed
|
||||
*/
|
||||
public static function newInstance($class, $config)
|
||||
{
|
||||
if (!(static::$_instance instanceof Container)) {
|
||||
static::$_instance = new Container();
|
||||
}
|
||||
static::$_instance->generate($config);
|
||||
|
||||
if (static::$_instance->exists($class)) {
|
||||
return static::$_instance->get($class);
|
||||
} else {
|
||||
return static::$_instance->generate($config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \common\Config $config
|
||||
* @return $this
|
||||
*/
|
||||
public function generate(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function createObject($class)
|
||||
{
|
||||
$newInstance = new \ReflectionClass($class);
|
||||
if (!$newInstance->isInstantiable()) {
|
||||
throw new Exception('Class Con\'t instance.');
|
||||
}
|
||||
$newInstance = $newInstance->newInstance();
|
||||
if (method_exists($newInstance, 'initConfig')) {
|
||||
$newInstance->initConfig($this->config);
|
||||
}
|
||||
|
||||
$this->container[$class] = $newInstance;
|
||||
return $newInstance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return $this->container[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($name)
|
||||
{
|
||||
return array_key_exists($name, $this->container) && $this->container[$name] instanceof Progaram;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user