1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineORMModule\Listener; |
21
|
|
|
|
22
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
23
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
24
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand as MigrationCommand; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @license MIT |
28
|
|
|
* @link www.doctrine-project.org |
29
|
|
|
* @author Thomas Dutrion <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class MigrationConfigurationListener |
32
|
|
|
{ |
33
|
|
|
private $container; |
34
|
|
|
|
35
|
16 |
|
public function __construct(ServiceLocatorInterface $container) |
36
|
|
|
{ |
37
|
16 |
|
$this->container = $container; |
38
|
16 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ConsoleCommandEvent $consoleCommandEvent |
42
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
43
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
44
|
|
|
*/ |
45
|
|
|
public function __invoke(ConsoleCommandEvent $consoleCommandEvent) |
46
|
|
|
{ |
47
|
|
|
if (! class_exists(MigrationCommand::class)) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* @var $command MigrationCommand */ |
52
|
|
|
$command = $consoleCommandEvent->getCommand(); |
53
|
|
|
if (! $command instanceof MigrationCommand) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Use default if none is specified |
58
|
|
|
$objectManagerAlias = 'doctrine.entitymanager.orm_default'; |
59
|
|
|
|
60
|
|
|
$input = $consoleCommandEvent->getInput(); |
61
|
|
|
if ($input->hasParameterOption(['--object-manager'])) { |
62
|
|
|
$objectManagerAlias = $input->getParameterOption(['--object-manager']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$migrationsConfigurationAlias = $this->createMigrationConfigurationAlias($objectManagerAlias); |
66
|
|
|
$migrationConfiguration = $this->container->get($migrationsConfigurationAlias); |
67
|
|
|
|
68
|
|
|
$command->setMigrationConfiguration($migrationConfiguration); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create the Migration Configuration alias from the Object Manager alias |
73
|
|
|
* |
74
|
|
|
* @param string $objectManagerAlias |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function createMigrationConfigurationAlias($objectManagerAlias) |
78
|
|
|
{ |
79
|
|
|
return str_replace('entitymanager', 'migrations_configuration', $objectManagerAlias); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|