1 | <?php |
||
18 | abstract class DoctrineCommand extends Command |
||
19 | { |
||
20 | /** @var ManagerRegistry */ |
||
21 | private $doctrine; |
||
22 | |||
23 | public function __construct(ManagerRegistry $doctrine) |
||
24 | { |
||
25 | parent::__construct(); |
||
26 | |||
27 | $this->doctrine = $doctrine; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * get a doctrine entity generator |
||
32 | * |
||
33 | * @return EntityGenerator |
||
34 | */ |
||
35 | protected function getEntityGenerator() |
||
36 | { |
||
37 | $entityGenerator = new EntityGenerator() |
||
38 | ->setGenerateAnnotations(false) |
||
|
|||
39 | ->setGenerateStubMethods(true) |
||
40 | ->setRegenerateEntityIfExists(false) |
||
41 | ->setUpdateEntityIfExists(true) |
||
42 | ->setNumSpaces(4) |
||
43 | ->setAnnotationPrefix('ORM\\'); |
||
44 | |||
45 | return $entityGenerator; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get a doctrine entity manager by symfony name. |
||
50 | * |
||
51 | * @param string $name |
||
52 | * @param int|null $shardId |
||
53 | * |
||
54 | * @return EntityManager |
||
55 | */ |
||
56 | protected function getEntityManager($name, $shardId = null) |
||
57 | { |
||
58 | $manager = $this->getDoctrine()->getManager($name); |
||
59 | |||
60 | if ($shardId) { |
||
61 | if (! $manager->getConnection() instanceof PoolingShardConnection) { |
||
62 | throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name)); |
||
63 | } |
||
64 | |||
65 | $manager->getConnection()->connect($shardId); |
||
66 | } |
||
67 | |||
68 | return $manager; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get a doctrine dbal connection by symfony name. |
||
73 | * |
||
74 | * @param string $name |
||
75 | * |
||
76 | * @return Connection |
||
77 | */ |
||
78 | protected function getDoctrineConnection($name) |
||
79 | { |
||
80 | return $this->getDoctrine()->getConnection($name); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return ManagerRegistry |
||
85 | */ |
||
86 | protected function getDoctrine() |
||
87 | { |
||
88 | return $this->doctrine; |
||
89 | } |
||
90 | } |
||
91 |