1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Portiny\Console\Adapter\Nette\DI; |
4
|
|
|
|
5
|
|
|
use Nette\DI\CompilerExtension; |
6
|
|
|
use Nette\DI\Statement; |
7
|
|
|
use Nette\Http\Request; |
8
|
|
|
use Nette\Http\UrlScript; |
9
|
|
|
use Nette\Schema\Expect; |
10
|
|
|
use Nette\Schema\Schema; |
11
|
|
|
use stdClass; |
12
|
|
|
use Symfony\Component\Console\Application; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Helper\HelperInterface; |
15
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
16
|
|
|
|
17
|
|
|
class ConsoleExtension extends CompilerExtension |
18
|
|
|
{ |
19
|
|
|
|
20
|
1 |
|
public function getConfigSchema(): Schema |
21
|
|
|
{ |
22
|
1 |
|
return Expect::structure([ |
23
|
1 |
|
'url' => Expect::anyOf(Expect::string(), Expect::null()), |
24
|
1 |
|
'autoExit' => Expect::bool(), |
25
|
1 |
|
'catchExceptions' => Expect::bool(), |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
1 |
|
public function loadConfiguration(): void |
34
|
|
|
{ |
35
|
1 |
|
$config = $this->loadFromFile(__DIR__ . '/../config/config.neon'); |
36
|
1 |
|
$this->loadDefinitionsFromConfig($config['services'] ?: []); |
37
|
|
|
|
38
|
1 |
|
$this->setupApplication(); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
1 |
|
public function beforeCompile(): void |
46
|
|
|
{ |
47
|
1 |
|
if (PHP_SAPI !== 'cli') { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @var stdClass $config */ |
52
|
1 |
|
$config = (object) $this->config; |
53
|
1 |
|
$builder = $this->getContainerBuilder(); |
54
|
1 |
|
$builder->getDefinitionByType(Application::class) |
55
|
1 |
|
->addSetup('setHelperSet', ['@' . $builder->getByType(HelperSet::class)]); |
56
|
|
|
|
57
|
1 |
|
$this->registerConsoleCommands(); |
58
|
1 |
|
$this->registerHelpers(); |
59
|
|
|
|
60
|
1 |
|
if ($config->url !== null && $builder->hasDefinition('http.request')) { |
61
|
1 |
|
$definition = $builder->getDefinition('http.request'); |
62
|
1 |
|
$definition->setFactory(Request::class, [new Statement(UrlScript::class, [$config->url])]); |
|
|
|
|
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
1 |
|
private function setupApplication(): void |
68
|
|
|
{ |
69
|
|
|
/** @var stdClass $config */ |
70
|
1 |
|
$config = (object) $this->config; |
71
|
1 |
|
$builder = $this->getContainerBuilder(); |
72
|
1 |
|
$applicationDefinition = $builder->getDefinitionByType(Application::class); |
73
|
|
|
|
74
|
1 |
|
if ($config->autoExit !== null) { |
75
|
1 |
|
$applicationDefinition->addSetup('setAutoExit', [(bool) $config->autoExit]); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
if ($config->catchExceptions !== null) { |
79
|
1 |
|
$applicationDefinition->addSetup('setCatchExceptions', [(bool) $config->catchExceptions]); |
80
|
|
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
1 |
|
private function registerConsoleCommands(): void |
85
|
|
|
{ |
86
|
1 |
|
$builder = $this->getContainerBuilder(); |
87
|
1 |
|
$applicationDefinition = $builder->getDefinitionByType(Application::class); |
88
|
|
|
|
89
|
1 |
|
$commandDefinitions = $builder->findByType(Command::class); |
90
|
1 |
|
foreach (array_keys($commandDefinitions) as $name) { |
91
|
1 |
|
$applicationDefinition->addSetup('add', ['@' . $name]); |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
1 |
|
private function registerHelpers(): void |
97
|
|
|
{ |
98
|
1 |
|
$builder = $this->getContainerBuilder(); |
99
|
1 |
|
$helperSetDefinition = $builder->getDefinitionByType(HelperSet::class); |
100
|
|
|
|
101
|
1 |
|
foreach ($builder->findByType(HelperInterface::class) as $helperDefinition) { |
102
|
1 |
|
$helperSetDefinition->addSetup('set', ['@' . $helperDefinition->getType(), null]); |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.