Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
webguosai committed Oct 27, 2022
1 parent 8e8d7f6 commit 7f76b4f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Core/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Webguosai\Core;

class Container
{
/**
* 容器对象实例
* @var null
*/
protected static $instance = null;

/**
* 容器绑定标识
* @var array
*/
protected $bindings = [];

/**
* 绑定到容器
* @param mixed $abstract
* @param null $concrete
* @return $this
*/
public function bind($abstract, $concrete = null)
{
// 支持数组批量绑定
if (is_array($abstract)) {
foreach ($abstract as $key => $val) {
$this->bind($key, $val);
}
} else {

// 支持单个参数绑定
if (is_null($concrete)) {
$concrete = $abstract;
}

$this->bindings[$abstract] = $concrete;
}

return $this;
}

/**
* 取出容器中的数据
* @param mixed $abstract
* @param array $params
* @return mixed|object|$abstract
*/
public function make($abstract, $params = [])
{
// if (!isset($this->bindings[$abstract])) {
// throw new \Exception('没有在容器中找到');
// }

$concrete = $this->bindings[$abstract];
if ($concrete instanceof \Closure) {
return call_user_func($concrete, ...$params);
}

try {
$reflector = new \ReflectionClass($concrete);
} catch (\Exception $e) {
return $concrete;
}

return $reflector->newInstance();
}

/**
* 获取实例
* @return $this
*/
public static function getInstance()
{
if (is_null(static::$instance)) {
static::$instance = new static();
}

return static::$instance;
}
}
27 changes: 27 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Graze\ArrayMerger\RecursiveArrayMerger;
use Graze\ArrayMerger\ValueMerger\LastNonNullValue;
use Webguosai\Ai\BaiduAi;
use Webguosai\Core\Container;
use Webguosai\Crypt\Aes;
use Webguosai\Crypt\Crypt;
use Webguosai\Helper\Helper;
Expand Down Expand Up @@ -59,6 +60,32 @@

require_once '../vendor/autoload.php';

/** 容器 **/
//Container::getInstance()->bind('test', 'this is test');
//Container::getInstance()->bind('test', 111);
//Container::getInstance()->bind('test', [1,2,3,4]);
//Container::getInstance()->bind('test', function(){
// return 'sb';
//});
//Container::getInstance()->bind([
// 'test' => [1,2,3,4],
// 'test2' => 'this is test2'
//]);
//Container::getInstance()->bind(Faker::class);
//dump(Container::getInstance()->bindings);
//dump(Container::getInstance()->make(Faker::class));


//Container::getInstance()->bind('cache', function($name, $name2){return 'cache '.$name.' '. $name2;});
////dump(Container::getInstance()->bindings);
//dump(Container::getInstance()->make('cache', ['haha', 'hehe']));


//Container::getInstance()->bind('cache2');
////dump(Container::getInstance()->bindings);
//dump(Container::getInstance()->make('cache2'));


/** session **/
//设置
//Session::setConfig('F:\www\framework\tests');
Expand Down

0 comments on commit 7f76b4f

Please sign in to comment.