Skip to content

Commit

Permalink
use ::class syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Jan 20, 2024
1 parent 43f0405 commit 02c8d28
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 80 deletions.
64 changes: 33 additions & 31 deletions src/Bridges/NetteDI/MigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
namespace Nextras\Migrations\Bridges\NetteDI;

use Doctrine;
use Dibi;
use Nette;
use Nette\DI\ContainerBuilder;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use Nextras;
use Symfony;


class MigrationsExtension extends Nette\DI\CompilerExtension
Expand All @@ -40,24 +42,24 @@ class MigrationsExtension extends Nette\DI\CompilerExtension

/** @var array */
protected $dbals = [
'dibi' => 'Nextras\Migrations\Bridges\Dibi\DibiAdapter',
'dibi3' => 'Nextras\Migrations\Bridges\Dibi\Dibi3Adapter',
'dibi4' => 'Nextras\Migrations\Bridges\Dibi\Dibi3Adapter',
'doctrine' => 'Nextras\Migrations\Bridges\DoctrineDbal\DoctrineAdapter',
'nette' => 'Nextras\Migrations\Bridges\NetteDatabase\NetteAdapter',
'nextras' => 'Nextras\Migrations\Bridges\NextrasDbal\NextrasAdapter',
'dibi' => Nextras\Migrations\Bridges\Dibi\DibiAdapter::class,
'dibi3' => Nextras\Migrations\Bridges\Dibi\Dibi3Adapter::class,
'dibi4' => Nextras\Migrations\Bridges\Dibi\Dibi3Adapter::class,
'doctrine' => Nextras\Migrations\Bridges\DoctrineDbal\DoctrineAdapter::class,
'nette' => Nextras\Migrations\Bridges\NetteDatabase\NetteAdapter::class,
'nextras' => Nextras\Migrations\Bridges\NextrasDbal\NextrasAdapter::class,
];

/** @var array */
protected $drivers = [
'mysql' => 'Nextras\Migrations\Drivers\MySqlDriver',
'pgsql' => 'Nextras\Migrations\Drivers\PgSqlDriver',
'mysql' => Nextras\Migrations\Drivers\MySqlDriver::class,
'pgsql' => Nextras\Migrations\Drivers\PgSqlDriver::class,
];

/** @var array */
protected $printers = [
'console' => 'Nextras\Migrations\Printers\Console',
'psrLog' => 'Nextras\Migrations\Bridges\PsrLog\PsrLogPrinter',
'console' => Nextras\Migrations\Printers\Console::class,
'psrLog' => Nextras\Migrations\Bridges\PsrLog\PsrLogPrinter::class,
];


Expand Down Expand Up @@ -101,7 +103,7 @@ public function loadConfiguration()
$configuration = $this->createConfigurationDefinition();

// commands
if (class_exists('Symfony\Component\Console\Command\Command')) {
if (class_exists(Symfony\Component\Console\Command\Command::class)) {
$this->createSymfonyCommandDefinitions($driver, $configuration, $printer);
}
}
Expand All @@ -113,13 +115,13 @@ public function beforeCompile()
$config = $this->validateConfig($this->defaults);

