Exemple #1
0
 /**
  * Create the new seeder.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->bootstrap($input, $output);
     // get the seed path from the config
     $path = $this->getConfig()->getSeedPath();
     if (!file_exists($path)) {
         $helper = $this->getHelper('question');
         $question = $this->getCreateSeedDirectoryQuestion();
         if ($helper->ask($input, $output, $question)) {
             mkdir($path, 0755, true);
         }
     }
     $this->verifySeedDirectory($path);
     $path = realpath($path);
     $className = $input->getArgument('name');
     if (!Util::isValidPhinxClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The seed class name "%s" is invalid. Please use CamelCase format', $className));
     }
     // Compute the file path
     $filePath = $path . DIRECTORY_SEPARATOR . $className . '.php';
     if (is_file($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', basename($filePath)));
     }
     // inject the class names appropriate to this seeder
     $contents = file_get_contents($this->getSeedTemplateFilename());
     $classes = array('$useClassName' => 'Phinx\\Seed\\AbstractSeed', '$className' => $className, '$baseClassName' => 'AbstractSeed');
     $contents = strtr($contents, $classes);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     $output->writeln('<info>using seed base class</info> ' . $classes['$useClassName']);
     $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
 }
Exemple #2
0
 public function testisValidPhinxClassName()
 {
     $expectedResults = array('CAmelCase' => false, 'CreateUserTable' => true, 'UserSeeder' => true, 'Test' => true, 'test' => false);
     foreach ($expectedResults as $input => $expectedResult) {
         $this->assertEquals($expectedResult, Util::isValidPhinxClassName($input));
     }
 }
Exemple #3
0
 /**
  * 创建一个新的seeder
  *
  * @param array $args 参数
  * @param array $options 选项
  */
 public function execute(array $args, array $options = [])
 {
     $this->bootstrap($args, $options);
     // get the seed path from the config
     $path = $this->getConfig()->getSeedPath();
     if (!file_exists($path)) {
         $ask = new Dialog();
         if ($ask->confirm(Colour::colour('Create seeds directory?', [Colour::RED, Colour::HIGHLIGHT]))) {
             mkdir($path, 0755, true);
         }
     }
     $this->verifySeedDirectory($path);
     $path = realpath($path);
     $className = $args[0];
     if (!Util::isValidPhinxClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The seed class name "%s" is invalid. Please use CamelCase format', $className));
     }
     // Compute the file path
     $filePath = $path . DIRECTORY_SEPARATOR . $className . '.php';
     if (is_file($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', basename($filePath)));
     }
     // inject the class names appropriate to this seeder
     $contents = file_get_contents($this->getSeedTemplateFilename());
     $classes = ['$useClassName' => 'Phinx\\Seed\\AbstractSeed', '$className' => $className, '$baseClassName' => 'AbstractSeed'];
     $contents = strtr($contents, $classes);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     Output::writeln('using seed base class ' . $classes['$useClassName']);
     Output::writeln('created ' . str_replace(Cml::getApplicationDir('secure_src'), '{secure_src}', $filePath));
 }
Exemple #4
0
 /**
  * Create the new migration.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->bootstrap($input, $output);
     // get the migration path from the config
     $path = $this->getConfig()->getMigrationPath();
     if (!file_exists($path)) {
         $helper = $this->getHelper('question');
         $question = $this->getCreateMigrationDirectoryQuestion();
         if ($helper->ask($input, $output, $question)) {
             mkdir($path, 0755, true);
         }
     }
     $this->verifyMigrationDirectory($path);
     $path = realpath($path);
     $className = $input->getArgument('name');
     if (!Util::isValidPhinxClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
     }
     if (!Util::isUniqueMigrationClassName($className, $path)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" already exists', $className));
     }
     // Compute the file path
     $fileName = Util::mapClassNameToFileName($className);
     $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
     if (is_file($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
     }
     // Get the alternative template and static class options, but only allow one of them.
     $altTemplate = $input->getOption('template');
     if (!$altTemplate) {
         $altTemplate = $this->getConfig()->getTemplateFile();
     }
     $creationClassName = $input->getOption('class');
     if (!$creationClassName) {
         $creationClassName = $this->getConfig()->getTemplateClass();
     }
     if ($altTemplate && $creationClassName) {
         throw new \InvalidArgumentException('Cannot use --template and --class at the same time');
     }
     // Verify the alternative template file's existence.
     if ($altTemplate && !is_file($altTemplate)) {
         throw new \InvalidArgumentException(sprintf('The alternative template file "%s" does not exist', $altTemplate));
     }
     // Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
     $aliasedClassName = null;
     if ($creationClassName) {
         // Supplied class does not exist, is it aliased?
         if (!class_exists($creationClassName)) {
             $aliasedClassName = $this->getConfig()->getAlias($creationClassName);
             if ($aliasedClassName && !class_exists($aliasedClassName)) {
                 throw new \InvalidArgumentException(sprintf('The class "%s" via the alias "%s" does not exist', $aliasedClassName, $creationClassName));
             } elseif (!$aliasedClassName) {
                 throw new \InvalidArgumentException(sprintf('The class "%s" does not exist', $creationClassName));
             }
         }
         // Does the class implement the required interface?
         if (!$aliasedClassName && !is_subclass_of($creationClassName, self::CREATION_INTERFACE)) {
             throw new \InvalidArgumentException(sprintf('The class "%s" does not implement the required interface "%s"', $creationClassName, self::CREATION_INTERFACE));
         } elseif ($aliasedClassName && !is_subclass_of($aliasedClassName, self::CREATION_INTERFACE)) {
             throw new \InvalidArgumentException(sprintf('The class "%s" via the alias "%s" does not implement the required interface "%s"', $aliasedClassName, $creationClassName, self::CREATION_INTERFACE));
         }
     }
     // Use the aliased class.
     $creationClassName = $aliasedClassName ?: $creationClassName;
     // Determine the appropriate mechanism to get the template
     if ($creationClassName) {
         // Get the template from the creation class
         $creationClass = new $creationClassName();
         $contents = $creationClass->getMigrationTemplate();
     } else {
         // Load the alternative template if it is defined.
         $contents = file_get_contents($altTemplate ?: $this->getMigrationTemplateFilename());
     }
     // inject the class names appropriate to this migration
     $classes = array('$useClassName' => $this->getConfig()->getMigrationBaseClassName(false), '$className' => $className, '$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true));
     $contents = strtr($contents, $classes);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     // Do we need to do the post creation call to the creation class?
     if ($creationClassName) {
         $creationClass->postMigrationCreation($filePath, $className, $this->getConfig()->getMigrationBaseClassName());
     }
     $output->writeln('<info>using migration base class</info> ' . $classes['$useClassName']);
     if (!empty($altTemplate)) {
         $output->writeln('<info>using alternative template</info> ' . $altTemplate);
     } elseif (!empty($creationClassName)) {
         $output->writeln('<info>using template creation class</info> ' . $creationClassName);
     } else {
         $output->writeln('<info>using default template</info>');
     }
     $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
 }
Exemple #5
0
 /**
  * {@inheritDoc}
  */
 public function fileName($name)
 {
     $name = $this->getMigrationName($name);
     return Util::getCurrentTimestamp() . '_' . Inflector::camelize($name) . '.php';
 }
 protected function checkUniqueMigrationClassName($class_name, $path)
 {
     if (!Util::isUniqueMigrationClassName($class_name, $path)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" already exists', $class_name));
     }
 }
Exemple #7
0
 /**
  * Gets an array of database seeders.
  *
  * @throws \InvalidArgumentException
  * @return AbstractSeed[]
  */
 public function getSeeds()
 {
     if (null === $this->seeds) {
         $config = $this->getConfig();
         $phpFiles = glob($config->getSeedPath() . DIRECTORY_SEPARATOR . '*.php');
         // filter the files to only get the ones that match our naming scheme
         $fileNames = array();
         /** @var AbstractSeed[] $seeds */
         $seeds = array();
         foreach ($phpFiles as $filePath) {
             if (Util::isValidSeedFileName(basename($filePath))) {
                 // convert the filename to a class name
                 $class = pathinfo($filePath, PATHINFO_FILENAME);
                 $fileNames[$class] = basename($filePath);
                 // load the seed file
                 /** @noinspection PhpIncludeInspection */
                 require_once $filePath;
                 if (!class_exists($class)) {
                     throw new \InvalidArgumentException(sprintf('Could not find class "%s" in file "%s"', $class, $filePath));
                 }
                 // instantiate it
                 $seed = new $class($this->getInput(), $this->getOutput());
                 if (!$seed instanceof AbstractSeed) {
                     throw new \InvalidArgumentException(sprintf('The class "%s" in file "%s" must extend \\Phinx\\Seed\\AbstractSeed', $class, $filePath));
                 }
                 $seeds[$class] = $seed;
             }
         }
         ksort($seeds);
         $this->setSeeds($seeds);
     }
     return $this->seeds;
 }
 /**
  * {@inheritDoc}
  */
 public function main($name = null)
 {
     $this->__timestamp = Util::getCurrentTimestamp();
     parent::main($name);
 }
 /**
  * {@inheritDoc}
  */
 public function fileName($name)
 {
     $name = $this->getMigrationName($name);
     return Util::mapClassNameToFileName($name);
 }
Exemple #10
0
 protected function mapClassNameToFileName($class_name)
 {
     return Util::mapClassNameToFileName($class_name);
 }
Exemple #11
0
 /**
  * 创建一个迁移
  *
  * @param array $args 参数
  * @param array $options 选项
  */
 public function execute(array $args, array $options = [])
 {
     $className = $args[0];
     $this->bootstrap($args, $options);
     if (!Util::isValidPhinxClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
     }
     // get the migration path from the config
     $path = $this->getConfig()->getMigrationPath();
     if (!is_dir($path)) {
         $ask = new Dialog();
         if ($ask->confirm(Colour::colour('Create migrations directory?', [Colour::RED, Colour::HIGHLIGHT]))) {
             mkdir($path, 0755, true);
         }
     }
     $this->verifyMigrationDirectory($path);
     $path = realpath($path);
     if (!Util::isUniqueMigrationClassName($className, $path)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" already exists', $className));
     }
     // Compute the file path
     $fileName = Util::mapClassNameToFileName($className);
     $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
     if (is_file($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
     }
     // Get the alternative template and static class options from the command line, but only allow one of them.
     $altTemplate = $options['template'];
     $creationClassName = $options['class'];
     if ($altTemplate && $creationClassName) {
         throw new \InvalidArgumentException('Cannot use --template and --class at the same time');
     }
     // Verify the alternative template file's existence.
     if ($altTemplate && !is_file($altTemplate)) {
         throw new \InvalidArgumentException(sprintf('The alternative template file "%s" does not exist', $altTemplate));
     }
     if ($creationClassName) {
         // Supplied class does not exist, is it aliased?
         if (!class_exists($creationClassName)) {
             throw new \InvalidArgumentException(sprintf('The class "%s" does not exist', $creationClassName));
         }
         // Does the class implement the required interface?
         if (!is_subclass_of($creationClassName, self::CREATION_INTERFACE)) {
             throw new \InvalidArgumentException(sprintf('The class "%s" does not implement the required interface "%s"', $creationClassName, self::CREATION_INTERFACE));
         }
     }
     // Determine the appropriate mechanism to get the template
     if ($creationClassName) {
         // Get the template from the creation class
         $creationClass = new $creationClassName();
         $contents = $creationClass->getMigrationTemplate();
     } else {
         // Load the alternative template if it is defined.
         $contents = file_get_contents($altTemplate ?: $this->getMigrationTemplateFilename());
     }
     // inject the class names appropriate to this migration
     $classes = ['$useClassName' => $this->getConfig()->getMigrationBaseClassName(false), '$className' => $className, '$version' => Util::getVersionFromFileName($fileName), '$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true)];
     $contents = strtr($contents, $classes);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     // Do we need to do the post creation call to the creation class?
     if ($creationClassName) {
         $creationClass->postMigrationCreation($filePath, $className, $this->getConfig()->getMigrationBaseClassName());
     }
     Output::writeln('using migration base class ' . Colour::colour($classes['$useClassName'], Colour::GREEN));
     if (!empty($altTemplate)) {
         Output::writeln('using alternative template ' . Colour::colour($altTemplate, Colour::GREEN));
     } elseif (!empty($creationClassName)) {
         Output::writeln('using template creation class ' . Colour::colour($creationClassName, Colour::GREEN));
     } else {
         Output::writeln('using default template');
     }
     Output::writeln('created ' . str_replace(Cml::getApplicationDir('secure_src'), '{secure_src}', $filePath));
 }
 protected function createMigrationFile($entityClass)
 {
     $baseClassName = $this->getClassNameFromFQCN($this->baseMigrationClass);
     $className = $this->getClassNameFromFQCN($entityClass) . "Migration";
     // validate class name
     if (!Util::isValidPhinxClassName($className) || !Util::isUniqueMigrationClassName($className, $this->outputDirectory)) {
         throw new MigrationGenerationException("The class name '{$className}' is invalid or already exists");
     }
     $fileName = Util::mapClassNameToFileName($className);
     // create tableDefinitions string
     $tableDefinition = implode("\n\n        ", $this->tables);
     // inject data into the template
     $template = file_get_contents($this->templateFile);
     $template = str_replace("{{baseFQCN}}", $this->baseMigrationClass, $template);
     $template = str_replace("{{baseClass}}", $baseClassName, $template);
     $template = str_replace("{{className}}", $className, $template);
     $template = str_replace("{{tableDefinition}}", $tableDefinition, $template);
     $filePath = $this->outputDirectory . "/" . $fileName;
     // write the file
     file_put_contents($filePath, $template);
     return $filePath;
 }