Example #1
0
 /**
  * 根据方法调用配置调用指定Bean的方法
  * @param Object $bean Bean对象
  * @param array $invokes 方法调用配置
  * @throws \herosphp\exception\BeanException
  */
 private static function methodsInvoke($bean, $invokes)
 {
     foreach ($invokes as $method => $params) {
         if (is_int($method)) {
             $method = $params;
             $params = null;
         } elseif ($method[0] == '@') {
             //以@开头的方法名为需要进一步处理的方法
             $methods = explode('/', $method);
             if (count($methods) != 2) {
                 continue;
             }
             $method = $methods[1];
             switch ($methods[0]) {
                 case '@id':
                     //参数是一个id指定的Bean
                     $params = Beans::get($params);
                     break;
                 case '@bean':
                     //参数是一个@bean配置项目,需要创建这个Bean
                     if (!is_array($params)) {
                         $exception = new BeanException("Bean属性配置错误!");
                         $exception->setBean($bean);
                         $exception->setAttributes($params);
                         throw $exception;
                     }
                     $params = Beans::build($params);
                     break;
                 default:
                     continue;
                     break;
             }
         }
         BeanUtil::invokeMethod($bean, $method, $params);
     }
 }