1 | <?php |
||
19 | class EntityMapBuilderController extends AbstractConsoleController |
||
20 | { |
||
21 | /** |
||
22 | * Менеджер для получения ObjectManager Doctrine2 |
||
23 | * |
||
24 | * @var ContainerInterface |
||
25 | */ |
||
26 | protected $doctrineObjectManager; |
||
27 | |||
28 | /** |
||
29 | * Сервис для кеширования EntityMap |
||
30 | * |
||
31 | * @var EntityMapCacheInterface |
||
32 | */ |
||
33 | protected $entityMapCache; |
||
34 | |||
35 | /** |
||
36 | * EntityMapBuilderController constructor. |
||
37 | * |
||
38 | * @param ContainerInterface $doctrineObjectManager |
||
39 | * @param EntityMapCacheInterface $entityMapCache |
||
40 | */ |
||
41 | public function __construct(ContainerInterface $doctrineObjectManager, EntityMapCacheInterface $entityMapCache) |
||
42 | { |
||
43 | $this->setDoctrineObjectManager($doctrineObjectManager); |
||
44 | $this->setEntityMapCache($entityMapCache); |
||
45 | } |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Генерация карты сущностей и сохранение ее в кеше |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | public function buildAction() |
||
54 | { |
||
55 | /** @var Request $request */ |
||
56 | $request = $this->getRequest(); |
||
57 | $managerName = $request->getParam('objectManager', 'doctrine.entitymanager.orm_default'); |
||
58 | |||
59 | if (!$this->getDoctrineObjectManager()->has($managerName)) { |
||
60 | return [ |
||
61 | ConsoleModel::RESULT => sprintf('Doctrine ObjectManager %s not found', $managerName) |
||
62 | ]; |
||
63 | } |
||
64 | |||
65 | $entityMapCache = $this->getEntityMapCache(); |
||
66 | if ($entityMapCache->hasEntityMap($managerName)) { |
||
67 | $entityMapCache->deleteEntityMap($managerName); |
||
68 | } |
||
69 | $entityMapCache->saveEntityMap($managerName); |
||
70 | |||
71 | |||
72 | $entityMap = $entityMapCache->loadEntityMap($managerName); |
||
73 | |||
74 | if (is_array($entityMap)) { |
||
75 | $result = "Entity map:\n"; |
||
76 | foreach ($entityMap as $interfaceName => $className) { |
||
77 | $result .= sprintf("Interface name: %s. Class name: %s \n", $interfaceName, $className); |
||
78 | } |
||
79 | } else { |
||
80 | $result = 'Empty entity map'; |
||
81 | } |
||
82 | |||
83 | |||
84 | return [ |
||
85 | ConsoleModel::RESULT => $result |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Очистка карты сущностей из кеша |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function clearAction() |
||
96 | { |
||
97 | /** @var Request $request */ |
||
98 | $request = $this->getRequest(); |
||
99 | $managerName = $request->getParam('objectManager', 'doctrine.entitymanager.orm_default'); |
||
100 | |||
101 | $entityMapCache = $this->getEntityMapCache(); |
||
102 | $result = "Entity map not found\n"; |
||
103 | if ($entityMapCache->hasEntityMap($managerName)) { |
||
104 | $entityMapCache->deleteEntityMap($managerName); |
||
105 | $result = "Entity map clear\n"; |
||
106 | } |
||
107 | |||
108 | return [ |
||
109 | ConsoleModel::RESULT => $result |
||
110 | ]; |
||
111 | } |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Возвращает менеджер для получения ObjectManager Doctrine2 |
||
116 | * |
||
117 | * @return ContainerInterface |
||
118 | */ |
||
119 | public function getDoctrineObjectManager() |
||
123 | |||
124 | /** |
||
125 | * Устанавливает менеджер для получения ObjectManager Doctrine2 |
||
126 | * |
||
127 | * @param ContainerInterface $doctrineObjectManager |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setDoctrineObjectManager(ContainerInterface $doctrineObjectManager) |
||
137 | |||
138 | /** |
||
139 | * Возвращает сервис для кеширования EntityMap |
||
140 | * |
||
141 | * @return EntityMapCacheInterface |
||
142 | */ |
||
143 | public function getEntityMapCache() |
||
147 | |||
148 | /** |
||
149 | * Устанавливает сервис для кеширования EntityMap |
||
150 | * |
||
151 | * @param EntityMapCacheInterface $entityMapCache |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setEntityMapCache(EntityMapCacheInterface $entityMapCache) |
||
161 | } |
||
162 |