Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Aug 14, 2018
1 parent 7a925f2 commit 33eb6ce
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
],
"require": {
"php": ">=7.1.0",
"ext-swoole":"^4.0",
"easyswoole/trigger": "^1.0"
"ext-swoole":"^4.0"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 10 additions & 10 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

namespace EasySwoole\Component;
use EasySwoole\Trigger\Trigger;
use Swoole\Coroutine as Co;

class Context
Expand All @@ -23,7 +22,13 @@ function register(string $name,callable $call)
return $this;
}

function get(string $name,$cid = null)
/**
* @param string $name
* @param null $cid
* @return mixed|null
* @throws \Throwable
*/
function get(string $name, $cid = null)
{
if($cid === null){
$cid = Co::getUid();
Expand All @@ -33,14 +38,9 @@ function get(string $name,$cid = null)
}else{
if(isset($this->register[$name])){
$call = $this->register[$name];
try{
$res = call_user_func($call);
$this->context[$cid][$name] = $res;
return $res;
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
return null;
}
$res = call_user_func($call);
$this->context[$cid][$name] = $res;
return $res;
}else{
return null;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

namespace EasySwoole\Component;
use EasySwoole\Trigger\Trigger;


class Di
Expand Down Expand Up @@ -37,6 +36,11 @@ function clear():void
$this->container = array();
}

/**
* @param $key
* @return null
* @throws \Throwable
*/
function get($key)
{
if(isset($this->container[$key])){
Expand All @@ -49,8 +53,7 @@ function get($key)
$this->container[$key]['obj'] = new $obj(...$params);
return $this->container[$key]['obj'];
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
return null;
throw $throwable;
}
}else{
return $obj;
Expand Down
18 changes: 8 additions & 10 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

namespace EasySwoole\Component;


use EasySwoole\Trigger\Trigger;

class Event extends Container
{
function set($key, $item)
Expand All @@ -22,16 +19,17 @@ function set($key, $item)
}
}

public function hook($event,...$args)
/**
* @param $event
* @param mixed ...$args
* @return mixed|null
* @throws \Throwable
*/
public function hook($event, ...$args)
{
$call = $this->get($event);
if(is_callable($call)){
try{
return call_user_func($call,...$args);
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
return null;
}
return call_user_func($call,...$args);
}else{
return null;
}
Expand Down
15 changes: 8 additions & 7 deletions src/MultiEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ function add($key, $item)
}
}

public function hook($event,...$args):array
/**
* @param $event
* @param mixed ...$args
* @return array
* @throws \Throwable
*/
public function hook($event, ...$args):array
{
$res = [];
$calls = $this->get($event);
if(is_array($calls)){
foreach ($calls as $key => $call){
try{
$res[$key] = call_user_func($call,...$args);
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
$res[$key] = null;
}
$res[$key] = call_user_func($call,...$args);
}
}
return $res;
Expand Down
24 changes: 13 additions & 11 deletions src/Pool/PoolManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@


use EasySwoole\Component\Singleton;
use EasySwoole\Trigger\Trigger;

class PoolManager
{
use Singleton;

private $pool = [];

function register(string $className,$maxNum = 20):bool
/**
* @param string $className
* @param int $maxNum
* @return bool
* @throws \Throwable
*/
function register(string $className, $maxNum = 20):bool
{
try{
$ref = new \ReflectionClass($className);
if($ref->isSubclassOf(AbstractPool::class)){
$this->pool[$this->generateKey($className)] = new $className($maxNum);
return true;
}
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
$ref = new \ReflectionClass($className);
if($ref->isSubclassOf(AbstractPool::class)){
$this->pool[$this->generateKey($className)] = new $className($maxNum);
return true;
}else{
return false;
}
return false;
}

function getPool(string $className):?AbstractPool
Expand Down

0 comments on commit 33eb6ce

Please sign in to comment.