Пример #1
0
/**
 * configの読み込みとconfigureクラスの定義を実行する
 */
function loadConfig($argConfigPath)
{
    // 自動ジェネレートフラグの取得
    $autoGenerateFlag = getAutoGenerateEnabled();
    static $autoMigrationFlag = NULL;
    static $localFlag = NULL;
    static $devFlag = NULL;
    static $testFlag = NULL;
    static $stagingFlag = NULL;
    static $debugFlag = NULL;
    static $errorReportFlag = NULL;
    static $regenerateFlag = NULL;
    if (NULL === $errorReportFlag) {
        // 自動マイグレート設定フラグのセット
        $autoMigrationFlag = getAutoMigrationEnabled();
        // ローカル環境フラグのセット
        $localFlag = getLocalEnabled();
        // 開発環境フラグのセット
        $devFlag = getDevelopmentEnabled();
        // テスト環境(テスト用凍結環境)フラグのセット
        $testFlag = getTestEnabled();
        // ステージング環境フラグのセット
        $stagingFlag = getStagingEnabled();
        // デバッグモードフラグのセット
        $debugFlag = getDebugEnabled();
        // エラーレポートフラグのセット
        $errorReportFlag = getErrorReportEnabled();
    }
    if (TRUE === $autoGenerateFlag) {
        if (is_file($argConfigPath)) {
            // フラグメントキャッシュの更新が有れば、コンフィグファイルを強制再読み込みする為のチェック
            if (NULL === $regenerateFlag) {
                $flagcacheFileName = getAutoGeneratedPath() . 'flagcache.php';
                if (file_exists($flagcacheFileName)) {
                    require_once $flagcacheFileName;
                    if (TRUE !== (TRUE === isset($flagchaces['autoMigrationFlag']) && (int) $autoMigrationFlag == $flagchaces['autoMigrationFlag'])) {
                        $regenerateFlag = TRUE;
                    }
                    if (!isset($flagchaces['localFlag']) || (int) $localFlag != $flagchaces['localFlag']) {
                        $regenerateFlag = TRUE;
                    }
                    if (!isset($flagchaces['devFlag']) || (int) $devFlag != $flagchaces['devFlag']) {
                        $regenerateFlag = TRUE;
                    }
                    if (!isset($flagchaces['testFlag']) || (int) $testFlag != $flagchaces['testFlag']) {
                        $regenerateFlag = TRUE;
                    }
                    if (!isset($flagchaces['stagingFlag']) || (int) $stagingFlag != $flagchaces['stagingFlag']) {
                        $regenerateFlag = TRUE;
                    }
                    if (!isset($flagchaces['debugFlag']) || (int) $debugFlag != $flagchaces['debugFlag']) {
                        $regenerateFlag = TRUE;
                    }
                    if (!isset($flagchaces['errorReportFlag']) || (int) $errorReportFlag != $flagchaces['errorReportFlag']) {
                        $regenerateFlag = TRUE;
                    }
                } else {
                    $regenerateFlag = TRUE;
                }
                if (TRUE === $regenerateFlag) {
                    $flagchaceBody = '$flagchaces = array(\'autoMigrationFlag\'=>' . (int) $autoMigrationFlag . ', \'localFlag\'=>' . (int) $localFlag . ', \'devFlag\'=>' . (int) $devFlag . ', \'testFlag\'=>' . (int) $testFlag . ', \'stagingFlag\'=>' . (int) $stagingFlag . ', \'debugFlag\'=>' . (int) $debugFlag . ', \'errorReportFlag\'=>' . (int) $errorReportFlag . ');';
                    // フラグメントキャッシュを更新
                    file_put_contents($flagcacheFileName, '<?php' . PHP_EOL . $flagchaceBody . PHP_EOL . '?>');
                    @chmod($flagcacheFileName, 0777);
                }
            }
            if (TRUE !== $regenerateFlag) {
                $configFileName = basename($argConfigPath);
                $generatedConfigFileName = getAutoGeneratedPath() . $configFileName . '.generated.php';
                if (file_exists($generatedConfigFileName) && filemtime($generatedConfigFileName) >= filemtime($argConfigPath)) {
                    // 静的ファイル化されたコンフィグクラスファイルを読み込んで終了
                    // fatal errorがいいのでrequireする
                    require_once $generatedConfigFileName;
                    // リプレースは不要
                    $regenerateFlag = FALSE;
                    return TRUE;
                }
            }
        }
    }
    if (!is_file($argConfigPath)) {
        return FALSE;
    }
    // configureの初期化
    $configs = array();
    $configure = simplexml_load_file($argConfigPath, NULL, LIBXML_NOCDATA);
    // 環境フラグをセット
    if (!class_exists('Configure', FALSE)) {
        $configure->addChild('AUTO_GENERATE_ENABLED', $autoGenerateFlag);
        $configure->addChild('AUTO_MIGRATE_ENABLED', $autoMigrationFlag);
        $configure->addChild('LOCAL_ENABLED', $localFlag);
        $configure->addChild('DEV_ENABLED', $devFlag);
        $configure->addChild('TEST_ENABLED', $testFlag);
        $configure->addChild('STAGING_ENABLED', $stagingFlag);
        $configure->addChild('DEBUG_ENABLED', $debugFlag);
        $configure->addChild('ERROR_REPORT_ENABLED', $errorReportFlag);
    }
    foreach (get_object_vars($configure) as $key => $val) {
        if ('comment' != $key) {
            if (count($configure->{$key}->children()) > 0) {
                if (!isset($configs[$key . 'Configure'])) {
                    $configs[$key . 'Configure'] = '';
                }
                foreach (get_object_vars($val) as $key2 => $val2) {
                    $evalFlag = FALSE;
                    if (count($val2) > 1) {
                        $skip = TRUE;
                        for ($attrCnt = 0; count($val2) > $attrCnt; $attrCnt++) {
                            if (isset($configure->{$key}->{$key2}[$attrCnt]->attributes()->stage)) {
                                $stage = $configure->{$key}->{$key2}[$attrCnt]->attributes()->stage;
                                if ('local' == $stage && TRUE === $localFlag) {
                                    $skip = FALSE;
                                    break;
                                } elseif ('dev' == $stage && TRUE === $devFlag) {
                                    $skip = FALSE;
                                    break;
                                } elseif ('test' == $stage && TRUE === $testFlag) {
                                    $skip = FALSE;
                                    break;
                                } elseif ('staging' == $stage && TRUE === $stagingFlag) {
                                    $skip = FALSE;
                                    break;
                                }
                            } else {
                                $defAttrCnt = $attrCnt;
                            }
                        }
                        if (TRUE === $skip) {
                            $attrCnt = $defAttrCnt;
                        }
                        $val2 = $val2[$attrCnt];
                        if (isset($configure->{$key}->{$key2}[$attrCnt]->attributes()->code)) {
                            $evalFlag = TRUE;
                        }
                    } elseif (isset($configure->{$key}->{$key2}) && isset($configure->{$key}->{$key2}->attributes()->code)) {
                        $evalFlag = TRUE;
                    }
                    $val2 = trim($val2);
                    $matches = NULL;
                    if (preg_match_all('/\\%(.+)\\%/', $val2, $matches) > 0) {
                        for ($matchCnt = 0; count($matches[0]) > $matchCnt; $matchCnt++) {
                            $matchKey = $matches[0][$matchCnt];
                            $matchStr = $matches[1][$matchCnt];
                            $val2 = substr_replace($val2, $configure->{$key}->{$matchStr}, strpos($val2, $matchKey), strlen($matchKey));
                        }
                    }
                    if (TRUE === $evalFlag) {
                        if (FALSE !== strpos($val2, '__FILE__')) {
                            $val2 = str_replace('__FILE__', '\'' . realpath($argConfigPath) . '\'', $val2);
                        }
                        @eval('$val2 = ' . $val2 . ';');
                        //$configure->{$key}->{$key2} = $val2;
                        $configs[$key . 'Configure'] .= PHP_TAB . 'const ' . $key2 . ' = \'' . $val2 . '\';' . PHP_EOL;
                    } else {
                        if (strlen($val2) == 0) {
                            $configs[$key . 'Configure'] .= PHP_TAB . 'const ' . $key2 . ' = \'\';' . PHP_EOL;
                        } elseif ('TRUE' == strtoupper($val2) || 'FALSE' == strtoupper($val2) || 'NULL' == strtoupper($val2) || is_numeric($val2)) {
                            $configs[$key . 'Configure'] .= PHP_TAB . 'const ' . $key2 . ' = ' . $val2 . ';' . PHP_EOL;
                        } else {
                            $configs[$key . 'Configure'] .= PHP_TAB . 'const ' . $key2 . ' = \'' . addslashes($val2) . '\';' . PHP_EOL;
                        }
                    }
                }
            } else {
                $evalFlag = FALSE;
                if (count($val) > 1) {
                    $skip = TRUE;
                    for ($attrCnt = 0; count($val) > $attrCnt; $attrCnt++) {
                        if (isset($configure->{$key}[$attrCnt]->attributes()->stage)) {
                            $stage = $configure->{$key}[$attrCnt]->attributes()->stage;
                            if ('local' == $stage && TRUE === $localFlag) {
                                $skip = FALSE;
                                break;
                            } elseif ('dev' == $stage && TRUE === $devFlag) {
                                $skip = FALSE;
                                break;
                            } elseif ('test' == $stage && TRUE === $testFlag) {
                                $skip = FALSE;
                                break;
                            } elseif ('staging' == $stage && TRUE === $stagingFlag) {
                                $skip = FALSE;
                                break;
                            }
                        } else {
                            $defAttr = $attrCnt;
                        }
                    }
                    if (TRUE === $skip) {
                        $attrCnt = $defAttr;
                    }
                    $val = $val[$attrCnt];
                    if (isset($configure->{$key}[$attrCnt]->attributes()->code)) {
                        $evalFlag = TRUE;
                    }
                } elseif (isset($configure->{$key}->attributes()->code)) {
                    $evalFlag = TRUE;
                }
                $val = trim($val);
                $matches = NULL;
                if (preg_match_all('/\\%(.+)\\%/', $val, $matches) > 0) {
                    for ($matchCnt = 0; count($matches[0]) > $matchCnt; $matchCnt++) {
                        $matchKey = $matches[0][$matchCnt];
                        $matchStr = $matches[1][$matchCnt];
                        $val = substr_replace($val, $configure->{$matchStr}, strpos($val, $matchKey), strlen($matchKey));
                    }
                }
                if (TRUE === $evalFlag) {
                    if (FALSE !== strpos($val, '__FILE__')) {
                        $val = str_replace('__FILE__', '\'' . realpath($argConfigPath) . '\'', $val);
                    }
                    eval('$val = ' . $val . ';');
                    if (!isset($configs['Configure'])) {
                        $configs['Configure'] = '';
                    }
                    $configs['Configure'] .= PHP_TAB . 'const ' . $key . ' = \'' . $val . '\';' . PHP_EOL;
                } else {
                    if (!isset($configs['Configure'])) {
                        $configs['Configure'] = '';
                    }
                    if (strlen($val) == 0) {
                        $configs['Configure'] .= PHP_TAB . 'const ' . $key . ' = \'\';' . PHP_EOL;
                    } elseif ('TRUE' == strtoupper($val) || 'FALSE' == strtoupper($val) || 'NULL' == strtoupper($val) || is_numeric($val)) {
                        $configs['Configure'] .= PHP_TAB . 'const ' . $key . ' = ' . $val . ';' . PHP_EOL;
                    } else {
                        // XXX ココ危険!!!addslashesしないと行けないシチュエーションが出てくるかも
                        $configs['Configure'] .= PHP_TAB . 'const ' . $key . ' = \'' . addslashes($val) . '\';' . PHP_EOL;
                    }
                }
            }
        }
    }
    static $baseConfigureClassDefine = NULL;
    if (NULL === $baseConfigureClassDefine) {
        /*------------------------------ Configureクラス定義のスケルトン ココから ------------------------------*/
        $baseConfigureClassDefine = <<<_CLASSDEF_
class %class% {

\t%consts%

\tprivate static function _search(\$argVal,\$argKey,\$argHints){
\t\tif(preg_match('/'.\$argHints['hint'].'/',\$argKey)){
\t\t\t\$argHints['data'][\$argKey] = \$argVal;
\t\t}
\t}

\tpublic static function constant(\$argHint,\$argSearchFlag = FALSE){
\t\tstatic \$myConsts = NULL;
\t\tif(FALSE !== \$argSearchFlag){
\t\t\tif(NULL === \$myConsts){
\t\t\t\t\$ref = new ReflectionClass(__CLASS__);
\t\t\t\t\$myConsts = \$ref->getConstants();
\t\t\t}
\t\t\t\$tmpArr = array();
\t\t\tforeach(\$myConsts as \$key => \$val){
\t\t\t\tif(preg_match('/'.\$argHint.'/',\$key)){
\t\t\t\t\t\$tmpArr[\$key] = \$val;
\t\t\t\t}
\t\t\t}
\t\t\tif(count(\$tmpArr)>0){
\t\t\t\treturn \$tmpArr;
\t\t\t}
\t\t}
\t\telseif(TRUE === defined('self::'.\$argHint)){
\t\t\treturn constant('self::'.\$argHint);
\t\t}
\t\treturn NULL;
\t}
}

\$paths = %class%::constant(".+_PATH\$", TRUE);
foreach(\$paths as \$key => \$val){
\tset_include_path(get_include_path().PATH_SEPARATOR.\$val);
}

_CLASSDEF_;
        /*------------------------------ Configureクラス定義のスケルトン ココまで ------------------------------*/
    }
    // Configureクラスを宣言する
    $configGeneratClassDefine = NULL;
    foreach ($configs as $key => $val) {
        $configClassDefine = $baseConfigureClassDefine;
        if ('Configure' !== $key) {
            $configClassDefine .= "\r\n/*requireに成功しているので、initFrameworkでコンフィグを追加する*/\r\n_initFramework('%class%');";
        }
        $configClassDefine = str_replace('%class%', ucwords($key), $configClassDefine);
        $configClassDefine = str_replace('%consts%', substr($val, 1), $configClassDefine);
        if (TRUE === $autoGenerateFlag) {
            $configGeneratClassDefine .= $configClassDefine;
        } else {
            eval($configClassDefine);
        }
    }
    // ジェネレート処理
    if (TRUE === $autoGenerateFlag) {
        $configFileName = basename($argConfigPath);
        $generatedConfigFileName = getAutoGeneratedPath() . $configFileName . '.generated.php';
        // タブ文字削除
        $configGeneratClassDefine = str_replace(PHP_TAB, '', $configGeneratClassDefine);
        // 改行文字削除
        $configGeneratClassDefine = str_replace(array(PHP_CR, PHP_LF), '', $configGeneratClassDefine);
        file_put_contents($generatedConfigFileName, '<?php' . PHP_EOL . $configGeneratClassDefine . PHP_EOL . '?>');
        @chmod($generatedConfigFileName, 0777);
        // 静的ファイル化されたコンフィグクラスファイルを読み込む
        require_once $generatedConfigFileName;
    }
    // インクルードパスの追加処理
    foreach ($configs as $key => $val) {
        $ConfigClassName = ucwords($key);
        $paths = $ConfigClassName::constant(".+_PATH\$", TRUE);
        foreach ($paths as $key => $val) {
            set_include_path(get_include_path() . PATH_SEPARATOR . $val);
        }
    }
    return TRUE;
}
Пример #2
0
 /**
  * ジェネレートファイルのパスを取得する
  * @param unknown $argNode
  */
 public static function _getAutogenerateFilePath($argTarget)
 {
     return getAutoGeneratedPath() . self::_getAutogenerateTargetPath($argTarget) . '.generated.inc.php';
 }