1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
6
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand; |
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand; |
8
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand; |
9
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand; |
10
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\LatestCommand; |
11
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand; |
12
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand; |
13
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\UpToDateCommand; |
14
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand; |
15
|
|
|
use Doctrine\DBAL\Tools\Console as DBALConsole; |
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
19
|
|
|
use Psr\Container\ContainerInterface; |
20
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
24
|
|
|
*/ |
25
|
|
|
class CliConfigCommandFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ContainerInterface |
29
|
|
|
*/ |
30
|
|
|
private $container; |
31
|
|
|
/** |
32
|
|
|
* @var Config |
33
|
|
|
*/ |
34
|
|
|
private $config; |
35
|
|
|
/** |
36
|
|
|
* @var EntityManagerInterface |
37
|
|
|
*/ |
38
|
|
|
private $entityManager; |
39
|
|
|
/** |
40
|
|
|
* @var Configuration |
41
|
|
|
*/ |
42
|
|
|
private $migrationsConfig; |
43
|
|
|
|
44
|
|
|
public function __construct(ContainerInterface $container, Config $config, EntityManagerInterface $entityManager) |
45
|
|
|
{ |
46
|
|
|
$this->container = $container; |
47
|
|
|
$this->config = $config; |
48
|
|
|
$this->entityManager = $entityManager; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* For use in your project's cli-config.php file |
53
|
|
|
* |
54
|
|
|
* @see /cli-config.php |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getCommands(): array |
59
|
|
|
{ |
60
|
|
|
$commands = [ |
61
|
|
|
$this->container->get(GenerateRelationsCommand::class), |
62
|
|
|
$this->container->get(GenerateEntityCommand::class), |
63
|
|
|
$this->container->get(SetRelationCommand::class), |
64
|
|
|
$this->container->get(GenerateFieldCommand::class), |
65
|
|
|
$this->container->get(SetFieldCommand::class), |
66
|
|
|
$this->container->get(SetEmbeddableCommand::class), |
67
|
|
|
$this->container->get(GenerateEmbeddableFromArchetypeCommand::class), |
68
|
|
|
$this->container->get(GenerateEmbeddableSkeletonCommand::class), |
69
|
|
|
$this->container->get(RemoveUnusedRelationsCommand::class), |
70
|
|
|
$this->container->get(OverrideCreateCommand::class), |
71
|
|
|
$this->container->get(OverridesUpdateCommand::class), |
72
|
|
|
$this->container->get(CreateConstraintCommand::class), |
73
|
|
|
$this->container->get(FinaliseBuildCommand::class), |
74
|
|
|
$this->container->get(CreateConstraintCommand::class), |
75
|
|
|
]; |
76
|
|
|
$migrationsCommands = [ |
77
|
|
|
$this->container->get(ExecuteCommand::class), |
78
|
|
|
$this->container->get(GenerateCommand::class), |
79
|
|
|
$this->container->get(LatestCommand::class), |
80
|
|
|
$this->container->get(MigrateCommand::class), |
81
|
|
|
$this->container->get(DiffCommand::class), |
82
|
|
|
$this->container->get(UpToDateCommand::class), |
83
|
|
|
$this->container->get(StatusCommand::class), |
84
|
|
|
$this->container->get(VersionCommand::class), |
85
|
|
|
]; |
86
|
|
|
foreach ($migrationsCommands as $command) { |
87
|
|
|
$commands[] = $this->addMigrationsConfig($command); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $commands; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function addMigrationsConfig(AbstractCommand $command): AbstractCommand |
94
|
|
|
{ |
95
|
|
|
$command->setMigrationConfiguration($this->getMigrationsConfig()); |
96
|
|
|
|
97
|
|
|
return $command; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function getMigrationsConfig(): Configuration |
101
|
|
|
{ |
102
|
|
|
if (null === $this->migrationsConfig) { |
103
|
|
|
$this->migrationsConfig = new Configuration($this->entityManager->getConnection()); |
104
|
|
|
$this->migrationsConfig->setMigrationsDirectory($this->config->get(Config::PARAM_MIGRATIONS_DIRECTORY)); |
105
|
|
|
$this->migrationsConfig->setMigrationsAreOrganizedByYearAndMonth(true); |
106
|
|
|
$this->migrationsConfig->setMigrationsNamespace('Migrations'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->migrationsConfig; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function createHelperSet(): HelperSet |
113
|
|
|
{ |
114
|
|
|
return new HelperSet( |
115
|
|
|
[ |
116
|
|
|
'db' => new DBALConsole\Helper\ConnectionHelper($this->entityManager->getConnection()), |
117
|
|
|
'em' => new EntityManagerHelper($this->entityManager), |
118
|
|
|
'question' => new \Symfony\Component\Console\Helper\QuestionHelper(), |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: