3 namespace Drupal\Console\Extension;
5 use Drupal\Console\Utils\Site;
8 * Class ExtensionManager
10 * @package Drupal\Console
26 private $extensions = [];
31 private $filters = [];
36 private $extension = null;
39 * ExtensionManager constructor.
42 * @param string $appRoot
44 public function __construct(
49 $this->appRoot = $appRoot;
56 public function showInstalled()
58 $this->filters['showInstalled'] = true;
65 public function showUninstalled()
67 $this->filters['showUninstalled'] = true;
74 public function showCore()
76 $this->filters['showCore'] = true;
83 public function showNoCore()
85 $this->filters['showNoCore'] = true;
90 * @param string $nameOnly
93 public function getList($nameOnly)
95 return $this->getExtensions($this->extension, $nameOnly);
101 public function discoverModules()
104 $this->discoverExtension('module');
112 public function discoverThemes()
115 $this->discoverExtension('theme');
123 public function discoverProfiles()
126 $this->discoverExtension('profile');
132 * @param string $extension
134 private function discoverExtension($extension)
136 $this->extension = $extension;
137 $this->extensions[$extension] = $this->discoverExtensions($extension);
143 private function initialize()
145 $this->extension = 'module';
146 $this->extensions = [
152 'showInstalled' => false,
153 'showUninstalled' => false,
155 'showNoCore' => false
160 * @param string $type
161 * @param bool|false $nameOnly
164 private function getExtensions(
168 $showInstalled = $this->filters['showInstalled'];
169 $showUninstalled = $this->filters['showUninstalled'];
170 $showCore = $this->filters['showCore'];
171 $showNoCore = $this->filters['showNoCore'];
174 if (!array_key_exists($type, $this->extensions)) {
178 foreach ($this->extensions[$type] as $extension) {
179 $name = $extension->getName();
181 $isInstalled = false;
182 if (property_exists($extension, 'status')) {
183 $isInstalled = ($extension->status)?true:false;
185 if (!$showInstalled && $isInstalled) {
188 if (!$showUninstalled && !$isInstalled) {
191 if (!$showCore && $extension->origin == 'core') {
194 if (!$showNoCore && $extension->origin != 'core') {
198 $extensions[$name] = $extension;
202 return $nameOnly?array_keys($extensions):$extensions;
206 * @param string $type
207 * @return \Drupal\Core\Extension\Extension[]
209 private function discoverExtensions($type)
211 if ($type === 'module') {
212 $this->site->loadLegacyFile('/core/modules/system/system.module');
213 system_rebuild_module_data();
216 if ($type === 'theme') {
217 $themeHandler = \Drupal::service('theme_handler');
218 $themeHandler->rebuildThemeData();
222 * @see Remove DrupalExtensionDiscovery subclass once
223 * https://www.drupal.org/node/2503927 is fixed.
225 $discovery = new Discovery($this->appRoot);
228 return $discovery->scan($type);
232 * @param string $name
233 * @return \Drupal\Console\Extension\Extension
235 public function getModule($name)
237 if ($extension = $this->getExtension('module', $name)) {
238 return $this->createExtension($extension);
245 * @param string $name
246 * @return \Drupal\Console\Extension\Extension
248 public function getProfile($name)
250 if ($extension = $this->getExtension('profile', $name)) {
251 return $this->createExtension($extension);
258 * @param string $name
259 * @return \Drupal\Console\Extension\Extension
261 public function getTheme($name)
263 if ($extension = $this->getExtension('theme', $name)) {
264 return $this->createExtension($extension);
271 * @param string $type
272 * @param string $name
274 * @return \Drupal\Core\Extension\Extension
276 private function getExtension($type, $name)
278 if (!$this->extensions[$type]) {
279 $this->discoverExtension($type);
282 if (array_key_exists($name, $this->extensions[$type])) {
283 return $this->extensions[$type][$name];
290 * @param \Drupal\Core\Extension\Extension $extension
291 * @return \Drupal\Console\Extension\Extension
293 private function createExtension($extension)
295 $consoleExtension = new Extension(
297 $extension->getType(),
298 $extension->getPathname(),
299 $extension->getExtensionFilename()
301 $consoleExtension->unserialize($extension->serialize());
303 return $consoleExtension;
307 * @param string $testType
311 public function getTestPath($testType, $fullPath = false)
313 return $this->getPath($fullPath) . '/Tests/' . $testType;
316 public function validateModuleFunctionExist($moduleName, $function, $moduleFile = null)
318 //Load module file to prevent issue of missing functions used in update
319 $module = $this->getModule($moduleName);
320 $modulePath = $module->getPath();
322 $this->site->loadLegacyFile($modulePath . '/'. $moduleFile);
324 $this->site->loadLegacyFile($modulePath . '/' . $module->getName() . '.module');
327 if (function_exists($function)) {
334 * @param string $moduleName
335 * @param string $pluginType
338 public function getPluginPath($moduleName, $pluginType)
340 $module = $this->getModule($moduleName);
342 return $module->getPath() . '/src/Plugin/'.$pluginType;
345 public function getDrupalExtension($type, $name)
347 $extension = $this->getExtension($type, $name);
348 return $this->createExtension($extension);