Completed
Pull Request — master (#877)
by Robin
03:09
created

DoctrineCommand::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
        $this->doctrine = $doctrine;
32
    }
33
34
    /**
35
     * @deprecated
36
     */
37
    public function setContainer(ContainerInterface $container = null)
38
    {
39
        $this->container = $container;
40
    }
41
42
    /**
43
     * @deprecated
44
     *
45
     * @return ContainerInterface
46
     *
47
     * @throws LogicException
48
     */
49
    protected function getContainer()
50
    {
51
        if ($this->container === null) {
52
            $application = $this->getApplication();
53
            if ($application === null) {
54
                throw new LogicException('The container cannot be retrieved as the application instance is not yet set.');
55
            }
56
57
            $this->container = $application->getKernel()->getContainer();
58
        }
59
60
        return $this->container;
61
    }
62
63
    /**
64
     * get a doctrine entity generator
65
     *
66
     * @return EntityGenerator
67
     */
68
    protected function getEntityGenerator()
69
    {
70
        $entityGenerator = new EntityGenerator();
71
        $entityGenerator->setGenerateAnnotations(false);
72
        $entityGenerator->setGenerateStubMethods(true);
73
        $entityGenerator->setRegenerateEntityIfExists(false);
74
        $entityGenerator->setUpdateEntityIfExists(true);
75
        $entityGenerator->setNumSpaces(4);
76
        $entityGenerator->setAnnotationPrefix('ORM\\');
77
78
        return $entityGenerator;
79
    }
80
81
    /**
82
     * Get a doctrine entity manager by symfony name.
83
     *
84
     * @param string   $name
85
     * @param int|null $shardId
86
     *
87
     * @return EntityManager
88
     */
89
    protected function getEntityManager($name, $shardId = null)
90
    {
91
        $manager = $this->getDoctrine()->getManager($name);
92
93
        if ($shardId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $shardId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
94
            if (! $manager->getConnection() instanceof PoolingShardConnection) {
95
                throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name));
96
            }
97
98
            $manager->getConnection()->connect($shardId);
99
        }
100
101
        return $manager;
102
    }
103
104
    /**
105
     * Get a doctrine dbal connection by symfony name.
106
     *
107
     * @param string $name
108
     *
109
     * @return Connection
110
     */
111
    protected function getDoctrineConnection($name)
112
    {
113
        return $this->getDoctrine()->getConnection($name);
114
    }
115
116
    /**
117
     * @return ManagerRegistry
118
     */
119
    protected function getDoctrine()
120
    {
121
        return $this->doctrine ?: $this->doctrine = $this->getContainer()->get('doctrine');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Bundle\Doctrine...Command::getContainer() has been deprecated.

This method has been deprecated.

Loading history...
122
    }
123
}
124