Skip to content

Commit

Permalink
Issue #4265: Load service directly and not with DI.
Browse files Browse the repository at this point in the history
  • Loading branch information
LOBsTerr committed Sep 17, 2022
1 parent df6df11 commit 8c1d2b7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 65 deletions.
145 changes: 81 additions & 64 deletions src/Extension/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
use Drupal\Core\Extension\ThemeHandler;
use Drupal\Core\Extension\ModuleHandlerInterface;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/**
* Class ExtensionManager
*
* @package Drupal\Console
*/
class Manager
{

/**
* @var Site
*/
Expand Down Expand Up @@ -46,37 +51,21 @@ class Manager
*/
private $extension = null;

/**
* @var ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* @var ThemeHandler
*/
protected $themeHandler;

/**
* ExtensionManager constructor.
*
* @param Site $site
* @param Client $httpClient
* @param string $appRoot
* @param ModuleHandlerInterface $moduleHandler
* @param ThemeHandler $themeHandler
* @param Site $site
* @param Client $httpClient
* @param string $appRoot
*/
public function __construct(
Site $site,
Client $httpClient,
$appRoot,
ModuleHandlerInterface $moduleHandler,
ThemeHandler $themeHandler
$appRoot
) {
$this->site = $site;
$this->site = $site;
$this->httpClient = $httpClient;
$this->appRoot = $appRoot;
$this->moduleHandler = $moduleHandler;
$this->themeHandler = $themeHandler;
$this->appRoot = $appRoot;
$this->initialize();
}

Expand All @@ -86,6 +75,7 @@ public function __construct(
public function showInstalled()
{
$this->filters['showInstalled'] = true;

return $this;
}

Expand All @@ -95,6 +85,7 @@ public function showInstalled()
public function showUninstalled()
{
$this->filters['showUninstalled'] = true;

return $this;
}

Expand All @@ -104,6 +95,7 @@ public function showUninstalled()
public function showCore()
{
$this->filters['showCore'] = true;

return $this;
}

Expand All @@ -113,11 +105,13 @@ public function showCore()
public function showNoCore()
{
$this->filters['showNoCore'] = true;

return $this;
}

/**
* @param boolean $nameOnly
* @param boolean $nameOnly
*
* @return array
*/
public function getList($nameOnly = false)
Expand Down Expand Up @@ -159,11 +153,11 @@ public function discoverProfiles()
}

/**
* @param string $extension
* @param string $extension
*/
private function discoverExtension($extension)
{
$this->extension = $extension;
$this->extension = $extension;
$this->extensions[$extension] = $this->discoverExtensions($extension);

return $this;
Expand All @@ -174,33 +168,34 @@ private function discoverExtension($extension)
*/
private function initialize()
{
$this->extension = 'module';
$this->extension = 'module';
$this->extensions = [
'module' => [],
'theme' => [],
'module' => [],
'theme' => [],
'profile' => [],
];
$this->filters = [
'showInstalled' => false,
$this->filters = [
'showInstalled' => false,
'showUninstalled' => false,
'showCore' => false,
'showNoCore' => false
'showCore' => false,
'showNoCore' => false,
];
}

/**
* @param string $type
* @param bool|false $nameOnly
* @param string $type
* @param bool|false $nameOnly
*
* @return array
*/
private function getExtensions(
$type = 'module',
$nameOnly = false
) {
$showInstalled = $this->filters['showInstalled'];
$showInstalled = $this->filters['showInstalled'];
$showUninstalled = $this->filters['showUninstalled'];
$showCore = $this->filters['showCore'];
$showNoCore = $this->filters['showNoCore'];
$showCore = $this->filters['showCore'];
$showNoCore = $this->filters['showNoCore'];

$extensions = [];
if (!array_key_exists($type, $this->extensions)) {
Expand All @@ -210,9 +205,12 @@ private function getExtensions(
foreach ($this->extensions[$type] as $extension) {
$name = $extension->getName();

$isInstalled = $type=='module' && $this->moduleHandler->moduleExists($name);
$isInstalled = $type == 'module'
&& \Drupal::service(
'extension.list.module'
)->moduleExists($name);
if (!$isInstalled && property_exists($extension, 'status')) {
$isInstalled = ($extension->status)?true:false;
$isInstalled = ($extension->status) ? true : false;
}
if (!$showInstalled && $isInstalled) {
continue;
Expand All @@ -231,11 +229,12 @@ private function getExtensions(
}


return $nameOnly?array_keys($extensions):$extensions;
return $nameOnly ? array_keys($extensions) : $extensions;
}

/**
* @param string $type
* @param string $type
*
* @return \Drupal\Core\Extension\Extension[]
*/
private function discoverExtensions($type)
Expand All @@ -247,7 +246,8 @@ private function discoverExtensions($type)
}

if ($type === 'theme') {
$this->themeHandler->rebuildThemeData();
$theme_handler = \Drupal::service('theme_handler');
$theme_handler->rebuildThemeData();
}

/*
Expand All @@ -261,7 +261,8 @@ private function discoverExtensions($type)
}

/**
* @param string $name
* @param string $name
*
* @return \Drupal\Console\Extension\Extension
*/
public function getModule($name)
Expand All @@ -274,7 +275,8 @@ public function getModule($name)
}

/**
* @param string $name
* @param string $name
*
* @return \Drupal\Console\Extension\Extension
*/
public function getProfile($name)
Expand All @@ -287,7 +289,8 @@ public function getProfile($name)
}

/**
* @param string $name
* @param string $name
*
* @return \Drupal\Console\Extension\Extension
*/
public function getTheme($name)
Expand All @@ -300,8 +303,8 @@ public function getTheme($name)
}

/**
* @param string $type
* @param string $name
* @param string $type
* @param string $name
*
* @return \Drupal\Core\Extension\Extension
*/
Expand All @@ -319,7 +322,8 @@ private function getExtension($type, $name)
}

/**
* @param \Drupal\Core\Extension\Extension $extension
* @param \Drupal\Core\Extension\Extension $extension
*
* @return \Drupal\Console\Extension\Extension
*/
private function createExtension($extension)
Expand All @@ -338,61 +342,71 @@ private function createExtension($extension)
}

/**
* @param string $testType
* @param $fullPath
* @param string $testType
* @param $fullPath
*
* @return string
*/
public function getTestPath($testType, $fullPath = false)
{
return $this->getPath($fullPath) . '/Tests/' . $testType;
return $this->getPath($fullPath).'/Tests/'.$testType;
}

public function validateModuleFunctionExist($moduleName, $function, $moduleFile = null)
{
public function validateModuleFunctionExist(
$moduleName,
$function,
$moduleFile = null
) {
//Load module file to prevent issue of missing functions used in update
$module = $this->getModule($moduleName);
$module = $this->getModule($moduleName);
$modulePath = $module->getPath();
if ($moduleFile) {
$this->site->loadLegacyFile($modulePath . '/' . $moduleFile);
$this->site->loadLegacyFile($modulePath.'/'.$moduleFile);
} else {
$this->site->loadLegacyFile($modulePath . '/' . $module->getName() . '.module');
$this->site->loadLegacyFile(
$modulePath.'/'.$module->getName().'.module'
);
}

if (function_exists($function)) {
return true;
}

return false;
}

/**
* @param string $moduleName
* @param string $pluginType
* @param string $moduleName
* @param string $pluginType
*
* @return string
*/
public function getPluginPath($moduleName, $pluginType)
{
$module = $this->getModule($moduleName);

return $module->getPath() . '/src/Plugin/' . $pluginType;
return $module->getPath().'/src/Plugin/'.$pluginType;
}

public function getDrupalExtension($type, $name)
{
$extension = $this->getExtension($type, $name);

return $this->createExtension($extension);
}

/**
* @param array $extensions
* @param string $type
* @param array $extensions
* @param string $type
*
* @return array
*/
public function checkExtensions(array $extensions, $type = 'module')
{
$checkextensions = [
'local_extensions' => [],
'local_extensions' => [],
'drupal_extensions' => [],
'no_extensions' => [],
'no_extensions' => [],
];

$local_extensions = $this->discoverExtension($type)
Expand All @@ -407,7 +421,9 @@ public function checkExtensions(array $extensions, $type = 'module')
$checkextensions['local_extensions'][] = $extension;
} else {
try {
$response = $this->httpClient->head('https://www.drupal.org/project/' . $extension);
$response = $this->httpClient->head(
'https://www.drupal.org/project/'.$extension
);
$header_link = $response->getHeader('link');
if (empty($header_link[0])) {
$checkextensions['no_extensions'][] = $extension;
Expand All @@ -422,4 +438,5 @@ public function checkExtensions(array $extensions, $type = 'module')

return $checkextensions;
}

}
2 changes: 1 addition & 1 deletion uninstall.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
arguments: ['@app.root', '@console.configuration_manager']
console.extension_manager:
class: Drupal\Console\Extension\Manager
arguments: ['@console.site', '@http_client', '@app.root', '@module_handler', '@theme_handler']
arguments: ['@console.site', '@http_client', '@app.root']
# Commands
console.server:
class: Drupal\Console\Command\ServerCommand
Expand Down

0 comments on commit 8c1d2b7

Please sign in to comment.