// +---------------------------------------------------------------------- // | phpMore [ WE CAN DO IT MORE ] // +---------------------------------------------------------------------- // | Copyright (c) 20011~2019 广州务商网络科技有限公司 All rights reserved.
// +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: sujunli 4297088@qq.com // +----------------------------------------------------------------------
RunnerSdk |
<?php
/**
* // +----------------------------------------------------------------------
* // | phpMore [ WE CAN DO IT MORE ]
* // +----------------------------------------------------------------------
* // | Copyright (c) 20011~2019 广州务商网络科技有限公司 All rights reserved.
* // +----------------------------------------------------------------------
* // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
* // +----------------------------------------------------------------------
* // | Author: sujunli <4297088@qq.com>
* // +----------------------------------------------------------------------
*/
namespace cn\gz53\framework\sdk\runner;
use cn\gz53\framework\sdk\Sdk;
use ReflectionClass;
use Reflection;
use ReflectionMethod;
use ReflectionException;
class RunnerSdk extends Sdk
{
private $router = null;
private $routerList = [];
private $serviceList = [];
private $initList = [];
private $instanceList = [];
private $loadRouterCallback;
private $loadServiceCallback;
private $loadInitCallback;
public function getInitCallback(RunnerInitCallbackGetSdkI $po){
$po->setInitCallback([$this, 'init']);
return true;
}
public function getRunCallback(RunnerRunCallbackGetSdkI $po){
$po->setRunCallback([$this, 'run']);
return true;
}
public function getListCallback(RunnerListCallbackGetSdkI $po){
$po->setRouterListCallback([$this, 'setRouterList']);
$po->setServiceListCallback([$this, 'setServiceList']);
$po->setInitListCallback([$this, 'setInitList']);
return true;
}
public function setLoadCallback(RunnerLoadCallbackSetSdkI $po){
$this->loadRouterCallback = $po->getLoadRouterCallback();
$this->loadServiceCallback = $po->getLoadServiceCallback();
$this->loadInitCallback = $po->getLoadInitCallback();
return true;
}
public function init( $router = null){
$this->setRouter($router);
$this->loadInit();
$this->loadRouter();
$this->loadService();
$this->initRouter();
$this->initService();
$this->runInit();
return true;
}
public function run($router){
if(!isset($router)){
$this->sysLog(1, 'router empty', $router);
return false;
}
$this->runInstance($router);
return true;
}
public function setRouterList($router_list){
//var_dump($router_list);
$this->routerList = $router_list;
return true;
}
public function setServiceList($service_list){
//var_dump($service_list);
$this->serviceList = $service_list;
return true;
}
public function setInitList($init_list){
//var_dump($init_list);
$this->initList = $init_list;
return true;
}
//========================
private function setRouter($router){
$this->router = $router;
}
private function loadRouter(){
$list = [];
if($this->router){
$list = $this->initList;
$list[] = $this->router;
}
if(!call_user_func($this->loadRouterCallback, $list)){
$this->sysLog(2, 'loadRouterCallback fail', null);
return false;
}
return true;
}
private function loadService(){
if(!call_user_func($this->loadServiceCallback, $this->routerList)){
$this->sysLog(2, 'loadServiceCallback fail', null);
return false;
}
return true;
}
private function loadInit(){
if(!call_user_func($this->loadInitCallback)){
$this->sysLog(2, 'loadInitCallback fail', null);
return false;
}
return true;
}
private function initRouter(){
$routerList = $this->routerList;
return true;
}
private function initService(){
$serviceList = $this->serviceList;
foreach ($serviceList as $router => $services){
$instanceList = $this->getInstanceList($services);
if(!is_array($instanceList)){
$this->sysLog(2, 'getInstanceList fail', null);
continue ;
}
$this->instanceList[$router] = $instanceList;
}
return true;
}
private function runInit(){
$list = $this->initList;
if(!is_array($list)){
$this->sysLog(1, 'runInit not is_array', $list);
return false;
}
foreach ($list as $router){
if($this->runInstance($router) == false)
return false;
}
return true;
}
private function getInstanceList($services){
$instanceList = [];
if(!is_array($services)){
$this->sysLog(1, 'getInstanceList not is_array', $services);
return $instanceList;
}
foreach ($services as $key => $service){
if(isset($service['ClassName']) && isset($service['MethodName'])){
$instance = $this->newInstance($service['ClassName'], $service['MethodName']);
if($instance == null){
continue ;
}
$instanceList[] = $instance;
}elseif(isset($service['ClassName']) && isset($service['MethodName']) && isset($service['Parameters'])){
$instance = $this->newInstance($service['ClassName'], $service['MethodName'], $service['Parameters']);
if($instance == null){
continue ;
}
$instanceList[] = $instance;
}
}
return $instanceList;
}
private function newInstance($className, $methodName, $parameters = []){
$serviceInstance = null;
try{
$reflectionClass = new ReflectionClass($className);
$getInstanceMethod = $reflectionClass->getMethod('getInstance');
$instance = $getInstanceMethod->invoke(null, $this->_context);
$method = $reflectionClass->getMethod($methodName);
$serviceInstance = ['Instance'=> $instance, 'Method' => $method , 'Parameters'=> $parameters];
}catch (ReflectionException $e){
$this->sysLog(2, $e->getCode(). ':'.$e->getMessage(), $e->getTraceAsString());
return null;
}
return $serviceInstance;
}
private function runInstance($router){
if(!isset($this->instanceList[$router])){
$this->sysLog(2, 'runInstance not isset', $router);
return false;
}
$instanceList = $this->instanceList[$router];
foreach ($instanceList as $key => $val){
if(!$this->invokeArgs($val['Method'], $val['Instance'], $val['Parameters']))
return false;
}
return true;
}
private function invokeArgs(ReflectionMethod $reflectionMethod, $instance, $parameters = []){
try{
return $reflectionMethod->invokeArgs($instance, $parameters);
}catch (\Exception $e){
$this->sysLog(2, $e->getCode(). ":". $e->getMessage(), $e->getTraceAsString());
exit();
}
}
}