1 | <?php |
||
23 | class MongodbMigrateCommand extends Command |
||
24 | { |
||
25 | /** |
||
26 | * @var ContainerInterface |
||
27 | */ |
||
28 | private $container; |
||
29 | |||
30 | /** |
||
31 | * @var Finder |
||
32 | */ |
||
33 | private $finder; |
||
34 | |||
35 | /** |
||
36 | * @var DocumentManagerHelper |
||
37 | */ |
||
38 | private $documentManager; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $databaseName; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $errors = []; |
||
49 | |||
50 | /** |
||
51 | * @param ContainerInterface $container container instance for injecting into aware migrations |
||
52 | * @param Finder $finder finder that finds configs |
||
53 | * @param DocumentManagerHelper $documentManager dm helper to get access to db in command |
||
54 | * @param string $databaseName name of database where data is found in |
||
55 | */ |
||
56 | 4 | public function __construct( |
|
69 | |||
70 | /** |
||
71 | * setup command |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | 4 | protected function configure() |
|
81 | |||
82 | /** |
||
83 | * call execute on found commands |
||
84 | * |
||
85 | * @param InputInterface $input user input |
||
86 | * @param OutputInterface $output command output |
||
87 | * |
||
88 | * @return void |
||
|
|||
89 | */ |
||
90 | public function execute(InputInterface $input, OutputInterface $output) |
||
91 | { |
||
92 | // graviton root |
||
93 | $baseDir = __DIR__.'/../../../'; |
||
94 | |||
95 | // vendorized? - go back some more.. |
||
96 | if (strpos($baseDir, '/vendor/') !== false) { |
||
97 | $baseDir .= '../../../'; |
||
98 | } |
||
99 | |||
100 | $this->finder |
||
101 | ->in($baseDir) |
||
102 | ->path('Resources/config') |
||
103 | ->name('/migrations.(xml|yml)/') |
||
104 | ->files(); |
||
105 | |||
106 | foreach ($this->finder as $file) { |
||
107 | if (!$file->isFile()) { |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | $output->writeln('Found '.$file->getRelativePathname()); |
||
112 | |||
113 | $command = $this->getApplication()->find('mongodb:migrations:migrate'); |
||
114 | |||
115 | $helperSet = $command->getHelperSet(); |
||
116 | $helperSet->set($this->documentManager, 'dm'); |
||
117 | $command->setHelperSet($helperSet); |
||
118 | |||
119 | $configuration = $this->getConfiguration($file->getPathname(), $output); |
||
120 | self::injectContainerToMigrations($this->container, $configuration->getMigrations()); |
||
121 | $command->setMigrationConfiguration($configuration); |
||
122 | |||
123 | $arguments = $input->getArguments(); |
||
124 | $arguments['command'] = 'mongodb:migrations:migrate'; |
||
125 | $arguments['--configuration'] = $file->getPathname(); |
||
126 | |||
127 | $migrateInput = new ArrayInput($arguments); |
||
128 | $migrateInput->setInteractive($input->isInteractive()); |
||
129 | $returnCode = $command->run($migrateInput, $output); |
||
130 | |||
131 | if ($returnCode !== 0) { |
||
132 | $this->errors[] = sprintf( |
||
133 | 'Calling mongodb:migrations:migrate failed for %s', |
||
134 | $file->getRelativePathname() |
||
135 | ); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if (!empty($this->errors)) { |
||
140 | $output->writeln( |
||
141 | sprintf('<error>%s</error>', implode(PHP_EOL, $this->errors)) |
||
142 | ); |
||
143 | return -1; |
||
144 | } |
||
145 | |||
146 | return 0; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * get configration object for migration script |
||
151 | * |
||
152 | * This is based on antromattr/mongodb-migartion code but extends it so we can inject |
||
153 | * non local stuff centrally. |
||
154 | * |
||
155 | * @param string $filepath path to configuration file |
||
156 | * @param Output $output ouput interface need by config parser to do stuff |
||
157 | * |
||
158 | * @return AntiMattr\MongoDB\Migrations\Configuration\Configuration |
||
159 | */ |
||
160 | private function getConfiguration($filepath, $output) |
||
182 | |||
183 | /** |
||
184 | * Injects the container to migrations aware of it |
||
185 | * |
||
186 | * @param ContainerInterface $container container to inject into container aware migrations |
||
187 | * @param array $versions versions that might need injecting a container |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | private static function injectContainerToMigrations(ContainerInterface $container, array $versions) |
||
200 | } |
||
201 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.