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 | public function __construct($name) |
||
43 | 21 | { |
|
44 | 21 | $this->name = ucfirst(strtolower($name)); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritDoc} |
||
49 | * |
||
50 | * @return AbstractCommand |
||
51 | * |
||
52 | 21 | * @throws InvalidArgumentException |
|
53 | */ |
||
54 | 21 | public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
|
55 | { |
||
56 | 21 | $className = 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command'; |
|
57 | 1 | ||
58 | if (! class_exists($className)) { |
||
59 | throw new InvalidArgumentException(); |
||
60 | } |
||
61 | |||
62 | 20 | $configuration = $container->get('doctrine.migrations_configuration.orm_default'); |
|
63 | // @TODO currently hardcoded: `orm_default` should be injected |
||
64 | 20 | assert($configuration instanceof Configuration); |
|
65 | $command = new $className(); |
||
66 | 20 | assert($command instanceof AbstractCommand); |
|
67 | |||
68 | 20 | $command->setMigrationConfiguration($configuration); |
|
69 | |||
70 | return $command; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @throws InvalidArgumentException |
||
75 | */ |
||
76 | 21 | public function createService(ServiceLocatorInterface $container) : AbstractCommand |
|
77 | { |
||
78 | 21 | return $this($container, 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command'); |
|
79 | } |
||
80 | } |
||
81 |