Exemple #1
0
 /**
  * ソースを追加する
  */
 public function addSource($argHint, $argTarget, $argFileEncoding = NULL, $argConvertEncoding = NULL, $argOuterEnabled = FALSE)
 {
     if (1024 >= strlen($argTarget) && TRUE === file_exists_ip($argTarget)) {
         $buffer =& self::_readBuffer($argTarget, $argFileEncoding, $argConvertEncoding);
     } else {
         // html文字列扱い
         $buffer =& self::_convertBuffer($argTarget, $argFileEncoding, $argConvertEncoding);
     }
     $this->assignHtml($argHint, $buffer, $argOuterEnabled);
     // parseし直し
     $this->refresh();
 }
 /**
  * 定義情報をチェックする
  * @param string XMLファイルのパス文字列 or XML文字列
  */
 public static function generate($argTarget, $argSection, $argBasePathTarget = '')
 {
     $filepathUsed = FALSE;
     $targetXML = $argTarget;
     if (1024 >= strlen($argTarget) && TRUE === file_exists_ip($argTarget)) {
         // ファイルパス指定の場合はファイルの中身を文字列として一旦取得
         $filepathUsed = TRUE;
         $targetXML = file_get_contents($argTarget);
     }
     // クラス名を確定する
     $className = $argSection;
     if (TRUE === $filepathUsed) {
         // オートジェネレートフラグの取得(フレームワークのコアから貰う)
         $autoGenerateFlag = getAutoGenerateEnabled();
         if (TRUE === $autoGenerateFlag) {
             $generatedClassPath = self::_getAutogenerateFilePath($argTarget);
             // unlinkされたか
             if (FALSE === resolveUnlinkAutoGeneratedFile($generatedClassPath)) {
                 // クラス名を返して終了
                 return $className;
             }
         }
     }
     // XML文字列を、XMLオブジェクト化
     libxml_use_internal_errors(TRUE);
     $FlowXML = simplexml_load_string($targetXML);
     if (FALSE === $FlowXML) {
         throw new LibXMLException(libxml_get_errors());
     }
     // FlowXMLとして正しいかどうか
     if (TRUE === self::validate($FlowXML)) {
         // $argSectionがIndex_authedだったとして、index-authedに変換される
         //$targetSection = str_replace('_', '-', ucfirst($argSection));
         // FlowXMLに基いてコントローラクラスを自動生成する
         $classDef = '';
         foreach ($FlowXML->children() as $firstNode) {
             // firstNodeのid属性から暮らす名を決める
             // idがindex-authedだったとして、Index_authedに変換される
             $tmpAttr = $firstNode->attributes();
             $sectionClassName = str_replace('-', '_', ucfirst($tmpAttr['id']));
             // 上位クラスは暫定でWebコントローラと言う事にする
             $extends = ' extends WebFlowControllerBase';
             // 上位クラスが指定されているかどうか
             if (isset($tmpAttr['extends']) && strlen($tmpAttr['extends']) > 0) {
                 // そのまま採用
                 $extends = ' extends ' . $tmpAttr['extends'];
             } elseif (isset($tmpAttr['type']) && strlen($tmpAttr['type']) > 0) {
                 $typeStr = $tmpAttr['type'];
                 // XXX $typeStrはSimpleXMLElementなので===の完全一致では動作しない!
                 if ('web' == $typeStr) {
                     $extends = ' extends WebFlowControllerBase';
                 } elseif ('api' == $typeStr) {
                     $extends = ' extends APIControllerBase';
                 } elseif ('image' == $typeStr) {
                     $extends = ' extends ImageControllerBase';
                 }
             }
             $methods = array();
             // メソッド定義
             foreach ($firstNode->children() as $methodNode) {
                 // 2次元目はメソッド定義
                 $methodName = $methodNode->getName();
                 // constructとdestructだけをマジックメソッドのマップとしてサポートする
                 if ('construct' == $methodName || 'destruct' == $methodName) {
                     $methodName = '__' . $methodName;
                 }
                 $method = PHP_TAB . 'function ' . $methodName . '(%s){' . PHP_EOL . '%s' . PHP_EOL . PHP_TAB . PHP_TAB . 'return TRUE;' . PHP_EOL . PHP_TAB . '}' . PHP_EOL;
                 $methodArg = '';
                 $methodArgs = $methodNode->attributes();
                 if (count($methodArgs) > 0) {
                     $methodArg = array();
                     foreach ($methodArgs as $key => $val) {
                         $arg = '';
                         $arg .= '$' . $key . ' = ' . self::_resolveValue($val);
                         $methodArg[] = $arg;
                     }
                     $methodArg = implode(', ', $methodArg);
                 }
                 // 3次元目は以降はネスト構造のソースコード定義
                 $code = '';
                 if (isset($extends) && strlen($extends) > 0 && ' extends WebFlowControllerBase' == $extends) {
                     // WebFlowBaseのinitをメソッドの頭で必ず呼ぶ
                     $code .= PHP_TAB . PHP_TAB . '$autoValidated = parent::_initWebFlow();' . PHP_EOL;
                 }
                 foreach ($methodNode->children() as $codeNode) {
                     $code .= self::_generateCode($codeNode, $argBasePathTarget);
                 }
                 $method = sprintf($method, $methodArg, $code);
                 $methods[] = $method;
             }
             // クラス定義つなぎ合わせ
             $classDef .= PHP_EOL . 'class ' . $sectionClassName . $extends . PHP_EOL;
             $classDef .= '{' . PHP_EOL . PHP_EOL;
             $classDef .= PHP_TAB . 'public $section=\'' . basename($argSection) . '\';' . PHP_EOL;
             $classDef .= PHP_TAB . 'public $target=\'' . $argBasePathTarget . '\';' . PHP_EOL . PHP_EOL;
             for ($methodIdx = 0; count($methods) > $methodIdx; $methodIdx++) {
                 $classDef .= $methods[$methodIdx];
             }
             $classDef .= '}' . PHP_EOL;
         }
         // オートジェネレートチェック
         if (TRUE === $filepathUsed && TRUE === $autoGenerateFlag) {
             // 空でジェネレートファイルを生成
             @file_put_contents($generatedClassPath, '');
             // ジェネレート
             generateClassCache($generatedClassPath, $argTarget, $classDef, $className);
             // 静的ファイル化されたクラスファイルを読み込んで終了
             // fatal errorがいいのでrequireする
             //require_once $generatedClassPath;
         }
         // オートジェネレートが有効だろうが無効だろうがココまで来たらクラス定義の実体化
         eval($classDef);
         return $className;
     }
     return FALSE;
 }
