1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\ORM\Tools\EntityGenerator; |
10
|
|
|
use LogicException; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Base class for Doctrine console commands to extend from. |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
abstract class DoctrineCommand extends Command |
20
|
|
|
{ |
21
|
|
|
/** @var ManagerRegistry|null */ |
22
|
|
|
private $doctrine; |
23
|
|
|
|
24
|
|
|
/** @var ContainerInterface|null */ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
public function __construct(ManagerRegistry $doctrine = null) |
28
|
|
|
{ |
29
|
|
|
parent::__construct(); |
30
|
|
|
|
31
|
|
|
if (null === $doctrine) { |
32
|
|
|
@trigger_error(sprintf( |
|
|
|
|
33
|
|
|
'The "%s" constructor expects a "%s" instance as first argument, not passing it will throw a \TypeError in DoctrineBundle 2.0.', |
34
|
|
|
get_class($this), |
35
|
|
|
ManagerRegistry::class |
36
|
|
|
), E_USER_DEPRECATED); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->doctrine = $doctrine; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @deprecated |
44
|
|
|
*/ |
45
|
|
|
public function setContainer(ContainerInterface $container = null) |
46
|
|
|
{ |
47
|
|
|
@trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->container = $container; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ContainerInterface |
54
|
|
|
* |
55
|
|
|
* @throws \LogicException |
56
|
|
|
* |
57
|
|
|
* @deprecated |
58
|
|
|
*/ |
59
|
|
|
protected function getContainer() |
60
|
|
|
{ |
61
|
|
|
@trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (null === $this->container) { |
64
|
|
|
$application = $this->getApplication(); |
65
|
|
|
if (null === $application) { |
66
|
|
|
throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->container = $application->getKernel()->getContainer(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->container; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* get a doctrine entity generator |
77
|
|
|
* |
78
|
|
|
* @return EntityGenerator |
79
|
|
|
*/ |
80
|
|
|
protected function getEntityGenerator() |
81
|
|
|
{ |
82
|
|
|
$entityGenerator = new EntityGenerator(); |
83
|
|
|
$entityGenerator->setGenerateAnnotations(false); |
84
|
|
|
$entityGenerator->setGenerateStubMethods(true); |
85
|
|
|
$entityGenerator->setRegenerateEntityIfExists(false); |
86
|
|
|
$entityGenerator->setUpdateEntityIfExists(true); |
87
|
|
|
$entityGenerator->setNumSpaces(4); |
88
|
|
|
$entityGenerator->setAnnotationPrefix('ORM\\'); |
89
|
|
|
|
90
|
|
|
return $entityGenerator; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get a doctrine entity manager by symfony name. |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* @param int|null $shardId |
98
|
|
|
* |
99
|
|
|
* @return EntityManager |
100
|
|
|
*/ |
101
|
|
|
protected function getEntityManager($name, $shardId = null) |
102
|
|
|
{ |
103
|
|
|
$manager = $this->getDoctrine()->getManager($name); |
104
|
|
|
|
105
|
|
|
if ($shardId) { |
|
|
|
|
106
|
|
|
if (! $manager->getConnection() instanceof PoolingShardConnection) { |
107
|
|
|
throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$manager->getConnection()->connect($shardId); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $manager; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get a doctrine dbal connection by symfony name. |
118
|
|
|
* |
119
|
|
|
* @param string $name |
120
|
|
|
* |
121
|
|
|
* @return Connection |
122
|
|
|
*/ |
123
|
|
|
protected function getDoctrineConnection($name) |
124
|
|
|
{ |
125
|
|
|
return $this->getDoctrine()->getConnection($name); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ManagerRegistry |
130
|
|
|
*/ |
131
|
|
|
protected function getDoctrine() |
132
|
|
|
{ |
133
|
|
|
return $this->doctrine ?: $this->doctrine = $this->getContainer()->get('doctrine'); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: