|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Symplify |
|
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Symplify\MultiCodingStandard\DI; |
|
9
|
|
|
|
|
10
|
|
|
use Nette\DI\CompilerExtension; |
|
11
|
|
|
use Nette\DI\Helpers; |
|
12
|
|
|
use Nette\DI\ServiceDefinition; |
|
13
|
|
|
use Symfony\Component\Console\Command\Command; |
|
14
|
|
|
use Symplify\MultiCodingStandard\Console\Application; |
|
15
|
|
|
|
|
16
|
|
|
final class MultiCodingStandardExtension extends CompilerExtension |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $defaults = [ |
|
22
|
|
|
'configPath' => '%appDir%/../multi-cs.json' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function loadConfiguration() |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->setConfigToContainerBuilder($this->defaults); |
|
31
|
4 |
|
$this->loadServicesFromConfig(); |
|
32
|
4 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function beforeCompile() |
|
38
|
|
|
{ |
|
39
|
4 |
|
$this->loadCommandsToConsoleApplication(); |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
private function loadServicesFromConfig() |
|
43
|
|
|
{ |
|
44
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
|
45
|
4 |
|
$config = $this->loadFromFile(__DIR__.'/../config/services.neon'); |
|
46
|
4 |
|
$this->compiler->parseServices($containerBuilder, $config); |
|
|
|
|
|
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
private function loadCommandsToConsoleApplication() |
|
50
|
|
|
{ |
|
51
|
4 |
|
$consoleApplication = $this->getDefinitionByType(Application::class); |
|
52
|
|
|
|
|
53
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
|
54
|
4 |
|
foreach ($containerBuilder->findByType(Command::class) as $definition) { |
|
55
|
4 |
|
$consoleApplication->addSetup('add', ['@'.$definition->getClass()]); |
|
56
|
|
|
} |
|
57
|
4 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $type |
|
61
|
|
|
* |
|
62
|
|
|
* @return ServiceDefinition |
|
63
|
|
|
*/ |
|
64
|
4 |
|
private function getDefinitionByType($type) |
|
65
|
|
|
{ |
|
66
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
|
67
|
4 |
|
$definitionName = $containerBuilder->getByType($type); |
|
68
|
|
|
|
|
69
|
4 |
|
return $containerBuilder->getDefinition($definitionName); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string[] $defaults |
|
74
|
|
|
*/ |
|
75
|
4 |
|
private function setConfigToContainerBuilder(array $defaults) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
4 |
|
$config = $this->validateConfig($this->defaults); |
|
78
|
4 |
|
$config['configPath'] = Helpers::expand($config['configPath'], $this->getContainerBuilder()->parameters); |
|
79
|
4 |
|
$this->getContainerBuilder()->parameters += $config; |
|
80
|
4 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.