Exemple #3
0
 /**
  * クラス名に該当するhtmlを探しだして指定のテンプレートクラスに詰めて返す
  * @param string クラス名
  * @param string htmlの読み込事にエラーが在る場合にbooleanを返すかどうか
  * @return boolean
  */
 public static function loadTemplate($argClassName = NULL, $argFileExistsCalled = FALSE, $argTargetPath = '', $argViewType = FALSE, $argTemplateEngine = 'HtmlViewAssignor')
 {
     static $currentTargetPath = '';
     if (FALSE === $argViewType) {
         $argViewType = '.html';
     }
     if (NULL === $argViewType) {
         // XXX 拡張子未指定と判断!
         $argViewType = '';
     }
     $targetPath = '';
     if (NULL !== $argClassName) {
         $controlerClassName = $argClassName;
     } else {
         // コントロール対象を自動特定
         $controlerClassName = 'Index';
         debug('_c_=' . $_GET['_c_']);
         if (isset($_GET['_c_']) && strlen($_GET['_c_']) > 0) {
             $controlerClassName = ucfirst($_GET['_c_']);
             if (FALSE !== strpos($_GET['_c_'], '/') && strlen($_GET['_c_']) > 1) {
                 $matches = NULL;
                 if (preg_match('/(.*)\\/([^\\/]*)$/', $_GET['_c_'], $matches) && is_array($matches) && isset($matches[2])) {
                     $controlerClassName = ucfirst($matches[2]);
                     if (isset($matches[1]) && strlen($matches[1]) > 0) {
                         $targetPath = $matches[1] . '/';
                         if ('' === $currentTargetPath) {
                             $currentTargetPath = $targetPath;
                         }
                     }
                 }
             }
         }
     }
     if ('' !== $argTargetPath) {
         $targetPath = $argTargetPath;
     }
     if ('' === $targetPath) {
         $targetPath = $currentTargetPath;
     }
     $version = NULL;
     if (isset($_GET['_v_']) && strlen($_GET['_v_']) > 0) {
         $version = $_GET['_v_'];
     }
     $HtmlView = NULL;
     // htmlを読み込み
     if (NULL !== $version) {
         $basePath = $targetPath . $version . '/';
         if ('' === $targetPath && '/' === $basePath) {
             $basePath = $targetPath;
         }
         if (TRUE === file_exists_ip($basePath . $controlerClassName . $argViewType)) {
             if (TRUE === $argFileExistsCalled) {
                 return $basePath . $controlerClassName . $argViewType;
             }
             // Viewインスタンスの生成
             $HtmlView = new $argTemplateEngine($basePath . $controlerClassName . $argViewType);
         } elseif (TRUE === file_exists_ip($basePath . strtolower($controlerClassName) . $argViewType)) {
             if (TRUE === $argFileExistsCalled) {
                 return $basePath . strtolower($controlerClassName) . $argViewType;
             }
             // Viewインスタンスの生成
             $HtmlView = new $argTemplateEngine($basePath . strtolower($controlerClassName) . $argViewType);
         }
     }
     if (NULL === $HtmlView) {
         $basePath = $targetPath . '/';
         if ('' === $targetPath && '/' === $basePath) {
             $basePath = $targetPath;
         }
         // バージョンを抜いてインクルード
         if (TRUE === file_exists_ip($basePath . $controlerClassName . $argViewType)) {
             if (TRUE === $argFileExistsCalled) {
                 return $basePath . $controlerClassName . $argViewType;
             }
             // Viewインスタンスの生成
             $HtmlView = new $argTemplateEngine($basePath . $controlerClassName . $argViewType);
         } elseif (TRUE === file_exists_ip($basePath . strtolower($controlerClassName) . $argViewType)) {
             if (TRUE === $argFileExistsCalled) {
                 return $basePath . strtolower($controlerClassName) . $argViewType;
             }
             // Viewインスタンスの生成
             $HtmlView = new $argTemplateEngine($basePath . strtolower($controlerClassName) . $argViewType);
         } else {
             // エラー終了
             return FALSE;
         }
     }
     return $HtmlView;
 }