|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Portiny\Console\Adapter\Nette\DI; |
|
6
|
|
|
|
|
7
|
|
|
use Nette\DI\Compiler; |
|
8
|
|
|
use Nette\DI\CompilerExtension; |
|
9
|
|
|
use Nette\Http\IRequest; |
|
10
|
|
|
use Nette\Http\RequestFactory as NetteRequestFactory; |
|
11
|
|
|
use Portiny\Console\Http\HttpRequestFactory; |
|
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
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $defaults = [ |
|
23
|
|
|
'url' => null, |
|
24
|
|
|
'autoExit' => null, |
|
25
|
|
|
'catchExceptions' => null, |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function loadConfiguration(): void |
|
32
|
|
|
{ |
|
33
|
1 |
|
$builder = $this->getContainerBuilder(); |
|
34
|
1 |
|
$config = $this->loadFromFile(__DIR__ . '/../config/config.neon'); |
|
35
|
1 |
|
Compiler::loadDefinitions($builder, $config['services'] ?: []); |
|
36
|
|
|
|
|
37
|
1 |
|
$this->setupApplication(); |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function beforeCompile(): void |
|
44
|
|
|
{ |
|
45
|
1 |
|
$builder = $this->getContainerBuilder(); |
|
46
|
1 |
|
$builder->getDefinitionByType(Application::class) |
|
47
|
1 |
|
->addSetup('setHelperSet', ['@' . $builder->getByType(HelperSet::class)]); |
|
48
|
|
|
|
|
49
|
1 |
|
$this->registerConsoleCommands(); |
|
50
|
1 |
|
$this->registerHelpers(); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
private function setupApplication(): void |
|
54
|
|
|
{ |
|
55
|
1 |
|
$config = $this->validateConfig($this->defaults); |
|
56
|
1 |
|
$builder = $this->getContainerBuilder(); |
|
57
|
1 |
|
$applicationDefinition = $builder->getDefinitionByType(Application::class); |
|
58
|
|
|
|
|
59
|
1 |
|
if ($config['autoExit'] !== null) { |
|
60
|
1 |
|
$applicationDefinition->addSetup('setAutoExit', [(bool) $config['autoExit']]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
if ($config['catchExceptions'] !== null) { |
|
64
|
1 |
|
$applicationDefinition->addSetup('setCatchExceptions', [(bool) $config['catchExceptions']]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
if (interface_exists(IRequest::class) && $config['url']) { |
|
68
|
1 |
|
$builder->getDefinition($builder->getByType(NetteRequestFactory::class) ?: 'nette.httpRequestFactory') |
|
69
|
1 |
|
->setFactory(HttpRequestFactory::class) |
|
70
|
1 |
|
->addSetup('setRequestUrl', [$config['url']]); |
|
71
|
|
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
private function registerConsoleCommands(): void |
|
75
|
|
|
{ |
|
76
|
1 |
|
$builder = $this->getContainerBuilder(); |
|
77
|
1 |
|
$applicationDefinition = $builder->getDefinitionByType(Application::class); |
|
78
|
|
|
|
|
79
|
1 |
|
$commandDefinitions = $builder->findByType(Command::class); |
|
80
|
1 |
|
foreach (array_keys($commandDefinitions) as $name) { |
|
81
|
1 |
|
$applicationDefinition->addSetup('add', ['@' . $name]); |
|
82
|
|
|
} |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
private function registerHelpers(): void |
|
86
|
|
|
{ |
|
87
|
1 |
|
$builder = $this->getContainerBuilder(); |
|
88
|
1 |
|
$helperSetDefinition = $builder->getDefinitionByType(HelperSet::class); |
|
89
|
|
|
|
|
90
|
1 |
|
foreach ($builder->findByType(HelperInterface::class) as $helperDefinition) { |
|
91
|
1 |
|
$helperSetDefinition->addSetup('set', ['@' . $helperDefinition->getType(), null]); |
|
92
|
|
|
} |
|
93
|
1 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|