3 namespace Drupal\Console\Extension;
5 use Drupal\Console\Utils\Site;
7 use GuzzleHttp\Exception\ClientException;
10 * Class ExtensionManager
12 * @package Drupal\Console
24 protected $httpClient;
34 private $extensions = [];
39 private $filters = [];
44 private $extension = null;
47 * ExtensionManager constructor.
50 * @param Client $httpClient
51 * @param string $appRoot
53 public function __construct(
59 $this->httpClient = $httpClient;
60 $this->appRoot = $appRoot;
67 public function showInstalled()
69 $this->filters['showInstalled'] = true;
76 public function showUninstalled()
78 $this->filters['showUninstalled'] = true;
85 public function showCore()
87 $this->filters['showCore'] = true;
94 public function showNoCore()
96 $this->filters['showNoCore'] = true;
101 * @param string $nameOnly
104 public function getList($nameOnly)
106 return $this->getExtensions($this->extension, $nameOnly);
112 public function discoverModules()
115 $this->discoverExtension('module');
123 public function discoverThemes()
126 $this->discoverExtension('theme');
134 public function discoverProfiles()
137 $this->discoverExtension('profile');
143 * @param string $extension
145 private function discoverExtension($extension)
147 $this->extension = $extension;
148 $this->extensions[$extension] = $this->discoverExtensions($extension);
156 private function initialize()
158 $this->extension = 'module';
159 $this->extensions = [
165 'showInstalled' => false,
166 'showUninstalled' => false,
168 'showNoCore' => false
173 * @param string $type
174 * @param bool|false $nameOnly
177 private function getExtensions(
181 $showInstalled = $this->filters['showInstalled'];
182 $showUninstalled = $this->filters['showUninstalled'];
183 $showCore = $this->filters['showCore'];
184 $showNoCore = $this->filters['showNoCore'];
187 if (!array_key_exists($type, $this->extensions)) {
191 foreach ($this->extensions[$type] as $extension) {
192 $name = $extension->getName();
194 $isInstalled = false;
195 if (property_exists($extension, 'status')) {
196 $isInstalled = ($extension->status)?true:false;
198 if (!$showInstalled && $isInstalled) {
201 if (!$showUninstalled && !$isInstalled) {
204 if (!$showCore && $extension->origin == 'core') {
207 if (!$showNoCore && $extension->origin != 'core') {
211 $extensions[$name] = $extension;
215 return $nameOnly?array_keys($extensions):$extensions;
219 * @param string $type
220 * @return \Drupal\Core\Extension\Extension[]
222 private function discoverExtensions($type)
224 if ($type === 'module') {
225 $this->site->loadLegacyFile('/core/modules/system/system.module');
226 system_rebuild_module_data();
229 if ($type === 'theme') {
230 $themeHandler = \Drupal::service('theme_handler');
231 $themeHandler->rebuildThemeData();
235 * @see Remove DrupalExtensionDiscovery subclass once
236 * https://www.drupal.org/node/2503927 is fixed.
238 $discovery = new Discovery($this->appRoot);
241 return $discovery->scan($type);
245 * @param string $name
246 * @return \Drupal\Console\Extension\Extension
248 public function getModule($name)
250 if ($extension = $this->getExtension('module', $name)) {
251 return $this->createExtension($extension);
258 * @param string $name
259 * @return \Drupal\Console\Extension\Extension
261 public function getProfile($name)
263 if ($extension = $this->getExtension('profile', $name)) {
264 return $this->createExtension($extension);
271 * @param string $name
272 * @return \Drupal\Console\Extension\Extension
274 public function getTheme($name)
276 if ($extension = $this->getExtension('theme', $name)) {
277 return $this->createExtension($extension);
284 * @param string $type
285 * @param string $name
287 * @return \Drupal\Core\Extension\Extension
289 private function getExtension($type, $name)
291 if (!$this->extensions[$type]) {
292 $this->discoverExtension($type);
295 if (array_key_exists($name, $this->extensions[$type])) {
296 return $this->extensions[$type][$name];
303 * @param \Drupal\Core\Extension\Extension $extension
304 * @return \Drupal\Console\Extension\Extension
306 private function createExtension($extension)
308 $consoleExtension = new Extension(
310 $extension->getType(),
311 $extension->getPathname(),
312 $extension->getExtensionFilename()
314 $consoleExtension->unserialize($extension->serialize());
316 return $consoleExtension;
320 * @param string $testType
324 public function getTestPath($testType, $fullPath = false)
326 return $this->getPath($fullPath) . '/Tests/' . $testType;
329 public function validateModuleFunctionExist($moduleName, $function, $moduleFile = null)
331 //Load module file to prevent issue of missing functions used in update
332 $module = $this->getModule($moduleName);
333 $modulePath = $module->getPath();
335 $this->site->loadLegacyFile($modulePath . '/'. $moduleFile);
337 $this->site->loadLegacyFile($modulePath . '/' . $module->getName() . '.module');
340 if (function_exists($function)) {
347 * @param string $moduleName
348 * @param string $pluginType
351 public function getPluginPath($moduleName, $pluginType)
353 $module = $this->getModule($moduleName);
355 return $module->getPath() . '/src/Plugin/'.$pluginType;
358 public function getDrupalExtension($type, $name)
360 $extension = $this->getExtension($type, $name);
361 return $this->createExtension($extension);
365 * @param array $extensions
366 * @param string $type
369 public function checkExtensions(array $extensions, $type = 'module')
372 'local_extensions' => [],
373 'drupal_extensions' => [],
374 'no_extensions' => [],
377 $local_extensions = $this->discoverExtension($type)
384 foreach ($extensions as $extension) {
385 if (in_array($extension, $local_extensions)) {
386 $checkextensions['local_extensions'][] = $extension;
389 $response = $this->httpClient->head('https://www.drupal.org/project/' . $extension);
390 $header_link = explode(';', $response->getHeader('link'));
391 if (empty($header_link[0])) {
392 $checkextensions['no_extensions'][] = $extension;
394 $checkextensions['drupal_extensions'][] = $extension;
396 } catch (ClientException $e) {
397 $checkextensions['no_extensions'][] = $extension;
402 return $checkextensions;