Skip to content

Commit 8c1d2b7

Browse files
committed
Issue #4265: Load service directly and not with DI.
1 parent df6df11 commit 8c1d2b7

2 files changed

Lines changed: 82 additions & 65 deletions

File tree

src/Extension/Manager.php

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
use Drupal\Core\Extension\ThemeHandler;
1010
use Drupal\Core\Extension\ModuleHandlerInterface;
1111

12+
ini_set('display_errors', 1);
13+
ini_set('display_startup_errors', 1);
14+
error_reporting(E_ALL);
15+
1216
/**
1317
* Class ExtensionManager
1418
*
1519
* @package Drupal\Console
1620
*/
1721
class Manager
1822
{
23+
1924
/**
2025
* @var Site
2126
*/
@@ -46,37 +51,21 @@ class Manager
4651
*/
4752
private $extension = null;
4853

49-
/**
50-
* @var ModuleHandlerInterface
51-
*/
52-
protected $moduleHandler;
53-
54-
/**
55-
* @var ThemeHandler
56-
*/
57-
protected $themeHandler;
58-
5954
/**
6055
* ExtensionManager constructor.
6156
*
62-
* @param Site $site
63-
* @param Client $httpClient
64-
* @param string $appRoot
65-
* @param ModuleHandlerInterface $moduleHandler
66-
* @param ThemeHandler $themeHandler
57+
* @param Site $site
58+
* @param Client $httpClient
59+
* @param string $appRoot
6760
*/
6861
public function __construct(
6962
Site $site,
7063
Client $httpClient,
71-
$appRoot,
72-
ModuleHandlerInterface $moduleHandler,
73-
ThemeHandler $themeHandler
64+
$appRoot
7465
) {
75-
$this->site = $site;
66+
$this->site = $site;
7667
$this->httpClient = $httpClient;
77-
$this->appRoot = $appRoot;
78-
$this->moduleHandler = $moduleHandler;
79-
$this->themeHandler = $themeHandler;
68+
$this->appRoot = $appRoot;
8069
$this->initialize();
8170
}
8271

@@ -86,6 +75,7 @@ public function __construct(
8675
public function showInstalled()
8776
{
8877
$this->filters['showInstalled'] = true;
78+
8979
return $this;
9080
}
9181

@@ -95,6 +85,7 @@ public function showInstalled()
9585
public function showUninstalled()
9686
{
9787
$this->filters['showUninstalled'] = true;
88+
9889
return $this;
9990
}
10091

@@ -104,6 +95,7 @@ public function showUninstalled()
10495
public function showCore()
10596
{
10697
$this->filters['showCore'] = true;
98+
10799
return $this;
108100
}
109101

@@ -113,11 +105,13 @@ public function showCore()
113105
public function showNoCore()
114106
{
115107
$this->filters['showNoCore'] = true;
108+
116109
return $this;
117110
}
118111

119112
/**
120-
* @param boolean $nameOnly
113+
* @param boolean $nameOnly
114+
*
121115
* @return array
122116
*/
123117
public function getList($nameOnly = false)
@@ -159,11 +153,11 @@ public function discoverProfiles()
159153
}
160154

161155
/**
162-
* @param string $extension
156+
* @param string $extension
163157
*/
164158
private function discoverExtension($extension)
165159
{
166-
$this->extension = $extension;
160+
$this->extension = $extension;
167161
$this->extensions[$extension] = $this->discoverExtensions($extension);
168162

169163
return $this;
@@ -174,33 +168,34 @@ private function discoverExtension($extension)
174168
*/
175169
private function initialize()
176170
{
177-
$this->extension = 'module';
171+
$this->extension = 'module';
178172
$this->extensions = [
179-
'module' => [],
180-
'theme' => [],
173+
'module' => [],
174+
'theme' => [],
181175
'profile' => [],
182176
];
183-
$this->filters = [
184-
'showInstalled' => false,
177+
$this->filters = [
178+
'showInstalled' => false,
185179
'showUninstalled' => false,
186-
'showCore' => false,
187-
'showNoCore' => false
180+
'showCore' => false,
181+
'showNoCore' => false,
188182
];
189183
}
190184

191185
/**
192-
* @param string $type
193-
* @param bool|false $nameOnly
186+
* @param string $type
187+
* @param bool|false $nameOnly
188+
*
194189
* @return array
195190
*/
196191
private function getExtensions(
197192
$type = 'module',
198193
$nameOnly = false
199194
) {
200-
$showInstalled = $this->filters['showInstalled'];
195+
$showInstalled = $this->filters['showInstalled'];
201196
$showUninstalled = $this->filters['showUninstalled'];
202-
$showCore = $this->filters['showCore'];
203-
$showNoCore = $this->filters['showNoCore'];
197+
$showCore = $this->filters['showCore'];
198+
$showNoCore = $this->filters['showNoCore'];
204199

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

213-
$isInstalled = $type=='module' && $this->moduleHandler->moduleExists($name);
208+
$isInstalled = $type == 'module'
209+
&& \Drupal::service(
210+
'extension.list.module'
211+
)->moduleExists($name);
214212
if (!$isInstalled && property_exists($extension, 'status')) {
215-
$isInstalled = ($extension->status)?true:false;
213+
$isInstalled = ($extension->status) ? true : false;
216214
}
217215
if (!$showInstalled && $isInstalled) {
218216
continue;
@@ -231,11 +229,12 @@ private function getExtensions(
231229
}
232230

233231

234-
return $nameOnly?array_keys($extensions):$extensions;
232+
return $nameOnly ? array_keys($extensions) : $extensions;
235233
}
236234

237235
/**
238-
* @param string $type
236+
* @param string $type
237+
*
239238
* @return \Drupal\Core\Extension\Extension[]
240239
*/
241240
private function discoverExtensions($type)
@@ -247,7 +246,8 @@ private function discoverExtensions($type)
247246
}
248247

249248
if ($type === 'theme') {
250-
$this->themeHandler->rebuildThemeData();
249+
$theme_handler = \Drupal::service('theme_handler');
250+
$theme_handler->rebuildThemeData();
251251
}
252252

253253
/*
@@ -261,7 +261,8 @@ private function discoverExtensions($type)
261261
}
262262

263263
/**
264-
* @param string $name
264+
* @param string $name
265+
*
265266
* @return \Drupal\Console\Extension\Extension
266267
*/
267268
public function getModule($name)
@@ -274,7 +275,8 @@ public function getModule($name)
274275
}
275276

276277
/**
277-
* @param string $name
278+
* @param string $name
279+
*
278280
* @return \Drupal\Console\Extension\Extension
279281
*/
280282
public function getProfile($name)
@@ -287,7 +289,8 @@ public function getProfile($name)
287289
}
288290

289291
/**
290-
* @param string $name
292+
* @param string $name
293+
*
291294
* @return \Drupal\Console\Extension\Extension
292295
*/
293296
public function getTheme($name)
@@ -300,8 +303,8 @@ public function getTheme($name)
300303
}
301304

