forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerAwareCommand.php
More file actions
330 lines (271 loc) · 9.1 KB
/
Copy pathContainerAwareCommand.php
File metadata and controls
330 lines (271 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
namespace Drupal\AppConsole\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Drupal\AppConsole\Command\Command;
use Drupal\Core\Extension\ExtensionDiscovery;
abstract class ContainerAwareCommand extends Command implements ContainerAwareInterface
{
private $container;
private $modules;
private $services;
private $route_provider;
/**
* @return ContainerInterface
*/
protected function getContainer()
{
if (null === $this->container) {
$this->container = $this->getApplication()->getKernel()->getContainer();
}
return $this->container;
}
/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* [getModules description]
* @param boolean $core Return core modules
* @return array list of modules
*/
public function getModules($core = false)
{
if (null === $this->modules) {
$this->modules = [];
$extensionDiscover = new ExtensionDiscovery(\Drupal::root());
$moduleList = $extensionDiscover->scan('module');
foreach ($moduleList as $name => $filename) {
if ($core) {
array_push($this->modules, $name);
} elseif (!preg_match('/^core/', $filename->getPathname())) {
array_push($this->modules, $name);
}
}
}
return $this->modules;
}
/**
* [getModules description]
* @param boolean $core Return core modules
* @return array list of modules
*/
public function getMigrations($group = false)
{
$entity_manager = $this->getEntityManager();
$migration_storage = $entity_manager->getStorage('migration');
$entity_query_service = $this->getEntityQuery();
$query = $entity_query_service->get('migration');
if ($group) {
$query->condition('migration_groups.*', $group);
}
$results = $query->execute();
$migration_entities = $migration_storage->loadMultiple($results);
$migrations = array();
foreach ($migration_entities as $migration) {
$migrations[$migration->id()]['version'] = ucfirst($migration->migration_groups[0]);
$label = str_replace($migrations[$migration->id()]['version'], '', $migration->label());
$migrations[$migration->id()]['description'] = ucwords($label);
}
return $migrations;
}
public function getRestDrupalConfig()
{
return $this->getConfigFactory()
->get('rest.settings')->get('resources') ?: [];
}
/**
* [geRest get a list of Rest Resouces]
* @param boolean $status return Rest Resources by status
* @return array list of rest resources
*/
public function getRestResources($rest_status = false)
{
$config = $this->getRestDrupalConfig();
$resourcePluginManager = $this->getPluginManagerRest();
$resources = $resourcePluginManager->getDefinitions();
$enabled_resources = array_combine(array_keys($config), array_keys($config));
$available_resources = array('enabled' => array(), 'disabled' => array());
foreach ($resources as $id => $resource) {
$status = in_array($id, $enabled_resources) ? 'enabled' : 'disabled';
$available_resources[$status][$id] = $resource;
}
// Sort the list of resources by label.
$sort_resources = function ($resource_a, $resource_b) {
return strcmp($resource_a['label'], $resource_b['label']);
};
if (!empty($available_resources['enabled'])) {
uasort($available_resources['enabled'], $sort_resources);
}
if (!empty($available_resources['disabled'])) {
uasort($available_resources['disabled'], $sort_resources);
}
if (isset($available_resources[$rest_status])) {
return array($rest_status => $available_resources[$rest_status]);
}
return $available_resources;
}
public function getServices()
{
if (null === $this->services) {
$this->services = [];
$this->services = $this->getContainer()->getServiceIds();
}
return $this->services;
}
public function getRouteProvider()
{
if (null === $this->route_provider) {
$this->route_provider = $this->getContainer()->get('router.route_provider');
}
return $this->route_provider;
}
/**
* @param $rest
* @param $rest_resources_ids
* @param $translator
* @return mixed
*/
public function validateRestResource($rest, $rest_resources_ids, $translator)
{
if (in_array($rest, $rest_resources_ids)) {
return $rest;
} else {
throw new \InvalidArgumentException(sprintf($translator->trans('commands.rest.disable.messages.invalid-rest-id'),
$rest));
}
}
/**
* @return \Drupal\Core\Config\ConfigFactoryInterface
*/
public function getConfigFactory()
{
return $this->getContainer()->get('config.factory');
}
/**
* @return \Drupal\Core\State\StateInterface
*/
public function getState()
{
return $this->getContainer()->get('state');
}
public function getConfigStorage()
{
return $this->getContainer()->get('config.storage');
}
/**
* @return Drupal\Core\Config\ConfigManagerInterface
*/
public function getConfigManager()
{
return $this->getContainer()->get('config.manager');
}
public function getEntityManager()
{
return $this->getContainer()->get('entity.manager');
}
public function getEntityQuery()
{
return $this->getContainer()->get('entity.query');
}
public function getModuleInstaller()
{
return $this->getContainer()->get('module_installer');
}
public function getPluginManagerRest()
{
return $this->getContainer()->get('plugin.manager.rest');
}
/**
* getTestDiscovery return a service object for Simpletest
* @return Drupal\simpletest\TestDiscovery
*/
public function getTestDiscovery()
{
return $this->getContainer()->get('test_discovery');
}
public function getHttpClient()
{
return $this->getContainer()->get('http_client');
}
public function getSerializerFormats()
{
return $this->getContainer()->getParameter('serializer.formats');
}
public function getAuthenticationProviders()
{
return $this->getContainer()->get('authentication')->getSortedProviders();
}
/**
* @return \Drupal\system\SystemManager
*/
public function getSystemManager()
{
return $this->getContainer()->get('system.manager');
}
/**
* @return array
*/
public function getConnectionInfo()
{
return \Drupal\Core\Database\Database::getConnectionInfo();
}
/**
* @return \Drupal\Core\Extension\ThemeHandlerInterface
*/
public function getThemeHandler()
{
return $this->getContainer()->get('theme_handler');
}
public function validateModuleExist($module_name)
{
return $this->getValidator()->validateModuleExist($module_name, $this->getModules());
}
public function validateServiceExist($service_name, $services = null)
{
if (!$services) {
$services = $this->getServices();
}
return $this->getValidator()->validateServiceExist($service_name, $services);
}
public function validateModule($machine_name)
{
$machine_name = $this->validateMachineName($machine_name);
$modules = array_merge($this->getModules(true), $this->getModules());
if (in_array($machine_name, $modules)) {
throw new \InvalidArgumentException(sprintf('Module "%s" already exist.', $machine_name));
}
return $machine_name;
}
public function validateModuleName($module_name)
{
return $this->getValidator()->validateModuleName($module_name);
}
public function validateModulePath($module_path, $create_dir = false)
{
return $this->getValidator()->validateModulePath($module_path, $create_dir);
}
public function validateClassName($class_name)
{
return $this->getValidator()->validateClassName($class_name);
}
public function validateMachineName($machine_name)
{
$machine_name = $this->getValidator()->validateMachineName($machine_name);
if ($this->getEntityManager()->hasDefinition($machine_name)) {
throw new \InvalidArgumentException(sprintf('Machine name "%s" is duplicated.', $machine_name));
}
return $machine_name;
}
public function validateSpaces($name)
{
return $this->getValidator()->validateSpaces($name);
}
public function removeSpaces($name)
{
return $this->getValidator()->removeSpaces($name);
}
}