Skip to content

Commit

Permalink
Added config options template and indent (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
lulco authored Mar 9, 2022
1 parent 1df86c4 commit 59368ba
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions config_example/phoenix.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/Command/AbstractDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 12 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ final class Config
'environments' => [],
'default_environment' => '',
'dependencies' => [],
'template' => __DIR__ . '/../Templates/DefaultTemplate.phoenix',
'indent' => '4spaces',
];

/**
Expand Down Expand Up @@ -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'];
}
}

0 comments on commit 59368ba

Please sign in to comment.