/** * Prepare command method * * @return string * @author Michal Piotrowski */ protected function prepare() { $command = $this->getCommand(); $dir = substr($this->getCommand(), 3); $afConsole = afStudioConsole::getInstance(); $pwd = $afConsole->getPwd(); if ($dir == '/') { $afConsole->setPwd('/'); } elseif ($dir == '..') { $tmp_dir = explode('/', $pwd); $cut_dir = array_pop($tmp_dir); $tmp_dir = implode('/', $tmp_dir); $afConsole->setPwd($tmp_dir); } else { $tmp_dir = $dir; if (is_dir($tmp_dir)) { $afConsole->setPwd($tmp_dir); } else { $tmp_dir = $pwd . '/' . $dir; if (is_dir($tmp_dir)) { $afConsole->setPwd($tmp_dir); } } } return $this->getCommand(); }
/** * Retrieve the instance of this class. * * @return afStudioConsole */ public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new self(); } return self::$instance; }
/** * Rename module name inside actions * * @param string $name * @param string $renamed * @param string $place * @param string $type * @return void * @author Sergey Startsev */ public static function renameAction($name, $renamed, $place, $type) { $afConsole = afStudioConsole::getInstance(); $root = afStudioUtil::getRootDir(); $dir = "{$root}/{$type}s/{$place}/modules/{$renamed}"; $actionsPath = "{$dir}/actions/actions.class.php"; // rename actions class if (file_exists($actionsPath)) { self::renameActionContent($actionsPath, $name, $renamed); } // generated lib actions class $actionsLibPath = "{$dir}/lib/Base{$name}Actions.class.php"; if (file_exists($actionsLibPath)) { self::renameActionContent($actionsLibPath, $name, $renamed); afsFileSystem::create()->rename($actionsLibPath, "{$dir}/lib/Base{$renamed}Actions.class.php"); } }
/** * Rename action name inside module * * @param string $name * @param string $renamed * @param string $module * @param string $place * @param string $type * @return boolean * @author Sergey Startsev */ public static function renameAction($name, $renamed, $module, $place, $type) { $root = afStudioUtil::getRootDir(); $afConsole = afStudioConsole::getInstance(); $console = ''; $module_dir = "{$root}/{$type}s/{$place}/modules/{$module}"; $action_dir = "{$module_dir}/actions"; $predictActions = "{$name}Action.class.php"; $predictActionsPath = "{$module_dir}/actions/{$predictActions}"; $oldName = "{$name}Action.class.php"; $newName = "{$renamed}Action.class.php"; $oldPath = "{$action_dir}/{$oldName}"; $newPath = "{$action_dir}/{$newName}"; if (file_exists($oldPath)) { $action = file_get_contents($oldPath); $action = str_ireplace("{$name}Action", "{$renamed}Action", $action); afStudioUtil::writeFile($oldPath, $action); return afsFileSystem::create()->rename($oldPath, $newPath); } return true; }
/** * Main method - execution setted command * * @return string * @author Sergey Startsev */ public final function execute() { $command = $this->prepare(); $result = array(); if (empty($command)) { if (!method_exists($this, 'getDescription')) { throw new afsBaseConsoleCommandException("If command empty - you should define getDescription method"); } $result[] = $this->getDescription(); } else { // execute ob_start(); $afConsole = afStudioConsole::getInstance(); $pwd = $afConsole->getPwd(); if (!empty($pwd)) { $pwd = trim($pwd); $command = "cd " . escapeshellarg($pwd) . " && {$command}"; } passthru($command . ' 2>&1', $status); $raw = ob_get_clean(); // getting command execute code $this->last_executed_status = $status; if ($status > 0) { $result[] = $this->render($command) . afsRenderConsoleCommand::render($raw, array('class' => 'afStudio_result_command')); } else { $arr = explode("\n", $raw); $result[] = $this->render($command); foreach ($arr as $a) { $res[] = afsRenderConsoleCommand::render($a, array('class' => 'afStudio_result_command')); } if ($res) { $result[] = implode('', $res); } } } return $result; }
/** * Autofix for checking is all migrations executed * * @return void * @author Sergey Startsev */ protected function autofixIsAllMigrationsExecuted() { \afStudioConsole::getInstance()->execute(array('sf propel:migrate')); }
/** * Adding to module functionality * * @param string $application - application name * @param string $name - module name * @return afResponse * @author Sergey Startsev */ private function addToApp($application, $module) { $afConsole = afStudioConsole::getInstance(); if (!$application || !$module) { return afResponseHelper::create()->success(false)->message("Can't create new module <b>{$module}</b> inside <b>{$application}</b> application!"); } $console = $afConsole->execute("sf generate:module {$application} {$module}"); $isCreated = $afConsole->wasLastCommandSuccessfull(); if ($isCreated) { $console .= $afConsole->execute('sf cc'); $message = "Created module <b>{$module}</b> inside <b>{$application}</b> application!"; } else { $message = "Could not create module <b>{$module}</b> inside <b>{$application}</b> application!"; } return afResponseHelper::create()->success($isCreated)->message($message)->console($console); }
/** * Setting model definition * * @param array $definition * @param string $connection * @return afResponse * @author Sergey Startsev */ public function setModelDefinition($model_name, $definition, $connection = 'propel') { $response = afResponseHelper::create(); if (!is_array($definition)) { return $response->success(false)->message('Definition contains wrong structure'); } $schema_file = $this->getSchemaFile(); $table_name = $this->getTableNameByModel($model_name); $definition_model_name = array_key_exists('_attributes', $definition) && array_key_exists('phpName', $definition['_attributes']) ? $definition['_attributes']['phpName'] : $model_name; if (!array_key_exists($model_name, $this->propelSchemaArray[$schema_file]['classes'])) { return $response->success(false)->message("Model '{$model_name}' doesn't exists"); } if (!array_key_exists($table_name, $this->originalSchemaArray[$schema_file][$connection])) { return $response->success(false)->message("Table '{$table_name}' doesn't exists"); } if (($test_table_name = $this->getTableNameByModel($definition_model_name)) && $test_table_name != $table_name) { return $response->success(false)->message("Model '{$definition_model_name}' from definition already exists in schema and not belongs current table"); } $this->originalSchemaArray[$schema_file][$connection][$table_name] = $definition; $this->saveSchema(); afStudioModelCommandHelper::deploy(); afStudioConsole::getInstance()->execute('sf afs:fix-perms'); return $response->success(true); }
/** * Update existed schemas from database * * @return string - Console results * @author Sergey Startsev */ public static function updateSchemas() { $console = afStudioConsole::getInstance()->execute(array('sf cc', 'sf appflower:validator-cache frontend cache yes', 'sf afs:update-schema', 'sf propel:build-model', 'sf propel:build-forms')); return $console; }
/** * Export data for a model * * @return afResponse * @author Radu Topala */ protected function processExportData() { $model = new $this->modelName(); $peer = $model->getPeer(); $table = $peer::TABLE_NAME; $db = afStudioUtil::getDbParams(); $backupDir = sfConfig::get('sf_root_dir') . '/data/sql/backup/'; $backupFile = $backupDir . $table . '_' . time() . '.sql'; $webFile = 'data/sql/backup/' . $table . '_' . time() . '.sql'; $console_result = afStudioConsole::getInstance()->execute(array("mkdir {$backupDir}", "mysqldump -u{$db['username']} -p{$db['password']} {$db['dbname']} {$table} --no-create-info --lock-tables --quick --complete-insert > {$backupFile}")); return afResponseHelper::create()->success(true)->message("A data backup of {$this->modelName} model was created at <a href=\"/studio#file#{$webFile}\">./{$webFile}</a>")->console($console_result); }
<script type="text/javascript"> var afStudioConsoleCommands = '<?php echo afStudioConsole::getCommands(false); ?> '; var afStudioUser = <?php echo html_entity_decode($afStudioUser); ?> ; var afStudioHost = { name: '<?php echo afStudioConsole::getInstance()->getUnameShort(); ?> ', user: '******' }; <?php $projectPath = sfConfig::get('sf_root_dir'); $projectInPath = explode('/', $projectPath); unset($projectInPath[count($projectInPath) - 1]); $projectInPath = implode('/', $projectInPath); ?> var afProjectInPath = '<?php echo $projectInPath; ?> '; var afTemplateConfig = <?php echo json_encode(afStudioUtil::getTemplateConfig());
/** * Rename page processing * * @return afResponse * @author Sergey Startsev */ protected function processDelete() { $application = $this->getParameter('app'); $page_name = pathinfo($this->getParameter('page'), PATHINFO_FILENAME); $permissions = new Permissions(); $is_writable = $permissions->isWritable(sfConfig::get('sf_apps_dir') . '/' . $application . '/config/pages/'); if ($is_writable !== true) { return $is_writable; } $page = afsPageModelHelper::retrieve($page_name, $application); if ($page->isNew()) { return afResponseHelper::create()->success(false)->message("Page <b>{$page_name}</b> doesn't exists"); } $response = $page->delete(); if ($response->getParameter(afResponseSuccessDecorator::IDENTIFICATOR)) { $response->console(afStudioConsole::getInstance()->execute('sf cc')); } return $response; }
/** * Add plugin functionality * * @return afResponse * @author Sergey Startsev */ protected function processAdd() { $name = $this->getParameter('name'); $root = sfConfig::get('sf_root_dir'); $console = afStudioConsole::getInstance(); $response = afResponseHelper::create(); $filesystem = afsFileSystem::create(); $permissions = new Permissions(); $are_writable = $permissions->areWritable(array($root . "/plugins/")); if ($are_writable !== true) { return $are_writable; } $dir = "{$root}/plugins/{$name}"; if (empty($name)) { return $response->success(false)->message('Please enter plugin name'); } if (substr($name, -6) != 'Plugin') { return $response->success(false)->message("Plugin '{$name}' should Contains 'Plugin' in the end"); } if (file_exists($dir)) { return $response->success(false)->message("Plugin '{$name}' already exists"); } $dirs = array($dir, "{$dir}/config", "{$dir}/modules"); foreach ($dirs as $dir) { $filesystem->mkdirs($dir); } if (file_exists($dir)) { // create config file with auto enable all modules in current plugin $created = afStudioUtil::writeFile("{$dir}/config/config.php", afStudioPluginCommandTemplate::config($name)); $console_result = $console->execute(array('afs fix-perms', 'sf cc')); return $response->success(true)->message("Plugin '{$name}' successfully created")->console($console_result); } return $response->success(false)->message("Some problems to create dirs, please check permissions, and run fix-perms task"); }
/** * Execute console command actiom * * @param sfWebRequest $request * @return string - json * @author Sergey Startsev */ public function executeConsole(sfWebRequest $request) { return $this->renderJson(afResponseHelper::create()->success(true)->console(afStudioConsole::getInstance()->execute(trim($request->getParameter("command"))))->asArray()); }
/** * Export project functionality * * @return afResponse * @author Sergey Startsev */ protected function processExport() { // ini set for windows ini_set("max_execution_time", "300"); $response = afResponseHelper::create(); $by_os = $this->getParameter('by_os', 'true'); $type = $this->getParameter('type', 'project'); $path = $this->getParameter('path', sys_get_temp_dir()); if (substr($path, -1, 1) != DIRECTORY_SEPARATOR) { $path .= DIRECTORY_SEPARATOR; } $name = $this->getParameter('name', pathinfo(afStudioUtil::getRootDir(), PATHINFO_BASENAME)); $console_result = afStudioConsole::getInstance()->execute("sf afs:export --type={$type} --by_os={$by_os} --path={$path} --project_name={$name}"); $postfix = $type == 'db' ? 'sql' : 'tar.gz'; if (!file_exists("{$path}{$name}.{$postfix}")) { return $response->success(false)->message('Please check permissions, and propel settings'); } return $response->success(true)->data(array(), array('name' => $name, 'file' => "{$name}.{$postfix}", 'path' => $path), 0)->console($console_result); }
/** * Generate all widgets for all models * * @return afResponse * @author Sergey Startsev */ protected function processGenerateAll() { $type = $this->getParameter('type', 'list,edit,show'); $refresh = $this->getParameter('refresh', 'false'); $console_output = afStudioConsole::getInstance()->execute("sf afs:generate-widget-all --type={$type} --refresh={$refresh}"); return afResponseHelper::create()->success(true)->console($console_output); }
/** * Getting commands list test * * @depends testInitialization * * @author Sergey Startsev */ public function testGettingCommands() { $commands = afStudioConsole::getInstance()->getCommands(); $this->assertTrue(is_array($commands), 'getCommands should return array'); }
/** * Returns Prepared commands results - for now just imploded values * * @return string * @author Sergey Startsev */ public static function processRun() { $commands = array('sf propel:build-model', 'sf appflower:validator-cache frontend cache yes', 'sf cc'); return afStudioConsole::getInstance()->execute($commands); }