private static function runPreparer($obj, $methodName) { $ref = new ReflectionObject($obj); $refMethod = $ref->getMethod($methodName); $comment = $refMethod->getDocComment(); if ($comment && preg_match_all('/@preparer +([a-zA-Z0-9_]+) +([a-zA-Z0-9_]+)/', $comment, $matches, PREG_SET_ORDER)) { foreach ($matches as $line) { $className = $line[1]; $propName = $line[2]; $iniFile = str_replace('_', '/', $className); $parser = new Ini_Simple(); $configs = $parser->parseIniFile("{$iniFile}.ini", true); $preparer = Builder::build($className); if (!$preparer instanceof Preparer) { trigger_error($className . ' is not implemented laiz.action.Preparer.'); continue; } self::prepare($preparer, (array) $configs); $setter = function ($val) use($obj, $propName) { $obj->{$propName} = $val; }; $ret = $preparer->prepare($setter); if ($ret) { return $ret; } } return null; } else { return null; } }
/** * Build a object. * * @param string $componentName * @param int $priority * @return Object */ public static function build($componentName) { $componentName = str_replace('.', '\\', $componentName); $iniFile = str_replace('_', '/', $componentName); $iniFile = str_replace('\\', '/', $iniFile); $parser = new Ini_Simple(); $configs = $parser->parseIniFile("{$iniFile}.ini", true); if (isset($configs['main']['class'])) { $className = $configs['main']['class']; } else { $className = $componentName; } // コンポーネントの登録 // インスタンスを作成し変数を設定する前に // コンテナに格納しないとループする if (!class_exists($className)) { // クラス定義エラー trigger_error("class {$className} is not defined.", E_USER_ERROR); return null; } // コンストラクタ引数の取得 try { $refClass = new ReflectionClass($className); $method = $refClass->getConstructor(); if ($method !== null) { $params = self::getMethodParamObjectsByReflection($className, $method); } else { $params = array(); } } catch (ReflectionException $e) { $params = array(); } if (isset($configs['main']['constructor'])) { $params = array_merge($params, self::parseArguments($configs['main']['constructor'], $configs['main'])); } // オブジェクトの生成と登録 $component = self::createObject($className, $params); self::initObject($component, $configs); return $component; }