Beispiel #1
0
 /**
  * Migrate the database.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->_init($input, $output);
     $tarako_path = $this->getConfig()->getMigrationPath();
     if (!is_writeable($tarako_path)) {
         throw new \InvalidArgumentException(sprintf('"%s" への書き込み権限がありません', $tarako_path));
     }
     $tarako_path = realpath($tarako_path);
     $cls_name = $input->getArgument('name');
     if (!Util::isValidMigrationClassName($cls_name)) {
         throw new \InvalidArgumentException(sprintf('マイグレーションクラス名 "%s" は正しくありません。クラス名はパスカルケースで指定してください', $cls_name));
     }
     // Compute the file path
     $file_name = Util::mapClassNameToFileName($cls_name);
     $file_path = $tarako_path . DS . $file_name;
     if (file_exists($file_path)) {
         throw new \InvalidArgumentException(sprintf('"%s" は既に存在します', $file_path));
     }
     $contents = file_get_contents(ROOT . DS . 'data' . DS . 'template' . DS . 'Migration.template.php.dist');
     $contents = str_replace('$cls_name', $cls_name, $contents);
     if (false === file_put_contents($file_path, $contents)) {
         throw new \RuntimeException(sprintf('"%s" マイグレーションクラスを生成出来ませんでした', $file_path));
     }
     $this->output->writeln('<info>created</info> ' . $file_name);
 }
Beispiel #2
0
 /**
  * 指定パスのマイグレーションファイルからクラスを読み込む
  *
  * @param  string $file_path  マイグレーションファイルへのパス
  * @return AbstractMigration
  **/
 private function _fetchMigrationClass($file_path)
 {
     $file_name = basename($file_path);
     if (!preg_match('/([0-9]+)_([_a-z0-9]*).php/', $file_name, $matches)) {
         return false;
     }
     $version = $matches[1];
     $cls_name = Util::mapFileNameToClassName($file_name);
     /** @noinspection PhpIncludeInspection */
     require_once $file_path;
     if (!class_exists($cls_name)) {
         throw new \InvalidArgumentException(sprintf('"%s" クラスがマイグレーションファイル("%s")内に見つかりません', $cls_name, $file_path));
     }
     $migration = new $cls_name($version);
     if (!$migration instanceof AbstractMigration) {
         throw new \InvalidArgumentException(sprintf('The class "%s" in file "%s" must extend \\Phinx\\Migration\\AbstractMigration', $class, $filePath));
     }
     return $migration;
 }