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
179 lines (145 loc) · 4.3 KB
/
Copy pathContainerAwareCommand.php
File metadata and controls
179 lines (145 loc) · 4.3 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
<?php
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\AppConsole\Command\Helper\TranslatorHelper;
abstract class ContainerAwareCommand extends Command implements ContainerAwareInterface
{
private $container;
private $modules;
private $services;
private $route_provider;
/**
* @var TranslatorHelper
*/
protected $translator;
function __construct($translator)
{
$this->translator = $translator;
parent::__construct();
}
/**
* @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;
}
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;
}
/**
* @return \Drupal\AppConsole\Utils\Validators
*/
public function getValidator()
{
return $this->getContainer()->get('console.validators');
}
public function getConfigFactory(){
return $this->getContainer()->get('config.factory');
}
public function getConfigStorage(){
return $this->getContainer()->get('config.storage');
}
public function getEntityManager(){
return $this->getContainer()->get('entity.manager');
}
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;
}
/**
* @return \Drupal\AppConsole\Utils\StringUtils
*/
public function getStringUtils()
{
return $this->getContainer()->get('console.string_utils');
}
/**
* @param $key string
* @return string
*/
public function trans($key){
return $this->translator->trans($key);
}
}