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\ORM\EntityManagerInterface; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
17
|
|
|
use Psr\Container\ContainerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
21
|
|
|
*/ |
22
|
|
|
class CliConfigCommandFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
/** |
29
|
|
|
* @var Config |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
/** |
33
|
|
|
* @var EntityManagerInterface |
34
|
|
|
*/ |
35
|
|
|
private $entityManager; |
36
|
|
|
/** |
37
|
|
|
* @var Configuration |
38
|
|
|
*/ |
39
|
|
|
private $migrationsConfig; |
40
|
|
|
|
41
|
|
|
public function __construct(ContainerInterface $container, Config $config, EntityManagerInterface $entityManager) |
42
|
|
|
{ |
43
|
|
|
$this->container = $container; |
44
|
|
|
$this->config = $config; |
45
|
|
|
$this->entityManager = $entityManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* For use in your project's cli-config.php file |
50
|
|
|
* |
51
|
|
|
* @see /cli-config.php |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getCommands(): array |
56
|
|
|
{ |
57
|
|
|
$commands = [ |
58
|
|
|
$this->container->get(GenerateRelationsCommand::class), |
59
|
|
|
$this->container->get(GenerateEntityCommand::class), |
60
|
|
|
$this->container->get(SetRelationCommand::class), |
61
|
|
|
$this->container->get(GenerateFieldCommand::class), |
62
|
|
|
$this->container->get(SetFieldCommand::class), |
63
|
|
|
$this->container->get(SetEmbeddableCommand::class), |
64
|
|
|
$this->container->get(GenerateEmbeddableFromArchetypeCommand::class), |
65
|
|
|
$this->container->get(GenerateEmbeddableSkeletonCommand::class), |
66
|
|
|
$this->container->get(RemoveUnusedRelationsCommand::class), |
67
|
|
|
$this->container->get(OverrideCreateCommand::class), |
68
|
|
|
$this->container->get(OverridesUpdateCommand::class), |
69
|
|
|
$this->container->get(CreateConstraintCommand::class), |
70
|
|
|
$this->container->get(FinaliseBuildCommand::class), |
71
|
|
|
$this->container->get(CreateConstraintCommand::class), |
72
|
|
|
]; |
73
|
|
|
$migrationsCommands = [ |
74
|
|
|
$this->container->get(ExecuteCommand::class), |
75
|
|
|
$this->container->get(GenerateCommand::class), |
76
|
|
|
$this->container->get(LatestCommand::class), |
77
|
|
|
$this->container->get(MigrateCommand::class), |
78
|
|
|
$this->container->get(DiffCommand::class), |
79
|
|
|
$this->container->get(UpToDateCommand::class), |
80
|
|
|
$this->container->get(StatusCommand::class), |
81
|
|
|
$this->container->get(VersionCommand::class), |
82
|
|
|
]; |
83
|
|
|
foreach ($migrationsCommands as $command) { |
84
|
|
|
$commands[] = $this->addMigrationsConfig($command); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $commands; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function addMigrationsConfig(AbstractCommand $command): AbstractCommand |
91
|
|
|
{ |
92
|
|
|
$command->setMigrationConfiguration($this->getMigrationsConfig()); |
93
|
|
|
|
94
|
|
|
return $command; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getMigrationsConfig(): Configuration |
98
|
|
|
{ |
99
|
|
|
if (null === $this->migrationsConfig) { |
100
|
|
|
$this->migrationsConfig = new Configuration($this->entityManager->getConnection()); |
101
|
|
|
$this->migrationsConfig->setMigrationsDirectory($this->config->get(Config::PARAM_MIGRATIONS_DIRECTORY)); |
102
|
|
|
$this->migrationsConfig->setMigrationsAreOrganizedByYearAndMonth(true); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->migrationsConfig; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
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: