Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Oct 10, 2018
1 parent 2d11624 commit 2e1fcc6
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Context
private $register = [];
private $context = [];

function register(string $name,callable $call)
function register(string $name,$object)
{
$this->register[$name] = $call;
$this->register[$name] = $object;
return $this;
}

Expand All @@ -28,7 +28,7 @@ function register(string $name,callable $call)
* @return mixed|null
* @throws \Throwable
*/
function get(string $name, $cid = null)
function get(string $name, $cid = null,...$params)
{
if($cid === null){
$cid = Co::getUid();
Expand All @@ -37,10 +37,19 @@ function get(string $name, $cid = null)
return $this->context[$cid][$name];
}else{
if(isset($this->register[$name])){
$call = $this->register[$name];
$res = call_user_func($call);
$this->context[$cid][$name] = $res;
return $res;
$obj = $this->register[$name];
if(is_object($obj) || is_callable($obj)){
return $obj;
}else if(is_string($obj) && class_exists($obj)){
try{
$this->context[$cid][$name] = new $obj(...$params);
return $this->context[$cid][$name];
}catch (\Throwable $throwable){
throw $throwable;
}
}else{
return $obj;
}
}else{
return null;
}
Expand Down

0 comments on commit 2e1fcc6

Please sign in to comment.