Passed
Push — master ( 5f65c4...8094be )
by Tomáš
02:41
created

ConsoleExtension::beforeCompile()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 11
c 2
b 1
f 0
nc 3
nop 0
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.9
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