123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace catcher\command;
- use catcher\CatchAdmin;
- use Phinx\Util\Util;
- use think\console\Input;
- use think\console\input\Argument as InputArgument;
- use think\console\Output;
- use think\exception\InvalidArgumentException;
- use think\migration\command\migrate\Create;
- class MigrateCreateCommand extends Create
- {
-
- protected function configure()
- {
- $this->setName('catch-migrate:create')
- ->setDescription('Create a new migration')
- ->addArgument('module', InputArgument::REQUIRED, 'the module where you create')
- ->addArgument('name', InputArgument::REQUIRED, 'What is the name of the migration?')
- ->setHelp(sprintf('%sCreates a new database migration%s', PHP_EOL, PHP_EOL));
- }
-
- protected function execute(Input $input, Output $output)
- {
- $module = $input->getArgument('module');
- $className = $input->getArgument('name');
- $path = $this->create($module, $className);
- $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', realpath($path)));
- }
-
- protected function create($module, $className): string
- {
- $path = CatchAdmin::makeDirectory(CatchAdmin::moduleMigrationsDirectory($module));
- 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));
- }
-
- $fileName = Util::mapClassNameToFileName($className);
- $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
- if (is_file($filePath)) {
- throw new InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
- }
-
- $aliasedClassName = null;
-
- $contents = file_get_contents($this->getTemplate());
-
- $contents = strtr($contents, [
- 'MigratorClass' => $className,
- ]);
- if (false === file_put_contents($filePath, $contents)) {
- throw new RuntimeException(sprintf('The file "%s" could not be written to', $path));
- }
- return $filePath;
- }
- protected function getTemplate()
- {
- return __DIR__ . '/stubs/migrate.stub';
- }
- }
|