1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine Bundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
namespace Saxulum\DoctrineOrmCommands\Command\Proxy; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; |
18
|
|
|
use Doctrine\DBAL\Connection; |
19
|
|
|
use Doctrine\ORM\EntityManager; |
20
|
|
|
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
21
|
|
|
use Symfony\Component\Console\Application; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Provides some helper and convenience methods to configure doctrine commands in the context of bundles |
25
|
|
|
* and multiple connections/entity managers. |
26
|
|
|
* |
27
|
|
|
* @author Fabien Potencier <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class DoctrineCommandHelper |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Convenience method to push the helper sets of a given entity manager into the application. |
33
|
|
|
* @param Application|null $application |
34
|
|
|
* @param string $emName |
35
|
|
|
* @throws \InvalidArgumentException |
36
|
|
|
*/ |
37
|
|
|
public static function setApplicationEntityManager(Application $application = null, $emName) |
38
|
|
|
{ |
39
|
|
|
if (is_null($application)) { |
40
|
|
|
throw new \InvalidArgumentException('Application instance needed!'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$helperSet = $application->getHelperSet(); |
44
|
|
|
|
45
|
|
|
/** @var ManagerRegistry $doctrine */ |
46
|
|
|
$doctrine = $helperSet->get('doctrine'); |
47
|
|
|
|
48
|
|
|
/** @var EntityManager $em */ |
49
|
|
|
$em = $doctrine->getManager($emName); |
50
|
|
|
|
51
|
|
|
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db'); |
52
|
|
|
$helperSet->set(new EntityManagerHelper($em), 'em'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Convenience method to push the helper sets of a given connection into the application. |
57
|
|
|
* |
58
|
|
|
* @param Application $application |
59
|
|
|
* @param string $connName |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
public static function setApplicationConnection(Application $application = null, $connName) |
63
|
|
|
{ |
64
|
|
|
if (is_null($application)) { |
65
|
|
|
throw new \InvalidArgumentException('Application instance needed!'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$helperSet = $application->getHelperSet(); |
69
|
|
|
|
70
|
|
|
/** @var ManagerRegistry $doctrine */ |
71
|
|
|
$doctrine = $helperSet->get('doctrine'); |
72
|
|
|
|
73
|
|
|
/** @var Connection $connection */ |
74
|
|
|
$connection = $doctrine->getConnection($connName); |
75
|
|
|
|
76
|
|
|
$helperSet->set(new ConnectionHelper($connection), 'db'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|