function dir_copy_wrapper($fromFile, $destFile) { // load settings $defaultNs = 'NAMESPACE'; $appNs = strtolower(__APP_NAME); // replace code if (is_file($destFile) && (preg_match('/.txt$/i', $destFile) || preg_match('/.ini$/i', $destFile) || preg_match('/.xml$/i', $destFile) || preg_match('/.java$/i', $destFile) || preg_match('/.project$/i', $destFile) || preg_match('/.properties/i', $destFile))) { echo "FILE {$destFile} ... "; $destFileContent = file_get_contents($destFile); $destFileContent = str_replace($defaultNs, $appNs, $destFileContent); file_put_contents($destFile, $destFileContent, LOCK_EX); echo "ok.\n"; return; } // change dir name if (is_dir($destFile) && preg_match("/{$defaultNs}\$/i", $destFile)) { echo "FILE1 {$destFile} ... \n"; $destDir = preg_replace("/{$defaultNs}\$/", $appNs, $destFile); echo "\nDIR1 {$destFile} > {$destDir}"; echo "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"; Hush_Util::dir_copy($destFile, $destDir); Hush_Util::dir_remove($destFile); echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; echo "ok.\n\n"; return; } }
function dir_copy_wrapper($fromFile, $destFile) { // load settings $defaultNs = 'NAMESPACE'; if (preg_match('/.sql$/i', $destFile) || preg_match('/.project$/i', $destFile)) { $appNs = strtolower(__APP_NAME); } else { $appNs = ucfirst(strtolower(__APP_NAME)); } // replace code if (is_file($destFile) && (preg_match('/.txt$/i', $destFile) || preg_match('/.ini$/i', $destFile) || preg_match('/.php$/i', $destFile) || preg_match('/.sql$/i', $destFile) || preg_match('/.project$/i', $destFile))) { echo "FILE {$destFile} ... "; $destFileContent = file_get_contents($destFile); $destFileContent = str_replace($defaultNs, $appNs, $destFileContent); file_put_contents($destFile, $destFileContent, LOCK_EX); echo "ok.\n"; return; } // replace httpd conf if (is_file($destFile) && preg_match('/.conf$/i', $destFile)) { echo "FILE {$destFile} ... "; $destFileContent = file_get_contents($destFile); $destFileContent = str_replace(array('APPNAME', 'APPROOT'), array(__APP_NAME, __APP_ROOT), $destFileContent); file_put_contents($destFile, $destFileContent, LOCK_EX); echo "ok.\n"; return; } // change dir name if (is_dir($destFile) && preg_match("/{$defaultNs}\$/i", $destFile)) { echo "FILE {$destFile} ... \n"; $destDir = preg_replace("/{$defaultNs}\$/", $appNs, $destFile); echo "\nDIR {$destFile} > {$destDir}"; echo "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"; Hush_Util::dir_copy($destFile, $destDir); Hush_Util::dir_remove($destFile); echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; echo "ok.\n\n"; return; } }
public function newappAction() { echo <<<NOTICE ********************************************************** * Start to create a new app copied from this app * ********************************************************** Please enter settings by following prompting !!! NAMESPACE of the new app : NOTICE; // check user input $namespace = trim(fgets(fopen("php://stdin", "r"))); if (!preg_match('/^[A-Za-z]+$/i', $namespace)) { echo "\nNAMESPACE must be a letter.\n"; exit; } echo <<<NOTICE LOCALPATH of the new app : NOTICE; // check user input $localpath = trim(fgets(fopen("php://stdin", "r"))); if (!is_dir($localpath)) { mkdir($localpath, 0777, true); } $localpath = realpath($localpath); if ($localpath) { echo "\nLOCALPATH : {$localpath}\n\n"; } echo <<<NOTICE Are you sure you want to continue [Y/N] : NOTICE; // check user input $input = fgets(fopen("php://stdin", "r")); if (strcasecmp(trim($input), 'y')) { exit; } // copy main code Hush_Util::dir_copy(__ROOT, $localpath, array('.svn'), array($this, 'copy_all_wrapper')); // used by copy_lib_wrapper callback $this->namespace = $namespace; // copy lib code $baseLibDir = realpath($localpath . '/lib/'); $oldLibDir = $baseLibDir . DIRECTORY_SEPARATOR . 'Ihush'; $newLibDir = $baseLibDir . DIRECTORY_SEPARATOR . $namespace; Hush_Util::dir_copy($oldLibDir, $newLibDir, null, array($this, 'copy_lib_wrapper')); // copy etc code $baseEtcDir = realpath($localpath . '/etc/'); $tmpEtcDir = $localpath . DIRECTORY_SEPARATOR . 'etc_tmp'; Hush_Util::dir_copy($baseEtcDir, $tmpEtcDir, null, array($this, 'copy_lib_wrapper')); Hush_Util::dir_copy($tmpEtcDir, $baseEtcDir, null, null); // copy bin code $baseBinDir = realpath($localpath . '/bin/'); $tmpBinDir = $localpath . DIRECTORY_SEPARATOR . 'bin_tmp'; Hush_Util::dir_copy($baseBinDir, $tmpBinDir, null, array($this, 'copy_lib_wrapper')); Hush_Util::dir_copy($tmpBinDir, $baseBinDir, null, null); // remove useless dir echo "Remove useless dirs ...\n"; Hush_Util::dir_remove($oldLibDir); Hush_Util::dir_remove($tmpEtcDir); Hush_Util::dir_remove($tmpBinDir); // change init configs echo "Change init configs ...\n"; $configFilePath = $baseEtcDir . DIRECTORY_SEPARATOR . 'global.config.php'; $configFileCode = file_get_contents($configFilePath); $pregArr = array('/__COMM_LIB_DIR\',.*?\\)/', '/__HUSH_LIB_DIR\',.*?\\)/'); $replaceArr = array('__COMM_LIB_DIR\', _hush_realpath(__ROOT . \'/../phplibs\')', '__HUSH_LIB_DIR\', _hush_realpath(__ROOT . \'/../phplibs\')'); $configFileCode = preg_replace($pregArr, $replaceArr, $configFileCode); file_put_contents($configFilePath, $configFileCode); // all completed echo <<<NOTICE ********************************************************** * Create successfully * ********************************************************** Please check new app in '{$localpath}' !!! NOTICE; }