Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to define namespace of Symfony Console Command #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Bridges/NetteDI/MigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MigrationsExtension extends Nette\DI\CompilerExtension
'diffGenerator' => TRUE, // false|doctrine
'withDummyData' => FALSE,
'ignoredQueriesFile' => NULL,
'commandNamespace' => Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand::DEFAULT_NAMESPACE,
];

/** @var array */
Expand Down Expand Up @@ -78,11 +79,11 @@ public function loadConfiguration()
}

Validators::assertField($config, 'groups', 'array');
$groups = $this->createGroupDefinitions($config['groups']);
$this->createGroupDefinitions($config['groups']);

// extensionHandlers
Validators::assertField($config, 'phpParams', 'array');
$extensionHandlers = $this->createExtensionHandlerDefinitions($driver, $config['phpParams']);
$this->createExtensionHandlerDefinitions($driver, $config['phpParams']);

// configuration
$configuration = $this->createConfigurationDefinition();
Expand Down Expand Up @@ -142,7 +143,7 @@ public function beforeCompile()
}

$builder->getDefinition($this->prefix('configuration'))
->setArguments([$groups, $extensionHandlers]);
->setArguments([$groups, $extensionHandlers, $config['commandNamespace']]);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Nextras\Migrations\Bridges\SymfonyBundle\DependencyInjection;

use Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -27,6 +28,9 @@ public function getConfigTreeBuilder()
}

$rootNode->children()
->scalarNode('command_namespace')
->defaultValue(BaseCommand::DEFAULT_NAMESPACE)
->end()
->scalarNode('dir')
->defaultValue('%kernel.project_dir%/migrations')
->cannotBeEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function load(array $configs, ContainerBuilder $container)
}

$configurationDefinition = new Definition('Nextras\Migrations\Configurations\DefaultConfiguration');
$configurationDefinition->setArguments([$config['dir'], $driverDefinition, $config['with_dummy_data'], $config['php_params']]);
$configurationDefinition->setArguments([$config['dir'], $driverDefinition, $config['with_dummy_data'], $config['php_params'], $config['command_namespace']]);
$configurationDefinition->addMethodCall('setStructureDiffGenerator', [$structureDiffGeneratorDefinition]);

$continueCommandDefinition = new Definition('Nextras\Migrations\Bridges\SymfonyConsole\ContinueCommand');
Expand Down
11 changes: 11 additions & 0 deletions src/Bridges/SymfonyConsole/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

abstract class BaseCommand extends Command
{
const DEFAULT_NAMESPACE = 'migrations';

/** @var IDriver */
protected $driver;

Expand Down Expand Up @@ -51,4 +53,13 @@ protected function runMigrations($mode, $config)
return 0;
}

/**
* @param string $name
* @return BaseCommand
*/
public function setName($name)
{
return parent::setName($this->config->getCommandNamespace() . ":$name");
}

}
6 changes: 1 addition & 5 deletions src/Bridges/SymfonyConsole/ContinueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

class ContinueCommand extends BaseCommand
{
/** @var string */
protected static $defaultName = 'migrations:continue';


protected function configure()
{
$this->setName(self::$defaultName);
$this->setName('continue');
$this->setDescription('Updates database schema by running all new migrations');
$this->setHelp("If table 'migrations' does not exist in current database, it is created automatically.");
}
Expand Down
5 changes: 1 addition & 4 deletions src/Bridges/SymfonyConsole/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class CreateCommand extends BaseCommand
const CONTENT_SOURCE_STDIN = 'stdin';
const CONTENT_SOURCE_EMPTY = 'empty';

/** @var string */
protected static $defaultName = 'migrations:create';

/** @var string */
protected $defaultContentSource = self::CONTENT_SOURCE_DIFF;

Expand All @@ -47,7 +44,7 @@ public function setDefaultContentSource($defaultContentSource)
*/
protected function configure()
{
$this->setName(self::$defaultName);
$this->setName('create');
$this->setDescription('Creates new migration file with proper name (e.g. 2015-03-14-130836-label.sql)');
$this->setHelp('Prints path of the created file to standard output.');

Expand Down
5 changes: 1 addition & 4 deletions src/Bridges/SymfonyConsole/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

class ResetCommand extends BaseCommand
{
/** @var string */
protected static $defaultName = 'migrations:reset';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default name should not be removed, even if not used in configure() because it breaks lazy loading.

Also commands with custom name can support lazy loading. In case of contributte/console could be added service tag $service->addTag('console.command', 'custom:command:name'). Not sure about kdyby/console and symfony DI integrations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I wanted to not remove this static property but symfony-bundle test failed on a weird notice like "undefined index continue" in Symfony/Application.php.

I didn't know this is symfony "build-in feature" from v3.4.10 and I have the same opinion like https://twitter.com/Majkl578/status/1214523751603286016

Any ideas how to resolve it? I agree that breaking lazy loading is not good.

protected function configure()
{
$this->setName(self::$defaultName);
$this->setName('reset');
$this->setDescription('Drops current database and recreates it from scratch');
$this->setHelp("Drops current database and runs all migrations");
}
Expand Down
16 changes: 14 additions & 2 deletions src/Configurations/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Nextras\Migrations\Configurations;

use Nette\Utils\Validators;
use Nextras\Migrations\Entities\Group;
use Nextras\Migrations\IConfiguration;
use Nextras\Migrations\IExtensionHandler;
Expand All @@ -26,15 +25,20 @@ class Configuration implements IConfiguration
/** @var IExtensionHandler[] (extension => IExtensionHandler) */
private $extensionHandlers;

/** @var string */
private $commandNamespace;


/**
* @param Group[] $groups
* @param IExtensionHandler[] $extensionHandlers (extension => IExtensionHandler)
* @param string $commandNamespace
*/
public function __construct(array $groups, array $extensionHandlers)
public function __construct(array $groups, array $extensionHandlers, $commandNamespace)
{
$this->groups = $groups;
$this->extensionHandlers = $extensionHandlers;
$this->commandNamespace = $commandNamespace;
}


Expand All @@ -54,4 +58,12 @@ public function getExtensionHandlers()
{
return $this->extensionHandlers;
}

/**
* @return string
*/
public function getCommandNamespace()
{
return $this->commandNamespace;
}
}
15 changes: 14 additions & 1 deletion src/Configurations/DefaultConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Nextras\Migrations\Configurations;

use Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand;
use Nextras\Migrations\Entities\Group;
use Nextras\Migrations\Extensions\PhpHandler;
use Nextras\Migrations\Extensions\SqlHandler;
Expand Down Expand Up @@ -48,19 +49,24 @@ class DefaultConfiguration implements IConfiguration
/** @var IDiffGenerator|NULL */
protected $dummyDataDiffGenerator;

/** @var string */
protected $commandNamespace;


/**
* @param string $dir
* @param IDriver $driver
* @param bool $withDummyData
* @param array $phpParams
* @param string $commandNamespace
*/
public function __construct($dir, IDriver $driver, $withDummyData = TRUE, array $phpParams = [])
public function __construct($dir, IDriver $driver, $withDummyData = TRUE, array $phpParams = [], $commandNamespace = BaseCommand::DEFAULT_NAMESPACE)
{
$this->dir = $dir;
$this->driver = $driver;
$this->withDummyData = $withDummyData;
$this->phpParams = $phpParams;
$this->commandNamespace = $commandNamespace;
}


Expand Down Expand Up @@ -132,4 +138,11 @@ public function setDummyDataDiffGenerator(IDiffGenerator $generator = NULL)
$this->dummyDataDiffGenerator = $generator;
}

/**
* @return string
*/
public function getCommandNamespace()
{
return $this->commandNamespace;
}
}
4 changes: 4 additions & 0 deletions src/IConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public function getGroups();
*/
public function getExtensionHandlers();

/**
* @return string
*/
public function getCommandNamespace();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extensions:
migrationsA: Nextras\Migrations\Bridges\NetteDI\MigrationsExtension
migrationsB: Nextras\Migrations\Bridges\NetteDI\MigrationsExtension

migrationsA:
dir: migrationsA
driver: mysql
dbal: Nextras\Migrations\Bridges\Dibi\DibiAdapter(Dibi\Connection(%dibiConfig%))

migrationsB:
dir: migrationsB
driver: mysql
dbal: Nextras\Migrations\Bridges\Dibi\DibiAdapter(Dibi\Connection(%dibiConfig%))
commandNamespace: fooCommandNamespace
19 changes: 19 additions & 0 deletions tests/cases/integration/nette-di/MigrationsExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace NextrasTests\Migrations;

use Nette;
use Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand;
use Tester\Assert;
use Tester\Environment;
use Tester\TestCase;
Expand Down Expand Up @@ -100,6 +101,24 @@ class MigrationsExtensionTest extends TestCase
}


public function testCommandNamespace()
{
$container = $this->createContainer('commandNamespace');

$commands = $container->findByType('Symfony\Component\Console\Command\Command');
foreach ($commands as $name) {
/** @var BaseCommand $command */
$command = $container->getService($name);

if (Nette\Utils\Strings::contains($name, 'migrationsA.')) {
Assert::contains('migrations:', $command->getName(), 'Default command namespace failed.');
} else {
Assert::contains('fooCommandNamespace:', $command->getName());
}
}
}


/**
* @param string $config
* @return Nette\DI\Container
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/integration/symfony-bundle/SymfonyBundleTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SymfonyBundleTest extends TestCase
{
$application = new Application($this->symfonyKernel);

$command = $application->find('migrations:reset');
$command = $application->find('fooCommandNamespace:reset');
$commandTester = new CommandTester($command);
Assert::same(0, $commandTester->execute([]));
}
Expand All @@ -61,7 +61,7 @@ class SymfonyBundleTest extends TestCase
{
$application = new Application($this->symfonyKernel);

$command = $application->find('migrations:continue');
$command = $application->find('fooCommandNamespace:continue');
$commandTester = new CommandTester($command);
Assert::same(0, $commandTester->execute([]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ doctrine:
nextras_migrations:
driver: '%nextras_migrations_driver%'
dir: '%nextras_migrations_dir%'
command_namespace: fooCommandNamespace