// dbal
foreach ($builder->findByType('Nextras\Migrations\IDbal') as $def) {
foreach ($builder->findByType(Nextras\Migrations\IDbal::class) as $def) {
$factory = $def->getFactory();
if ($factory->getEntity() !== 'Nextras\Migrations\Bridges\Dibi\DibiAdapter') {
if ($factory->getEntity() !== Nextras\Migrations\Bridges\Dibi\DibiAdapter::class) {
continue;
}

$conn = $builder->getByType('Dibi\Connection');
$conn = $builder->getByType(Dibi\Connection::class);
if (!$conn) {
continue;
}
Expand All @@ -129,7 +131,7 @@ public function beforeCompile()

// diff generator
if ($config['diffGenerator'] === true) {
if ($builder->findByType('Doctrine\ORM\EntityManagerInterface') && $builder->hasDefinition($this->prefix('group.structures'))) {
if ($builder->findByType(Doctrine\ORM\EntityManagerInterface::class) && $builder->hasDefinition($this->prefix('group.structures'))) {
Validators::assertField($config, 'ignoredQueriesFile', 'null|string');
$diffGenerator = $this->createDoctrineStructureDiffGeneratorDefinition($config['ignoredQueriesFile']);
$builder->getDefinition($this->prefix('group.structures'))
Expand Down Expand Up @@ -165,7 +167,7 @@ private function getDbalDefinition($dbal)
if ($factory) {
return $this->getContainerBuilder()
->addDefinition($this->prefix('dbal'))
->setClass('Nextras\Migrations\IDbal')
->setClass(Nextras\Migrations\IDbal::class)
->setFactory($factory);

} elseif ($dbal === null) {
Expand Down Expand Up @@ -201,7 +203,7 @@ private function getDriverDefinition($driver, $dbal)
if ($factory) {
return $this->getContainerBuilder()
->addDefinition($this->prefix('driver'))
->setClass('Nextras\Migrations\IDriver')
->setClass(Nextras\Migrations\IDriver::class)
->setFactory($factory);

} elseif ($driver === null) {
Expand Down Expand Up @@ -234,7 +236,7 @@ private function getPrinterDefinition($printer)
if ($factory) {
return $this->getContainerBuilder()
->addDefinition($this->prefix('printer'))
->setClass('Nextras\Migrations\IPrinter')
->setClass(Nextras\Migrations\IPrinter::class)
->setFactory($factory);

} elseif ($printer === null) {
Expand Down Expand Up @@ -320,7 +322,7 @@ private function createGroupDefinitions(array $groups)
$groupDefinitions[] = $builder->addDefinition($this->prefix("group.$serviceName"))
->addTag(self::TAG_GROUP, ['for' => [$this->name]])
->setAutowired(false)
->setClass('Nextras\Migrations\Entities\Group')
->setClass(Nextras\Migrations\Entities\Group::class)
->addSetup('$name', [$groupName])
->addSetup('$enabled', [$enabled])
->addSetup('$directory', [$directory])
Expand All @@ -339,12 +341,12 @@ private function createExtensionHandlerDefinitions($driver, $phpParams)
$sqlHandler = $builder->addDefinition($this->prefix('extensionHandler.sql'))
->addTag(self::TAG_EXTENSION_HANDLER, ['for' => [$this->name], 'extension' => 'sql'])
->setAutowired(false)
->setClass('Nextras\Migrations\Extensions\SqlHandler')
->setClass(Nextras\Migrations\Extensions\SqlHandler::class)
->setArguments([$driver]);

$phpHandler = $builder->addDefinition($this->prefix('extensionHandler.php'))
->addTag(self::TAG_EXTENSION_HANDLER, ['for' => [$this->name], 'extension' => 'php'])
->setClass('Nextras\Migrations\Extensions\PhpHandler')
->setClass(Nextras\Migrations\Extensions\PhpHandler::class)
->setAutowired(false)
->setArguments([$phpParams]);

Expand All @@ -356,8 +358,8 @@ private function createConfigurationDefinition()
{
return $this->getContainerBuilder()
->addDefinition($this->prefix('configuration'))
->setClass('Nextras\Migrations\IConfiguration')
->setFactory('Nextras\Migrations\Configurations\Configuration');
->setClass(Nextras\Migrations\IConfiguration::class)
->setFactory(Nextras\Migrations\Configurations\Configuration::class);
}


Expand All @@ -367,40 +369,40 @@ private function createDoctrineStructureDiffGeneratorDefinition($ignoredQueriesF

return $builder->addDefinition($this->prefix('diffGenerator.structures'))
->setAutowired(false)
->setClass('Nextras\Migrations\IDiffGenerator')
->setFactory('Nextras\Migrations\Bridges\DoctrineOrm\StructureDiffGenerator')
->setClass(Nextras\Migrations\IDiffGenerator::class)
->setFactory(Nextras\Migrations\Bridges\DoctrineOrm\StructureDiffGenerator::class)
->setArguments(['@Doctrine\ORM\EntityManagerInterface', $ignoredQueriesFile]);
}


private function createSymfonyCommandDefinitions($driver, $configuration, $printer)
{
$builder = $this->getContainerBuilder();
$builder->addExcludedClasses(['Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand']);
$builder->addExcludedClasses([Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand::class]);

$builder->addDefinition($this->prefix('continueCommand'))
->setClass('Nextras\Migrations\Bridges\SymfonyConsole\ContinueCommand')
->setClass(Nextras\Migrations\Bridges\SymfonyConsole\ContinueCommand::class)
->setArguments([$driver, $configuration, $printer])
->addTag('kdyby.console.command');

$builder->addDefinition($this->prefix('createCommand'))
->setClass('Nextras\Migrations\Bridges\SymfonyConsole\CreateCommand')
->setClass(Nextras\Migrations\Bridges\SymfonyConsole\CreateCommand::class)
->setArguments([$driver, $configuration, $printer])
->addTag('kdyby.console.command');

$builder->addDefinition($this->prefix('resetCommand'))
->setClass('Nextras\Migrations\Bridges\SymfonyConsole\ResetCommand')
->setClass(Nextras\Migrations\Bridges\SymfonyConsole\ResetCommand::class)
->setArguments([$driver, $configuration, $printer])
->addTag('kdyby.console.command');
}


private function filterArguments(array $arguments)
{
if (method_exists('Nette\DI\Helpers', 'filterArguments')) {
if (method_exists(Nette\DI\Helpers::class, 'filterArguments')) {
return Nette\DI\Helpers::filterArguments($arguments);

} elseif (method_exists('Nette\DI\Compiler', 'filterArguments')) {
} elseif (method_exists(Nette\DI\Compiler::class, 'filterArguments')) {
return Nette\DI\Compiler::filterArguments($arguments);

} else {
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;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -18,24 +19,24 @@ class NextrasMigrationsExtension extends Extension
{
/** @var array */
protected $dbals = [
'dibi' => 'Nextras\Migrations\Bridges\Dibi\DibiAdapter',
'dibi3' => 'Nextras\Migrations\Bridges\Dibi\Dibi3Adapter',
'dibi4' => 'Nextras\Migrations\Bridges\Dibi\Dibi3Adapter',
'doctrine' => 'Nextras\Migrations\Bridges\DoctrineDbal\DoctrineAdapter',
'nette' => 'Nextras\Migrations\Bridges\NetteDatabase\NetteAdapter',
'nextras' => 'Nextras\Migrations\Bridges\NextrasDbal\NextrasAdapter',
'dibi' => Nextras\Migrations\Bridges\Dibi\DibiAdapter::class,
'dibi3' => Nextras\Migrations\Bridges\Dibi\Dibi3Adapter::class,
'dibi4' => Nextras\Migrations\Bridges\Dibi\Dibi3Adapter::class,
'doctrine' => Nextras\Migrations\Bridges\DoctrineDbal\DoctrineAdapter::class,
'nette' => Nextras\Migrations\Bridges\NetteDatabase\NetteAdapter::class,
'nextras' => Nextras\Migrations\Bridges\NextrasDbal\NextrasAdapter::class,
];

/** @var array */
protected $drivers = [
'mysql' => 'Nextras\Migrations\Drivers\MySqlDriver',
'pgsql' => 'Nextras\Migrations\Drivers\PgSqlDriver',
'mysql' => Nextras\Migrations\Drivers\MySqlDriver::class,
'pgsql' => Nextras\Migrations\Drivers\PgSqlDriver::class,
];

/** @var array */
protected $printers = [
'console' => 'Nextras\Migrations\Printers\Console',
'psrLog' => 'Nextras\Migrations\Bridges\PsrLog\PsrLogPrinter',
'console' => Nextras\Migrations\Printers\Console::class,
'psrLog' => Nextras\Migrations\Bridges\PsrLog\PsrLogPrinter::class,
];


Expand All @@ -62,13 +63,13 @@ public function load(array $configs, ContainerBuilder $container)
]);

$container->addAliases([
'Nextras\Migrations\IDbal' => 'nextras_migrations.dbal',
'Nextras\Migrations\IDriver' => 'nextras_migrations.driver',
'Nextras\Migrations\IPrinter' => 'nextras_migrations.printer',
Nextras\Migrations\IDbal::class => 'nextras_migrations.dbal',
Nextras\Migrations\IDriver::class => 'nextras_migrations.driver',
Nextras\Migrations\IPrinter::class => 'nextras_migrations.printer',
]);

if ($config['diff_generator'] === 'doctrine') {
$structureDiffGeneratorDefinition = new Definition('Nextras\Migrations\Bridges\DoctrineOrm\StructureDiffGenerator');
$structureDiffGeneratorDefinition = new Definition(Nextras\Migrations\Bridges\DoctrineOrm\StructureDiffGenerator::class);
$structureDiffGeneratorDefinition->setAutowired(true);
$structureDiffGeneratorDefinition->setArgument('$ignoredQueriesFile', $config['ignored_queries_file']);

Expand All @@ -83,19 +84,19 @@ public function load(array $configs, ContainerBuilder $container)
}
}

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

$continueCommandDefinition = new Definition('Nextras\Migrations\Bridges\SymfonyConsole\ContinueCommand');
$continueCommandDefinition = new Definition(Nextras\Migrations\Bridges\SymfonyConsole\ContinueCommand::class);
$continueCommandDefinition->setArguments([$driverDefinition, $configurationDefinition, $printerDefinition]);
$continueCommandDefinition->addTag('console.command');

$createCommandDefinition = new Definition('Nextras\Migrations\Bridges\SymfonyConsole\CreateCommand');
$createCommandDefinition = new Definition(Nextras\Migrations\Bridges\SymfonyConsole\CreateCommand::class);
$createCommandDefinition->setArguments([$driverDefinition, $configurationDefinition, $printerDefinition]);
$createCommandDefinition->addTag('console.command');

$resetCommandDefinition = new Definition('Nextras\Migrations\Bridges\SymfonyConsole\ResetCommand');
$resetCommandDefinition = new Definition(Nextras\Migrations\Bridges\SymfonyConsole\ResetCommand::class);
$resetCommandDefinition->setArguments([$driverDefinition, $configurationDefinition, $printerDefinition]);
$resetCommandDefinition->addTag('console.command');

Expand All @@ -107,7 +108,7 @@ public function load(array $configs, ContainerBuilder $container)
]);

$container->addAliases([
'Nextras\Migrations\IConfiguration' => 'nextras_migrations.configuration',
Nextras\Migrations\IConfiguration::class => 'nextras_migrations.configuration',
]);

if ($structureDiffGeneratorDefinition) {
Expand Down
7 changes: 4 additions & 3 deletions src/Bridges/SymfonyConsole/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Contracts\Translation\LocaleAwareInterface;


class CreateCommand extends BaseCommand
Expand Down Expand Up @@ -152,12 +153,12 @@ protected function getFileName($label, $extension)
if (preg_match('#^[a-z0-9.-]++$#i', $label)) {
$slug = strtolower($label);

} elseif (class_exists('Nette\Utils\Strings')) {
} elseif (class_exists(Strings::class)) {
$slug = Strings::webalize($label, '.');

} elseif (
interface_exists('Symfony\Contracts\Translation\LocaleAwareInterface') &&
class_exists('Symfony\Component\String\Slugger\AsciiSlugger')
interface_exists(LocaleAwareInterface::class) &&
class_exists(AsciiSlugger::class)
) {
$slugger = new AsciiSlugger('en');
$slug = $slugger->slug($label)->toString();
Expand Down
2 changes: 0 additions & 2 deletions tests/cases/integration/dbal/Runner.EmptyRun.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

namespace NextrasTests\Migrations;

use Mockery;
use Nextras\Migrations\Engine\Runner;
use Tester;
use Tester\Assert;

require __DIR__ . '/../../../bootstrap.php';
Expand Down
1 change: 0 additions & 1 deletion tests/cases/integration/dbal/Runner.FirstRun.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace NextrasTests\Migrations;

use Mockery;
use Nextras\Migrations\Engine\Runner;
use Tester;
use Tester\Assert;
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/integration/dbal/Runner.SecondRun.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace NextrasTests\Migrations;

use Mockery;
use Nextras;
use Nextras\Migrations\Engine\Runner;
use Tester;
use Tester\Assert;
Expand Down Expand Up @@ -65,7 +65,7 @@ class SecondRunTest extends IntegrationTestCase

Assert::throws(function () {
$this->runner->run(Runner::MODE_CONTINUE);
}, 'Nextras\Migrations\LogicException');
}, Nextras\Migrations\LogicException::class);

Assert::same([
'Nextras Migrations',
Expand Down
14 changes: 8 additions & 6 deletions tests/cases/integration/nette-di/MigrationsExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace NextrasTests\Migrations;

use Nette;
use Nextras;
use Symfony;
use Tester\Assert;
use Tester\Environment;
use Tester\TestCase;
Expand All @@ -23,8 +25,8 @@ class MigrationsExtensionTest extends TestCase
{
$dic = $this->createContainer($config);

Assert::type('Nextras\Migrations\Drivers\MySqlDriver', $dic->getByType('Nextras\Migrations\IDriver'));
Assert::count(3, $dic->findByType('Symfony\Component\Console\Command\Command'));
Assert::type(Nextras\Migrations\Drivers\MySqlDriver::class, $dic->getByType(Nextras\Migrations\IDriver::class));
Assert::count(3, $dic->findByType(Symfony\Component\Console\Command\Command::class));
Assert::count(3, $dic->findByTag('kdyby.console.command'));
}

Expand All @@ -49,12 +51,12 @@ class MigrationsExtensionTest extends TestCase
{
$dic = $this->createContainer($config);

$configuration = $dic->getByType('Nextras\Migrations\IConfiguration');
Assert::type('Nextras\Migrations\Configurations\Configuration', $configuration);
$configuration = $dic->getByType(Nextras\Migrations\IConfiguration::class);
Assert::type(Nextras\Migrations\Configurations\Configuration::class, $configuration);

$groups = $configuration->getGroups();
Assert::count(3, $groups);
Assert::type('Nextras\Migrations\Bridges\DoctrineOrm\StructureDiffGenerator', $groups[0]->generator);
Assert::type(Nextras\Migrations\Bridges\DoctrineOrm\StructureDiffGenerator::class, $groups[0]->generator);
Assert::null($groups[1]->generator);
Assert::null($groups[2]->generator);
}
Expand All @@ -71,7 +73,7 @@ class MigrationsExtensionTest extends TestCase

public function testDynamicContainerParameters()
{
if (!method_exists('Nette\DI\Compiler', 'setDynamicParameterNames')) {
if (!method_exists(Nette\DI\Compiler::class, 'setDynamicParameterNames')) {
Environment::skip('Required Nette >= 2.4.7');
}

Expand Down
Loading

0 comments on commit 02c8d28

Please sign in to comment.