TypeHintingなServiceに対応するの巻。
お手軽連携キットは、Serviceに実装されるメソッドがTypeHintingな引数をとっていた場合に対応できていなかったので、実装の仕方を変えて対応しました。
Serviceがinterfaceとimplementに分かれるメリットはTypeHintingを使って引数の型が指定できるというメリットもあるわけですから、ここをおっとこしておくといまいち利点がないですしね。
ということで、今まではServiceの実装に対してEntityのWrapperをwrapしたりunWrapしたりするinterceptorをAOPで実行する形で、EntityWrapperを使っていたんですが、
(interceptorで引数と戻り値を横取りして、WrapperをかませてReflectionでメソッドを実行させていた。そしてTypeHintingを使ってると、引数の型が違うので実行時エラーが発生していた。)
引数のunWrapと戻り値のwrapを行うメソッドを持ったクラスを作って、各ServiceのWrapperはこのクラスのサブクラスとして定義する形に変更しました。
こっちの形の方がシンプルですし、理にかなってますね。
そして、AOPが1つ減った分動作が軽い・・・といいですね。
メモ書きなのでソースのBeforeとAfter。
今回はさくさく作ってしまっているので、バージョン管理までしてなかったりなんです。
Before
moduleName = S2Base_CommandUtil::getModuleName();
if(S2Base_CommandUtil::isListExitLabel($this->moduleName)){
return;
}
//対象ディレクトリ
$this->srcDirectory = S2BASE_PHP5_MODULES_DIR . $this->moduleName . "/" . self::SOURCE_DIRECTORY_NAME;
//Entityを取得
$this->entityReflections = $this->getReflections(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_ENTITY_DIR);
//Serviceを取得
$this->serviceReflections = $this->getReflections(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_SERVICE_DIR);
//Implは削除
$replace = array();
foreach ($this->serviceReflections as $service) {
if(!ereg("Impl$", $service->name)) {
array_push($replace, $service);
}
}
$this->serviceReflections = $replace;
//DAOを取得
$this->daoReflections = $this->getReflections(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DAO_DIR);
//最終確認
if (!$this->finalConfirm()){
return;
}
//ファイル書き出し
$this->prepareFiles();
} catch(Exception $e) {
CmdCommand::showException($e);
return;
}
}
/**
* 最終確認
*/
protected function finalConfirm(){
print PHP_EOL . "[ generate information ]" . PHP_EOL;
print " entity name : { ";
foreach ($this->entityReflections as $entity) {
print $entity->name . " ";
}
print "}" . PHP_EOL;
print " dao name : { ";
foreach ($this->daoReflections as $dao) {
print $dao->name . " ";
}
print "}" . PHP_EOL;
print " service name : { ";
foreach ($this->serviceReflections as $service) {
print $service->name . " ";
}
print "}" . PHP_EOL;
return S2Base_StdinManager::isYes("confirm ?");
}
/**
* ファイル出力
*/
protected function prepareFiles(){
//srcDirectory
S2Base_CommandUtil::createDirectory($this->srcDirectory);
//Entity
S2Base_CommandUtil::createDirectory($this->srcDirectory . "/" . self::ENTITY_DIRECTORY_NAME);
foreach ($this->entityReflections as $entity) {
$this->prepareEntityFile($entity);
}
//Service
S2Base_CommandUtil::createDirectory($this->srcDirectory . "/" . self::SERVICE_DIRECTORY_NAME);
foreach ($this->serviceReflections as $service) {
$this->prepareServiceFile($service);
}
//Interceptor
S2Base_CommandUtil::createDirectory(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_INTERCEPTOR_DIR);
$this->prepareInterceptorFile();
//dicon
$this->prepareDiconFiles();
//Include File
$this->prepareIncludeFile();
}
/**
* 指定されたディレクトリに定義されたS2Base.PHP5命名規則に沿ったクラスのReflectionを取得します
* @param $directory
* @return Reflectionの配列
*/
protected function getReflections($directory) {
$result = array();
$entity_files = scandir($directory);
foreach ($entity_files as $entity_file) {
//S2Base.PHP5の命名規則にあたるものだけ対象とする
if(ereg("\.class\.php", $entity_file)) {
//ファイル名からクラス名を取得
$class_name = str_replace(".class.php", "", $entity_file);
//クラス定義がなければ、クラスをロード
if(!class_exists($class_name)) {
S2ContainerClassLoader::import($directory . $entity_file);
S2ContainerClassLoader::load($class_name);
}
array_push($result, new ReflectionClass($class_name));
}
}
return $result;
}
/**
* Entityファイルの出力メソッド
* @param $entity 出力対象となるEntityのRefrection
*/
protected function prepareEntityFile($entity) {
$srcFile = $this->srcDirectory . "/" . self::ENTITY_DIRECTORY_NAME . "/" . $entity->name . self::WRAPPER_PREFIX . self::PHP_CLASS_PREFIX;
$tempContent = "moduleName . self::WRAPPER_PREFIX . self::INCLUDE_FILE_PREFIX . "\");" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= "class " . $entity->name . self::WRAPPER_PREFIX ." {" . PHP_EOL;
$tempContent .= PHP_EOL;
//プロパティを取得
$properties = $entity->getProperties();
foreach($properties as $property) {
$tempContent .= " public \$" . $property->getName() . ";" . PHP_EOL;
}
$tempContent .= PHP_EOL;
$tempContent .= " public function wrap(\$entity) {" . PHP_EOL;
foreach($properties as $property) {
$tempContent .= " \$this->" . $property->getName() . " = \$entity->get" . ucfirst($property->getName()) . "();" . PHP_EOL;
}
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function unWrap() {" . PHP_EOL;
$tempContent .= " \$entity = new " . $entity->name . "();" . PHP_EOL;
foreach($properties as $property) {
$tempContent .= " \$entity->set" . ucfirst($property->getName()) . " (\$this->" . $property->getName() . ");" . PHP_EOL;
}
$tempContent .= " return \$entity;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= "}" . PHP_EOL;
$tempContent .= "?>" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* Serviceファイルの出力メソッド
* @param $service 出力対象となるServiceのRefrection
*/
protected function prepareServiceFile($service) {
$srcFile = $this->srcDirectory . "/" . self::SERVICE_DIRECTORY_NAME . "/" . $service->name . self::WRAPPER_PREFIX . self::PHP_CLASS_PREFIX;
$tempContent = "moduleName . self::WRAPPER_PREFIX . self::INCLUDE_FILE_PREFIX . "\");" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= "class " . $service->name . self::WRAPPER_PREFIX ." {" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " private \$container;" . PHP_EOL;
$tempContent .= " private \$service;". PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function __construct(){" . PHP_EOL;
$tempContent .= " \$this->container = S2ContainerFactory::create(MODULE_DIR . \"/dicon/" . $this->moduleName . self::WRAPPER_PREFIX . S2BASE_PHP5_DICON_SUFFIX . "\");" . PHP_EOL;
$tempContent .= " \$this->service = \$this->container->getComponent(\"" . $service->name . "\");" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function __destruct() {" . PHP_EOL;
$tempContent .= " \$this->service = null;" . PHP_EOL;
$tempContent .= " \$this->container = null;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function getContainer() {" . PHP_EOL;
$tempContent .= " return \$this->container;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
//メソッドを取得
$methods = $service->getMethods();
foreach($methods as $method) {
//publicでコンストラクタでないメソッドを対象にする
if($method->isPublic() && !$method->isConstructor()) {
$tempContent .= " public function " . $method->getName() . "(";
//パラメタを取得
$parameters = $method->getParameters();
$is_first = true;
foreach($parameters as $parameter) {
if(!$is_first) {
$tempContent .= ", ";
}
$is_first = false;
$tempContent .= "\$" . $parameter->getName();
}
$tempContent .= ") {" . PHP_EOL;
$tempContent .= " return \$this->service->" . $method->getName() . "(";
$is_first = true;
foreach($parameters as $parameter) {
if(!$is_first) {
$tempContent .= ", ";
}
$is_first = false;
$tempContent .= "\$" .$parameter->getName();
}
$tempContent .= ");" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
}
}
$tempContent .= PHP_EOL;
$tempContent .= "}" . PHP_EOL;
$tempContent .= "?>" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* Interceptorファイルの出力メソッド
*/
protected function prepareInterceptorFile() {
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_INTERCEPTOR_DIR . $this->moduleName . "Entity" . self::WRAPPER_PREFIX . "Interceptor" . S2BASE_PHP5_CLASS_SUFFIX;
$tempContent = "moduleName . "Entity" . self::WRAPPER_PREFIX . "Interceptor" . " extends S2Container_AbstractInterceptor {" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " const WRAPPER_PREFIX = \"" . self::WRAPPER_PREFIX . "\";" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " private static \$entities = array(";
$is_first = true;
foreach ($this->entityReflections as $entity) {
if(!$is_first) {
$tempContent .= ", ";
}
$is_first = false;
$tempContent .= "\"" . $entity->name . "\"";
}
$tempContent .= ");" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function invoke(S2Container_MethodInvocation \$invocation){" . PHP_EOL;
$tempContent .= " \$args = \$invocation->getArguments();" . PHP_EOL;
$tempContent .= " \$method = \$invocation->getMethod();" . PHP_EOL;
$tempContent .= " \$target = \$invocation->getThis();" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " \$replace_args = array();" . PHP_EOL;
$tempContent .= " \$is_found = false;" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " foreach (\$args as \$arg) {" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$arg, \$entity_name . self::WRAPPER_PREFIX)) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_args, \$arg->unWrap());" . PHP_EOL;
$tempContent .= " \$is_found = true;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " if(!\$is_found) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_args, \$arg);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " \$returns = S2Container_MethodUtil::invoke(\$method, \$target, \$replace_args);" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " if(is_array(\$returns)) {" . PHP_EOL;
$tempContent .= " \$replace_returns = array();" . PHP_EOL;
$tempContent .= " foreach (\$returns as \$return) {" . PHP_EOL;
$tempContent .= " \$is_found = false;" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$return, \$entity_name)) {" . PHP_EOL;
$tempContent .= " \$reflection = new ReflectionClass(\$entity_name . self::WRAPPER_PREFIX);" . PHP_EOL;
$tempContent .= " \$wrapper = \$reflection->newInstance();" . PHP_EOL;
$tempContent .= " \$wrapper->wrap(\$return);" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$wrapper);" . PHP_EOL;
$tempContent .= " \$is_found = true;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " if(!\$is_found) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$return);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " return \$replace_returns;" . PHP_EOL;
$tempContent .= " } else if(is_a(\$returns, \"S2Dao_ArrayList\")) {" . PHP_EOL;
$tempContent .= " \$replace_returns = array();" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " \$iter = \$returns->iterator();" . PHP_EOL;
$tempContent .= " \$is_found = false;" . PHP_EOL;
$tempContent .= " while(\$iter->valid()) {" . PHP_EOL;
$tempContent .= " \$return = \$iter->current();" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$return, \$entity_name)) {" . PHP_EOL;
$tempContent .= " \$reflection = new ReflectionClass(\$entity_name . self::WRAPPER_PREFIX);" . PHP_EOL;
$tempContent .= " \$wrapper = \$reflection->newInstance();" . PHP_EOL;
$tempContent .= " \$wrapper->wrap(\$return);" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$wrapper);" . PHP_EOL;
$tempContent .= " \$is_found = true;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " if(!\$is_found) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$return);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " \$iter->next();" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " return \$replace_returns;" . PHP_EOL;
$tempContent .= " } else {" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$returns, \$entity_name)) {" . PHP_EOL;
$tempContent .= " \$reflection = new ReflectionClass(\$entity_name . self::WRAPPER_PREFIX);" . PHP_EOL;
$tempContent .= " \$wrapper = \$reflection->newInstance();" . PHP_EOL;
$tempContent .= " \$wrapper->wrap(\$returns);" . PHP_EOL;
$tempContent .= " return \$wrapper;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " return \$returns;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= "}" . PHP_EOL;
$tempContent .= "?>" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* diconファイル出力メソッド
*/
protected function prepareDiconFiles() {
//pdo dicon
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DICON_DIR . "pdo" . S2BASE_PHP5_DICON_SUFFIX;
$tempContent = S2Base_CommandUtil::readFile(PDO_DICON);
self::writeFile($srcFile, $tempContent);
//dao dicon
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DICON_DIR . "dao" . S2BASE_PHP5_DICON_SUFFIX;
$tempContent = S2Base_CommandUtil::readFile(dirname(PDO_DICON) . "/dao" . S2BASE_PHP5_DICON_SUFFIX);
$pattern = "%PDO_DICON%";
$replacement = "%MODULE_DIR%/dicon/pdo.dicon";
$tempContent = str_replace($pattern, $replacement, $tempContent);
self::writeFile($srcFile, $tempContent);
//dicon
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DICON_DIR . $this->moduleName . self::WRAPPER_PREFIX . S2BASE_PHP5_DICON_SUFFIX;
$tempContent = "" . PHP_EOL;
$tempContent .= "" . PHP_EOL;
$tempContent .= "" . PHP_EOL;
$tempContent .= " " . PHP_EOL;
$tempContent .= PHP_EOL;
foreach ($this->serviceReflections as $service) {
$tempContent .= " name . "Impl\">" . PHP_EOL;
$tempContent .= " pdo.requiredTx " . PHP_EOL;
$tempContent .= " " . PHP_EOL;
$tempContent .= " moduleName . "Entity" . self::WRAPPER_PREFIX . "Interceptor\" />" . PHP_EOL;
$tempContent .= " " . PHP_EOL;
$tempContent .= " " . PHP_EOL;
}
$tempContent .= PHP_EOL;
foreach ($this->daoReflections as $dao) {
$tempContent .= " name . "\">" . PHP_EOL;
$tempContent .= " dao.interceptor " . PHP_EOL;
$tempContent .= " " . PHP_EOL;
}
$tempContent .= PHP_EOL;
$tempContent .= " " . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* インクルードファイル出力メソッド
*/
protected function prepareIncludeFile() {
$srcFile = $this->srcDirectory . "/" . $this->moduleName . self::WRAPPER_PREFIX . self::INCLUDE_FILE_PREFIX;
$tempContent = "" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
}
?>
After
moduleName = S2Base_CommandUtil::getModuleName();
if(S2Base_CommandUtil::isListExitLabel($this->moduleName)){
return;
}
//対象ディレクトリ
$this->srcDirectory = S2BASE_PHP5_MODULES_DIR . $this->moduleName . "/" . self::SOURCE_DIRECTORY_NAME;
//Entityを取得
$this->entityReflections = $this->getReflections(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_ENTITY_DIR);
//Serviceを取得
$this->serviceReflections = $this->getReflections(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_SERVICE_DIR);
//Implは削除
$replace = array();
foreach ($this->serviceReflections as $service) {
if(!ereg("Impl$", $service->name)) {
array_push($replace, $service);
}
}
$this->serviceReflections = $replace;
//DAOを取得
$this->daoReflections = $this->getReflections(S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DAO_DIR);
//最終確認
if (!$this->finalConfirm()){
return;
}
//ファイル書き出し
$this->prepareFiles();
} catch(Exception $e) {
CmdCommand::showException($e);
return;
}
}
/**
* 最終確認
*/
protected function finalConfirm(){
print PHP_EOL . "[ generate information ]" . PHP_EOL;
print " entity name : { ";
foreach ($this->entityReflections as $entity) {
print $entity->name . " ";
}
print "}" . PHP_EOL;
print " dao name : { ";
foreach ($this->daoReflections as $dao) {
print $dao->name . " ";
}
print "}" . PHP_EOL;
print " service name : { ";
foreach ($this->serviceReflections as $service) {
print $service->name . " ";
}
print "}" . PHP_EOL;
return S2Base_StdinManager::isYes("confirm ?");
}
/**
* ファイル出力
*/
protected function prepareFiles(){
//srcDirectory
S2Base_CommandUtil::createDirectory($this->srcDirectory);
//Entity
S2Base_CommandUtil::createDirectory($this->srcDirectory . "/" . self::ENTITY_DIRECTORY_NAME);
foreach ($this->entityReflections as $entity) {
$this->prepareEntityFile($entity);
}
//Service
S2Base_CommandUtil::createDirectory($this->srcDirectory . "/" . self::SERVICE_DIRECTORY_NAME);
$this->prepareServiceWrapperFile();
foreach ($this->serviceReflections as $service) {
$this->prepareServiceFile($service);
}
//dicon
$this->prepareDiconFiles();
//Include File
$this->prepareIncludeFile();
}
/**
* 指定されたディレクトリに定義されたS2Base.PHP5命名規則に沿ったクラスのReflectionを取得します
* @param $directory
* @return Reflectionの配列
*/
protected function getReflections($directory) {
$result = array();
$entity_files = scandir($directory);
foreach ($entity_files as $entity_file) {
//S2Base.PHP5の命名規則にあたるものだけ対象とする
if(ereg("\.class\.php", $entity_file)) {
//ファイル名からクラス名を取得
$class_name = str_replace(".class.php", "", $entity_file);
//クラス定義がなければ、クラスをロード
if(!class_exists($class_name)) {
S2ContainerClassLoader::import($directory . $entity_file);
S2ContainerClassLoader::load($class_name);
}
array_push($result, new ReflectionClass($class_name));
}
}
return $result;
}
/**
* Entityファイルの出力メソッド
* @param $entity 出力対象となるEntityのRefrection
*/
protected function prepareEntityFile($entity) {
$srcFile = $this->srcDirectory . "/" . self::ENTITY_DIRECTORY_NAME . "/" . $entity->name . self::WRAPPER_PREFIX . self::PHP_CLASS_PREFIX;
$tempContent = "moduleName . self::WRAPPER_PREFIX . self::INCLUDE_FILE_PREFIX . "\");" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= "class " . $entity->name . self::WRAPPER_PREFIX ." {" . PHP_EOL;
$tempContent .= PHP_EOL;
//プロパティを取得
$properties = $entity->getProperties();
foreach($properties as $property) {
$tempContent .= " public \$" . $property->getName() . ";" . PHP_EOL;
}
$tempContent .= PHP_EOL;
$tempContent .= " public function wrap(\$entity) {" . PHP_EOL;
foreach($properties as $property) {
$tempContent .= " \$this->" . $property->getName() . " = \$entity->get" . ucfirst($property->getName()) . "();" . PHP_EOL;
}
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function unWrap() {" . PHP_EOL;
$tempContent .= " \$entity = new " . $entity->name . "();" . PHP_EOL;
foreach($properties as $property) {
$tempContent .= " \$entity->set" . ucfirst($property->getName()) . " (\$this->" . $property->getName() . ");" . PHP_EOL;
}
$tempContent .= " return \$entity;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= "}" . PHP_EOL;
$tempContent .= "?>" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* ServiceWrapperのスーパクラスファイルの出力メソッド
*/
protected function prepareServiceWrapperFile() {
$srcFile = $this->srcDirectory . "/" . self::SERVICE_DIRECTORY_NAME . "/Service" . self::WRAPPER_PREFIX . self::PHP_CLASS_PREFIX;
$tempContent = "entityReflections as $entity) {
if(!$is_first) {
$tempContent .= ", ";
}
$is_first = false;
$tempContent .= "\"" . $entity->name . "\"";
}
$tempContent .= ");" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " protected function unWrapEntities(\$args) {" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " if(is_array(\$args)) {" . PHP_EOL;
$tempContent .= " \$replace_args = array();" . PHP_EOL;
$tempContent .= " foreach (\$args as \$arg) {" . PHP_EOL;
$tempContent .= " \$is_found = false;" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$arg, \$entity_name . self::WRAPPER_PREFIX)) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_args, \$arg->unWrap());" . PHP_EOL;
$tempContent .= " \$is_found = true;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " if(!\$is_found) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_args, \$arg);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " return \$replace_args;" . PHP_EOL;
$tempContent .= " } else {" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$args, \$entity_name . self::WRAPPER_PREFIX)) {" . PHP_EOL;
$tempContent .= " return \$args->unWrap();" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " return \$args;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " protected function wrapReturns(\$returns) {" . PHP_EOL;
$tempContent .= " if(is_array(\$returns)) {" . PHP_EOL;
$tempContent .= " \$replace_returns = array();" . PHP_EOL;
$tempContent .= " foreach (\$returns as \$return) {" . PHP_EOL;
$tempContent .= " \$is_found = false;" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$return, \$entity_name)) {" . PHP_EOL;
$tempContent .= " \$reflection = new ReflectionClass(\$entity_name . self::WRAPPER_PREFIX);" . PHP_EOL;
$tempContent .= " \$wrapper = \$reflection->newInstance();" . PHP_EOL;
$tempContent .= " \$wrapper->wrap(\$return);" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$wrapper);" . PHP_EOL;
$tempContent .= " \$is_found = true;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " if(!\$is_found) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$return);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " return \$replace_returns;" . PHP_EOL;
$tempContent .= " } else if(is_a(\$returns, \"S2Dao_ArrayList\")) {" . PHP_EOL;
$tempContent .= " \$replace_returns = array();" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " \$iter = \$returns->iterator();" . PHP_EOL;
$tempContent .= " \$is_found = false;" . PHP_EOL;
$tempContent .= " while(\$iter->valid()) {" . PHP_EOL;
$tempContent .= " \$return = \$iter->current();" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$return, \$entity_name)) {" . PHP_EOL;
$tempContent .= " \$reflection = new ReflectionClass(\$entity_name . self::WRAPPER_PREFIX);" . PHP_EOL;
$tempContent .= " \$wrapper = \$reflection->newInstance();" . PHP_EOL;
$tempContent .= " \$wrapper->wrap(\$return);" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$wrapper);" . PHP_EOL;
$tempContent .= " \$is_found = true;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " if(!\$is_found) {" . PHP_EOL;
$tempContent .= " array_push(\$replace_returns, \$return);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " \$iter->next();" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " return \$replace_returns;" . PHP_EOL;
$tempContent .= " } else {" . PHP_EOL;
$tempContent .= " foreach(self::\$entities as \$entity_name) {" . PHP_EOL;
$tempContent .= " if(is_a(\$returns, \$entity_name)) {" . PHP_EOL;
$tempContent .= " \$reflection = new ReflectionClass(\$entity_name . self::WRAPPER_PREFIX);" . PHP_EOL;
$tempContent .= " \$wrapper = \$reflection->newInstance();" . PHP_EOL;
$tempContent .= " \$wrapper->wrap(\$returns);" . PHP_EOL;
$tempContent .= " return \$wrapper;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " return \$returns;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= "}" . PHP_EOL;
$tempContent .= "?>" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* Serviceファイルの出力メソッド
* @param $service 出力対象となるServiceのRefrection
*/
protected function prepareServiceFile($service) {
$srcFile = $this->srcDirectory . "/" . self::SERVICE_DIRECTORY_NAME . "/" . $service->name . self::WRAPPER_PREFIX . self::PHP_CLASS_PREFIX;
$tempContent = "moduleName . self::WRAPPER_PREFIX . self::INCLUDE_FILE_PREFIX . "\");" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= "class " . $service->name . self::WRAPPER_PREFIX ." extends Service" . self::WRAPPER_PREFIX . "{" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " private \$container;" . PHP_EOL;
$tempContent .= " private \$service;". PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function __construct(){" . PHP_EOL;
$tempContent .= " \$this->container = S2ContainerFactory::create(MODULE_DIR . \"/dicon/" . $this->moduleName . self::WRAPPER_PREFIX . S2BASE_PHP5_DICON_SUFFIX . "\");" . PHP_EOL;
$tempContent .= " \$this->service = \$this->container->getComponent(\"" . $service->name . "\");" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function __destruct() {" . PHP_EOL;
$tempContent .= " \$this->service = null;" . PHP_EOL;
$tempContent .= " \$this->container = null;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= PHP_EOL;
$tempContent .= " public function getContainer() {" . PHP_EOL;
$tempContent .= " return \$this->container;" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
//メソッドを取得
$methods = $service->getMethods();
foreach($methods as $method) {
//publicでコンストラクタでないメソッドを対象にする
if($method->isPublic() && !$method->isConstructor()) {
$tempContent .= " public function " . $method->getName() . "(";
//パラメタを取得
$parameters = $method->getParameters();
$is_first = true;
foreach($parameters as $parameter) {
if(!$is_first) {
$tempContent .= ", ";
}
$is_first = false;
$tempContent .= "\$" . $parameter->getName();
}
$tempContent .= ") {" . PHP_EOL;
foreach($parameters as $parameter) {
$tempContent .= " \$unwrap_" . $parameter->getName() . " = \$this->unWrapEntities(\$" . $parameter->getName() . ");" . PHP_EOL;
}
$tempContent .= " \$returns = \$this->service->" . $method->getName() . "(";
$is_first = true;
foreach($parameters as $parameter) {
if(!$is_first) {
$tempContent .= ", ";
}
$is_first = false;
$tempContent .= "\$unwrap_" .$parameter->getName();
}
$tempContent .= ");" . PHP_EOL;
$tempContent .= " return \$this->wrapReturns(\$returns);" . PHP_EOL;
$tempContent .= " }" . PHP_EOL;
$tempContent .= PHP_EOL;
}
}
$tempContent .= PHP_EOL;
$tempContent .= "}" . PHP_EOL;
$tempContent .= "?>" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* diconファイル出力メソッド
*/
protected function prepareDiconFiles() {
//pdo dicon
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DICON_DIR . "pdo" . S2BASE_PHP5_DICON_SUFFIX;
$tempContent = S2Base_CommandUtil::readFile(PDO_DICON);
self::writeFile($srcFile, $tempContent);
//dao dicon
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DICON_DIR . "dao" . S2BASE_PHP5_DICON_SUFFIX;
$tempContent = S2Base_CommandUtil::readFile(dirname(PDO_DICON) . "/dao" . S2BASE_PHP5_DICON_SUFFIX);
$pattern = "%PDO_DICON%";
$replacement = "%MODULE_DIR%/dicon/pdo.dicon";
$tempContent = str_replace($pattern, $replacement, $tempContent);
self::writeFile($srcFile, $tempContent);
//dicon
$srcFile = S2BASE_PHP5_MODULES_DIR . $this->moduleName . S2BASE_PHP5_DICON_DIR . $this->moduleName . self::WRAPPER_PREFIX . S2BASE_PHP5_DICON_SUFFIX;
$tempContent = "" . PHP_EOL;
$tempContent .= "" . PHP_EOL;
$tempContent .= "" . PHP_EOL;
$tempContent .= " " . PHP_EOL;
$tempContent .= PHP_EOL;
foreach ($this->serviceReflections as $service) {
$tempContent .= " name . "Impl\">" . PHP_EOL;
$tempContent .= " pdo.requiredTx " . PHP_EOL;
$tempContent .= " " . PHP_EOL;
}
$tempContent .= PHP_EOL;
foreach ($this->daoReflections as $dao) {
$tempContent .= " name . "\">" . PHP_EOL;
$tempContent .= " dao.interceptor " . PHP_EOL;
$tempContent .= " " . PHP_EOL;
}
$tempContent .= PHP_EOL;
$tempContent .= " " . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
/**
* インクルードファイル出力メソッド
*/
protected function prepareIncludeFile() {
$srcFile = $this->srcDirectory . "/" . $this->moduleName . self::WRAPPER_PREFIX . self::INCLUDE_FILE_PREFIX;
$tempContent = "" . PHP_EOL;
self::writeFile($srcFile, $tempContent);
}
}
?>
とりあえずソースいじるのはここまでにして、ドキュメントまとめないと。