1 | <?php |
||
31 | use function class_exists; |
||
32 | use function strtolower; |
||
33 | use function ucfirst; |
||
34 | |||
35 | /** |
||
36 | * Service factory for migrations command |
||
37 | */ |
||
38 | class MigrationsCommandFactory implements FactoryInterface |
||
39 | { |
||
40 | private string $name; |
||
|
|||
41 | 21 | ||
42 | /** |
||
43 | 21 | * {@inheritDoc} |
|
44 | 21 | */ |
|
45 | public function __construct($name) |
||
46 | { |
||
47 | $this->name = ucfirst(strtolower($name)); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | 21 | * |
|
53 | * @return AbstractCommand |
||
54 | 21 | * |
|
55 | * @throws InvalidArgumentException |
||
56 | 21 | */ |
|
57 | 1 | public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
|
58 | { |
||
59 | $className = 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command'; |
||
60 | |||
61 | if (! class_exists($className)) { |
||
62 | 20 | throw new InvalidArgumentException(); |
|
63 | } |
||
64 | 20 | ||
65 | $configuration = $container->get('doctrine.migrations_configuration.orm_default'); |
||
66 | 20 | // @TODO currently hardcoded: `orm_default` should be injected |
|
67 | assert($configuration instanceof Configuration); |
||
68 | 20 | $command = new $className(); |
|
69 | assert($command instanceof AbstractCommand); |
||
70 | |||
71 | $command->setMigrationConfiguration($configuration); |
||
72 | |||
73 | return $command; |
||
74 | } |
||
75 | |||
76 | 21 | /** |
|
77 | * @throws InvalidArgumentException |
||
78 | 21 | */ |
|
79 | public function createService(ServiceLocatorInterface $container) : AbstractCommand |
||
80 | { |
||
81 | return $this($container, 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command'); |
||
84 |