public static function run($class) { $args = $_SERVER["argv"]; array_shift($args); $class = array_shift($args); unshift_include_path(RUN_BASE . DS . "tasks"); if (class_exists($class, true)) { $ins = new $class(); $ins->setArguments($args); if (isset($args[0]) && ($args[0] === "-h" || $args[0] === "--help")) { $ins->usage(); } else { try { if ($ins->hasMethod("initialize")) { $ins->initialize(); } $ins->run(); if ($ins->hasMethod("finalize")) { $ins->finalize(); } } catch (Exception $e) { Sabel_Console::error($e->getMessage()); } } } else { Sabel_Console::error("such a task doesn't exist."); } }
protected function dropColumn() { $restore = $this->getRestoreFileName(); if (Sabel_Db_Migration_Manager::isUpgrade()) { if (is_file($restore)) { unlink($restore); } $columns = $this->getReader()->readDropColumn()->getColumns(); $schema = $this->getSchema()->getTable($this->tblName); $sColumns = $schema->getColumns(); $writer = new Sabel_Db_Migration_Writer($restore); $writer->writeColumns($schema, $columns); $writer->close(); foreach ($columns as $column) { if (isset($sColumns[$column])) { unset($sColumns[$column]); } else { $warning = "column '{$column}' does not exist. (SKIP)"; Sabel_Console::warning($warning); } } $this->dropColumnsAndRemakeTable($sColumns, $schema); } else { $columns = $this->getReader($restore)->readAddColumn()->getColumns(); $this->execAddColumn($columns); } }
protected function defineEnvironmentByOption($opt = "e", $default = DEVELOPMENT) { if (Sabel_Console::hasOption($opt, $this->arguments)) { $this->defineEnvironment(Sabel_Console::getOption($opt, $this->arguments)); } elseif (!defined("ENVIRONMENT")) { define("ENVIRONMENT", $default); } }
public function downgrade($query) { if (is_string($query)) { $this->downgradeQueries[] = $query; } else { Sabel_Console::error("argument must be a string."); exit; } }
public function testGetOption2() { $args = array("cmd", "--dir=/var/tmp", "--file=/tmp/test.txt"); $this->assertEquals("/var/tmp", Sabel_Console::getOption("dir", $args)); $this->assertEquals(array("cmd", "--file=/tmp/test.txt"), $args); $args = array("cmd", "--dir=/var/tmp", "--file=/tmp/test.txt"); $this->assertEquals("/tmp/test.txt", Sabel_Console::getOption("file", $args)); $this->assertEquals(array("cmd", "--dir=/var/tmp"), $args); }
private function setBoolean($bool, $key) { if (is_bool($bool)) { $this->column->{$key} = $bool; } else { Sabel_Console::error("argument for {$key}() should be a boolean."); exit; } }
public function unique($columnNames) { if (is_string($columnNames)) { $this->uniques[] = (array) $columnNames; } elseif (is_array($columnNames)) { $this->uniques[] = $columnNames; } else { Sabel_Console::error("unique() argument should be a string or an array."); exit; } }
public function start($testName, $testFilePath) { if (is_readable($testFilePath)) { try { $testCaseName = $this->classPrefix . $testName; $this->doRun($this->getTest($testCaseName, $testFilePath)); } catch (Exception $e) { Sabel_Console::error("couldn't run the TestSuite: " . $e->getMessage()); } } else { Sabel_Console::error($testFilePath . " not found"); } }
public function run() { $args = $this->arguments; if (Sabel_Console::hasOption("v", $args)) { $this->version = Sabel_Console::getOption("v", $args); } if (Sabel_Console::hasOption("a", $args)) { $this->installAddon(Sabel_Console::getOption("a", $args)); } elseif (Sabel_Console::hasOption("l", $args)) { // library } elseif (Sabel_Console::hasOption("p", $args)) { // processor } else { } }
public function run() { $args = $this->arguments; if (Sabel_Console::hasOption("v", $args)) { $version = Sabel_Console::getOption("v", $args); } else { $version = "HEAD"; } if (Sabel_Console::hasOption("a", $args)) { $this->installAddon(Sabel_Console::getOption("a", $args), $version); } else { $message = __METHOD__ . "() invalid install option."; throw new Sabel_Exception_Runtime($message); } }
private static function initTable() { if (extension_loaded("mysql")) { $params = array("package" => "sabel.db.mysql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pgsql")) { $params = array("package" => "sabel.db.pgsql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pdo_sqlite")) { $params = array("package" => "sabel.db.pdo.sqlite", "database" => SABEL_BASE . "/Test/data/sdb_test.sq3"); } else { Sabel_Console::message("skipped 'Test_Session_Database'."); return false; } Sabel_Db_Config::add("default", $params); Sabel_Db::createDriver("default")->execute("DELETE FROM session"); return true; }
protected function getFixtureMethod() { $method = "upFixture"; $arguments = $this->arguments; if (Sabel_Console::hasOption("up", $arguments)) { $index = array_search("--up", $arguments, true); unset($arguments[$index]); $arguments = array_values($arguments); } if (Sabel_Console::hasOption("down", $arguments)) { $index = array_search("--down", $arguments, true); unset($arguments[$index]); $arguments = array_values($arguments); $method = "downFixture"; } $this->arguments = $arguments; return $method; }
protected function create() { $tblName = convert_to_tablename($this->mdlName); $schema = $this->getSchema(); $tables = $schema->getTableList(); if (Sabel_Db_Migration_Manager::isUpgrade()) { if (in_array($tblName, $tables)) { Sabel_Console::warning("table '{$tblName}' already exists. (SKIP)"); } else { $this->createTable($this->filePath); } } else { if (in_array($tblName, $tables)) { $this->dropSequence($schema->getTable($tblName)->getSequenceColumn()); $this->executeQuery("DROP TABLE " . $this->quoteIdentifier($tblName)); } else { Sabel_Console::warning("unknown table '{$tblName}'. (SKIP)"); } } }
public static function getFiles() { if (!is_dir(self::$directory)) { Sabel_Console::error("no such dirctory. '" . self::$directory . "'"); exit; } $files = array(); foreach (scandir(self::$directory) as $file) { $num = substr($file, 0, strpos($file, "_")); if (!is_numeric($num)) { continue; } if (isset($files[$num])) { Sabel_Console::error("the same version({$num}) files exists."); exit; } else { $files[$num] = $file; } } ksort($files); return $files; }
private static function initTable() { if (extension_loaded("mysql")) { $params = array("package" => "sabel.db.mysql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pgsql")) { $params = array("package" => "sabel.db.pgsql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pdo_sqlite")) { $params = array("package" => "sabel.db.pdo.sqlite", "database" => SABEL_BASE . "/Test/data/sdb_test.sq3"); } else { Sabel_Console::message("skipped 'TemplateDb'."); return false; } Sabel_Db_Config::add("default", $params); $stmt = Sabel_Db::createStatement(); $tblName = $stmt->quoteIdentifier("sbl_template"); $nCol = $stmt->quoteIdentifier("name"); $nsCol = $stmt->quoteIdentifier("namespace"); $cCol = $stmt->quoteIdentifier("contents"); $stmt->setQuery("DELETE FROM {$tblName}")->execute(); $data = array(); $data[0]["path"] = "views" . DS . "serverError" . TPL_SUFFIX; $data[0]["cont"] = ""; $data[1]["path"] = "index" . DS . "views" . DS . "error" . TPL_SUFFIX; $data[1]["cont"] = ""; $data[2]["path"] = "index" . DS . "views" . DS . "hoge" . DS . "index" . TPL_SUFFIX; $data[2]["cont"] = "hoge/index.tpl"; $data[3]["path"] = "index" . DS . "views" . DS . "hoge" . DS . "hoge" . TPL_SUFFIX; $data[3]["cont"] = "hoge/hoge.tpl"; $data[4]["path"] = "index" . DS . "views" . DS . "fuga" . DS . "index" . TPL_SUFFIX; $data[4]["cont"] = "fuga/index.tpl"; $data[5]["path"] = "index" . DS . "views" . DS . "fuga" . DS . "fuga" . TPL_SUFFIX; $data[5]["cont"] = "fuga/fuga.tpl"; foreach ($data as $d) { $query = "INSERT INTO {$tblName}({$nCol}, {$nsCol}, {$cCol}) VALUES('{$d["path"]}', '', '{$d["cont"]}')"; $stmt->setQuery($query)->execute(); } return true; }
public function write($connectionName) { $cn = $connectionName; $fileName = ucfirst($cn) . "TableList"; $className = "Schema_" . $fileName; Sabel_Console::success("output table list of '{$cn}' database."); $contents = array(); $contents[] = "<?php" . PHP_EOL; $contents[] = "class {$className}"; $contents[] = "{"; $contents[] = " public function get()"; $contents[] = " {"; $tables = array_map(create_function('$v', 'return \'"\' . $v . \'"\';'), $this->tables[$cn]); $contents[] = " return array(" . implode(", ", $tables) . ");"; $contents[] = " }"; $contents[] = "}"; $fp = fopen($this->outputDir . DS . $fileName . ".php", "w"); fwrite($fp, implode(PHP_EOL, $contents)); fclose($fp); }
protected function readOptions($args) { if (in_array("--overwrite", $args, true)) { $index = array_search("--overwrite", $args) + 1; for ($i = $index, $c = count($args); $i < $c; $i++) { if (substr($args[$i], 0, 2) === "--") { break; } $path = $this->targetDir . DS . $args[$i]; if (is_file($path)) { unlink($path); } elseif (is_dir($path)) { $fs = new Sabel_Util_FileSystem($path); $fs->rmdir(); } } } if (in_array("--ignore", $args, true)) { $index = array_search("--ignore", $args) + 1; for ($i = $index, $c = count($args); $i < $c; $i++) { if (substr($args[$i], 0, 2) === "--") { break; } $this->ignore[$args[$i]] = 1; } } if (Sabel_Console::hasOption("l", $args)) { if (($lang = Sabel_Console::getOption("l", $args)) !== null) { $lang = strtolower($lang); if (is_dir($this->skeletonDir . DS . $lang)) { $this->lang = $lang; } } } }
protected function dropColumn() { $restore = $this->getRestoreFileName(); if (Sabel_Db_Migration_Manager::isUpgrade()) { if (is_file($restore)) { unlink($restore); } $columns = $this->getReader()->readDropColumn()->getColumns(); $schema = $this->getSchema()->getTable($this->tblName); $colNames = $schema->getColumnNames(); $writer = new Sabel_Db_Migration_Writer($restore); $writer->writeColumns($schema, $columns)->close(); $quotedTblName = $this->quoteIdentifier($this->tblName); foreach ($columns as $column) { if (in_array($column, $colNames)) { $colName = $this->quoteIdentifier($column); $this->executeQuery("ALTER TABLE {$quotedTblName} DROP COLUMN {$colName}"); } else { Sabel_Console::warning("column '{$column}' does not exist. (SKIP)"); } } } else { $columns = $this->getReader($restore)->readAddColumn()->getColumns(); $this->execAddColumn($columns); } }
protected function alterChange($column, $current) { $line = array(); $line[] = $column->name; if ($current->isText() && $column->type !== null && !$column->isText()) { Sabel_Console::warning("cannot modify lob column '{$current->name}'. (SKIP)"); } elseif (!$current->isText()) { $col = $column->type === null ? $current : $column; $type = $this->getTypeDefinition($col, false); if ($col->isString()) { $max = $column->max === null ? $current->max : $column->max; $line[] = $type . "({$max})"; } else { $line[] = $type; } } if (($d = $column->default) === _NULL) { $line[] = "DEFAULT NULL"; } else { $cd = $current->default; if ($d === $cd) { $line[] = $this->getDefaultValue($current); } else { $this->valueCheck($column, $d); $line[] = $this->getDefaultValue($column); } } if ($current->nullable === true && $column->nullable === false) { $line[] = "NOT NULL"; } elseif ($current->nullable === false && $column->nullable === true) { $line[] = "NULL"; } return implode(" ", $line); }
private function createOptions() { $args = $this->arguments; if (Sabel_Console::hasOption("f", $args)) { $this->fileName = Sabel_Console::getOption("f", $args); } /* if (Sabel_Console::hasOption("dl", $args)) { $this->defaultLocale = Sabel_Console::getOption("dl", $args); } */ if (Sabel_Console::hasOption("l", $args)) { $idx = array_search("-l", $args, true); for ($i = ++$idx, $c = count($args); $i < $c; $i++) { if ($args[$i][0] === "-") { break; } $this->locales[] = $args[$i]; } } $this->arguments = $args; }
protected function defineMigrationDirectory() { if (Sabel_Console::hasOption("d", $this->arguments)) { $dir = Sabel_Console::getOption("d", $this->arguments); } else { $dir = RUN_BASE . DS . "migration" . DS . $this->getConnectionName(); } if (!is_dir($dir)) { $this->error("no such directory '{$dir}'."); exit; } Sabel_Db_Migration_Manager::setDirectory($dir); return $dir; }