302305
/**
303-
* @param string $type
304-
* @param string $name
306+
* @param string $type
307+
* @param string $name
305308
*
306309
* @return \Drupal\Core\Extension\Extension
307310
*/
@@ -319,7 +322,8 @@ private function getExtension($type, $name)
319322
}
320323

321324
/**
322-
* @param \Drupal\Core\Extension\Extension $extension
325+
* @param \Drupal\Core\Extension\Extension $extension
326+
*
323327
* @return \Drupal\Console\Extension\Extension
324328
*/
325329
private function createExtension($extension)
@@ -338,61 +342,71 @@ private function createExtension($extension)
338342
}
339343

340344
/**
341-
* @param string $testType
342-
* @param $fullPath
345+
* @param string $testType
346+
* @param $fullPath
347+
*
343348
* @return string
344349
*/
345350
public function getTestPath($testType, $fullPath = false)
346351
{
347-
return $this->getPath($fullPath) . '/Tests/' . $testType;
352+
return $this->getPath($fullPath).'/Tests/'.$testType;
348353
}
349354

350-
public function validateModuleFunctionExist($moduleName, $function, $moduleFile = null)
351-
{
355+
public function validateModuleFunctionExist(
356+
$moduleName,
357+
$function,
358+
$moduleFile = null
359+
) {
352360
//Load module file to prevent issue of missing functions used in update
353-
$module = $this->getModule($moduleName);
361+
$module = $this->getModule($moduleName);
354362
$modulePath = $module->getPath();
355363
if ($moduleFile) {
356-
$this->site->loadLegacyFile($modulePath . '/' . $moduleFile);
364+
$this->site->loadLegacyFile($modulePath.'/'.$moduleFile);
357365
} else {
358-
$this->site->loadLegacyFile($modulePath . '/' . $module->getName() . '.module');
366+
$this->site->loadLegacyFile(
367+
$modulePath.'/'.$module->getName().'.module'
368+
);
359369
}
360370

361371
if (function_exists($function)) {
362372
return true;
363373
}
374+
364375
return false;
365376
}
366377

367378
/**
368-
* @param string $moduleName
369-
* @param string $pluginType
379+
* @param string $moduleName
380+
* @param string $pluginType
381+
*
370382
* @return string
371383
*/
372384
public function getPluginPath($moduleName, $pluginType)
373385
{
374386
$module = $this->getModule($moduleName);
375387

376-
return $module->getPath() . '/src/Plugin/' . $pluginType;
388+
return $module->getPath().'/src/Plugin/'.$pluginType;
377389
}
378390

379391
public function getDrupalExtension($type, $name)
380392
{
381393
$extension = $this->getExtension($type, $name);
394+
382395
return $this->createExtension($extension);
383396
}
384397

385398
/**
386-
* @param array $extensions
387-
* @param string $type
399+
* @param array $extensions
400+
* @param string $type
401+
*
388402
* @return array
389403
*/
390404
public function checkExtensions(array $extensions, $type = 'module')
391405
{
392406
$checkextensions = [
393-
'local_extensions' => [],
407+
'local_extensions' => [],
394408
'drupal_extensions' => [],
395-
'no_extensions' => [],
409+
'no_extensions' => [],
396410
];
397411

398412
$local_extensions = $this->discoverExtension($type)
@@ -407,7 +421,9 @@ public function checkExtensions(array $extensions, $type = 'module')
407421
$checkextensions['local_extensions'][] = $extension;
408422
} else {
409423
try {
410-
$response = $this->httpClient->head('https://www.drupal.org/project/' . $extension);
424+
$response = $this->httpClient->head(
425+
'https://www.drupal.org/project/'.$extension
426+
);
411427
$header_link = $response->getHeader('link');
412428
if (empty($header_link[0])) {
413429
$checkextensions['no_extensions'][] = $extension;
@@ -422,4 +438,5 @@ public function checkExtensions(array $extensions, $type = 'module')
422438

423439
return $checkextensions;
424440
}
441+
425442
}

uninstall.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
arguments: ['@app.root', '@console.configuration_manager']
55
console.extension_manager:
66
class: Drupal\Console\Extension\Manager
7-
arguments: ['@console.site', '@http_client', '@app.root', '@module_handler', '@theme_handler']
7+
arguments: ['@console.site', '@http_client', '@app.root']
88
# Commands
99
console.server:
1010
class: Drupal\Console\Command\ServerCommand

0 commit comments

Comments
 (0)