1 | <?php |
||
10 | class ProviderProvider extends ResourceFactoryAware |
||
11 | { |
||
12 | /** |
||
13 | * @var EntityManager |
||
14 | */ |
||
15 | protected $manager; |
||
16 | |||
17 | /** |
||
18 | * @var RepositoryInterface |
||
19 | */ |
||
20 | protected $repository; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $dataClass; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $defaultProvider; |
||
31 | |||
32 | public function __construct(EntityManager $manager, $dataClass, $defaultProvider = 'dummy') |
||
|
|||
33 | { |
||
34 | $this->manager = $manager; |
||
35 | $this->dataClass = $dataClass; |
||
36 | $this->repository = $manager->getRepository($dataClass); |
||
37 | $this->defaultProvider = $defaultProvider; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param $name |
||
42 | */ |
||
43 | public function setDefaultProvider($name) |
||
47 | |||
48 | /** |
||
49 | * @param $name |
||
50 | * |
||
51 | * @return null|ProviderInterface |
||
52 | */ |
||
53 | public function findByName($name) |
||
57 | |||
58 | /** |
||
59 | * @return null|ProviderInterface |
||
60 | */ |
||
61 | public function getActivedProvider() |
||
69 | |||
70 | /** |
||
71 | * Get paramter by provider name. |
||
72 | * |
||
73 | * @param $name |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getParameters($name) |
||
85 | } |
||
86 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.