Skip to content

Commit

Permalink
暴露Creator接口 使扩展里可以生成迁移脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Apr 16, 2019
1 parent aa4fcab commit c3d04e8
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 61 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
}
},
"require": {
"topthink/framework": "^6.0",
"topthink/framework": "^6.0.0",
"topthink/think-helper": "^3.0.3"
},
"minimum-stability": "dev",
"suggest": {
"fzaninotto/faker": "Required to use the factory builder (^1.8)."
},
Expand Down
5 changes: 2 additions & 3 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use InvalidArgumentException;
use Phinx\Db\Adapter\AdapterFactory;
use think\facade\Db;

abstract class Command extends \think\console\Command
{
Expand Down Expand Up @@ -42,7 +41,7 @@ public function getAdapter()
*/
protected function getDbConfig()
{
$config = Db::getConfig();
$config = $this->app->config->get('database');

if (0 == $config['deploy']) {
$dbConfig = [
Expand All @@ -68,7 +67,7 @@ protected function getDbConfig()
];
}

$dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $this->app->config->get('database.migration_table', 'migrations');
$dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . ($config['migration_table'] ?? 'migrations');

return $dbConfig;
}
Expand Down
77 changes: 77 additions & 0 deletions src/Creator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace think\migration;

use InvalidArgumentException;
use Phinx\Util\Util;
use RuntimeException;
use think\App;

class Creator
{

protected $app;

public function __construct(App $app)
{
$this->app = $app;
}

public function create($className)
{
$path = $this->ensureDirectory();

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));
}

// Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
$aliasedClassName = null;

// Load the alternative template if it is defined.
$contents = file_get_contents($this->getTemplate());

// inject the class names appropriate to this migration
$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 ensureDirectory()
{
$path = $this->app->getRootPath() . 'database' . DIRECTORY_SEPARATOR . 'migrations';

if (!is_dir($path) && !mkdir($path, 0755, true)) {
throw new InvalidArgumentException(sprintf('directory "%s" does not exist', $path));
}

if (!is_writable($path)) {
throw new InvalidArgumentException(sprintf('directory "%s" is not writable', $path));
}

return $path;
}

protected function getTemplate()
{
return __DIR__ . '/command/stubs/migrate.stub';
}
}
2 changes: 2 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function boot()
return (new Factory($this->app->make(FakerGenerator::class)))->load($this->app->getRootPath() . 'database/factories/');
});

$this->app->bind('migration.creator', Creator::class);

$this->commands([
MigrateCreate::class,
MigrateRun::class,
Expand Down
4 changes: 1 addition & 3 deletions src/command/factory/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ protected function handle()
$path = $this->getPath();

if (!file_exists($path)) {
if ($this->output->confirm($this->input, 'Create factories directory? [y]/n')) {
mkdir($path, 0755, true);
}
mkdir($path, 0755, true);
}

if (!is_dir($path)) {
Expand Down
62 changes: 11 additions & 51 deletions src/command/migrate/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

namespace think\migration\command\migrate;

use Phinx\Util\Util;
use InvalidArgumentException;
use RuntimeException;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument as InputArgument;
use think\console\Output;
use think\migration\command\Migrate;
use think\migration\Creator;

class Create extends Migrate
class Create extends Command
{

/**
Expand All @@ -35,61 +37,19 @@ protected function configure()
* @param Input $input
* @param Output $output
* @return void
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws InvalidArgumentException
* @throws RuntimeException
*/
protected function execute(Input $input, Output $output)
{
$path = $this->getPath();
/** @var Creator $creator */
$creator = $this->app->get('migration.creator');

if (!file_exists($path)) {
if ($this->output->confirm($this->input, 'Create migrations directory? [y]/n')) {
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));
}

// Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
$aliasedClassName = null;
$path = $creator->create($className);

// Load the alternative template if it is defined.
$contents = file_get_contents($this->getTemplate());

// inject the class names appropriate to this migration
$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));
}

$output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
}

protected function getTemplate()
{
return __DIR__ . '/../stubs/migrate.stub';
$output->writeln('<info>created</info> .' . str_replace(getcwd(), '', realpath($path)));
}

}
4 changes: 1 addition & 3 deletions src/command/seed/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ protected function execute(Input $input, Output $output)
$path = $this->getPath();

if (!file_exists($path)) {
if ($this->output->confirm($this->input, 'Create seeds directory? [y]/n')) {
mkdir($path, 0755, true);
}
mkdir($path, 0755, true);
}

$this->verifyMigrationDirectory($path);
Expand Down

0 comments on commit c3d04e8

Please sign in to comment.