diff --git a/CHANGELOG.md b/CHANGELOG.md index 90844d15..3750b1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ### [Unreleased][unreleased] +#### Added +- config options template and indent + ### [2.0.0] - 2022-01-24 #### Changed - use utf8mb4 as default charset for mysql (fix but BC break, use e.g. `$this->changeCollation('utf8mb4_general_ci')` to change all tables and fields to it) diff --git a/config_example/phoenix.neon b/config_example/phoenix.neon index 15bf67c8..6bb89e72 100644 --- a/config_example/phoenix.neon +++ b/config_example/phoenix.neon @@ -7,13 +7,13 @@ environments: host: localhost port: 3306 # optional username: root - password: 123 + password: '123' db_name: phoenix charset: utf8mb4 # optional pgsql: adapter: pgsql host: localhost username: postgres - password: 123 + password: '123' db_name: phoenix charset: utf8 diff --git a/docs/configuration/index.md b/docs/configuration/index.md index 8f039957..8f909bd6 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -9,6 +9,8 @@ Configuration array can consist of five parts: - `environments` - array - list of environment where migrations are executed, e.g. local, staging, production etc. - `default_environment` - string - environment which is used in phoenix command if `--environment` option is not set. It has to be one of keys from `environments` array. If no `default_environment` is set, first of `environments` is used. - `dependencies` - array - list of dependencies which can be used in __construct of Migration classes. Key is type of dependency (class or interface name which will be used in __construct) and value is object of this type +- `template` - string - path to template file for migrations (used in create, dump and diff commands) +- `indent` - string - indentation in created migrations. Available values: 2spaces, 3spaces, 4spaces, 5spaces, tab [default: 4spaces] ### Example Let's say you want to create configuration file, where `log_table_name` is "my_phoenix_log", you have two `migration_dirs` (first and second, which are located in the same directory as configuration file), also two `environments` both uses mysql adapter, and your `default_environment` is "local". Now we show you, how this config looks like using different type of configuration files: diff --git a/src/Command/AbstractDumpCommand.php b/src/Command/AbstractDumpCommand.php index f4b65df3..13053382 100644 --- a/src/Command/AbstractDumpCommand.php +++ b/src/Command/AbstractDumpCommand.php @@ -20,7 +20,7 @@ protected function configure(): void { $this ->addOption('ignore-tables', null, InputOption::VALUE_REQUIRED, 'Comma separated list of tables to ignore (Structure and data).', 'phoenix_log') - ->addOption('indent', 'i', InputOption::VALUE_REQUIRED, 'Indentation. Available values: 2spaces, 3spaces, 4spaces, 5spaces, tab', '4spaces') + ->addOption('indent', 'i', InputOption::VALUE_REQUIRED, 'Indentation. Available values: 2spaces, 3spaces, 4spaces, 5spaces, tab') ->addOption('migration', null, InputOption::VALUE_REQUIRED, 'Name of migration', $this->migrationDefaultName()) ->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Directory to create migration in') ->addOption('template', null, InputOption::VALUE_REQUIRED, 'Path to template') @@ -33,14 +33,14 @@ protected function runCommand(): void { $indenter = new Indenter(); /** @var string $indentOption */ - $indentOption = $this->input->getOption('indent'); + $indentOption = $this->input->getOption('indent') ?: $this->getConfig()->getIndent(); $indent = $indenter->indent($indentOption); $dumper = $this->createDumper($indent); /** @var string $migration */ $migration = $this->input->getOption('migration') ?: $this->migrationDefaultName(); /** @var string $template */ - $template = $this->input->getOption('template'); + $template = $this->input->getOption('template') ?: $this->getConfig()->getTemplate(); $migrationCreator = new MigrationCreator($migration, $indent, $template); $sourceStructure = $this->sourceStructure(); diff --git a/src/Command/CreateCommand.php b/src/Command/CreateCommand.php index c686d5e4..33245430 100644 --- a/src/Command/CreateCommand.php +++ b/src/Command/CreateCommand.php @@ -25,20 +25,20 @@ protected function configure(): void ->addArgument('migration', InputArgument::REQUIRED, 'Name of migration') ->addArgument('dir', InputArgument::OPTIONAL, 'Directory to create migration in') ->addOption('template', null, InputOption::VALUE_REQUIRED, 'Path to template') - ->addOption('indent', 'i', InputOption::VALUE_REQUIRED, 'Indentation. Available values: 2spaces, 3spaces, 4spaces, 5spaces, tab', '4spaces'); + ->addOption('indent', 'i', InputOption::VALUE_REQUIRED, 'Indentation. Available values: 2spaces, 3spaces, 4spaces, 5spaces, tab'); } protected function runCommand(): void { $indenter = new Indenter(); /** @var string $indentOption */ - $indentOption = $this->input->getOption('indent'); + $indentOption = $this->input->getOption('indent') ?: $this->getConfig()->getIndent(); $indent = $indenter->indent($indentOption); /** @var string $migration */ $migration = $this->input->getArgument('migration'); - /** @var string|null $template */ - $template = $this->input->getOption('template'); + /** @var string $template */ + $template = $this->input->getOption('template') ?: $this->getConfig()->getTemplate(); $migrationCreator = new MigrationCreator($migration, $indent, $template); /** @var string|null $dir */ diff --git a/src/Config/Config.php b/src/Config/Config.php index 2327bc23..481c0751 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -18,6 +18,8 @@ final class Config 'environments' => [], 'default_environment' => '', 'dependencies' => [], + 'template' => __DIR__ . '/../Templates/DefaultTemplate.phoenix', + 'indent' => '4spaces', ]; /** @@ -99,4 +101,14 @@ public function getDependency(string $type): object } throw new InvalidArgumentValueException('Dependency for type "' . $type . '" not found. Register it via $configuration[\'dependencies\'][\'' . $type . '\']'); } + + public function getTemplate(): string + { + return $this->configuration['template']; + } + + public function getIndent(): string + { + return $this->configuration['indent']; + } }