Completed
Push — master ( 6e7f82...ad27f2 )
by Tomáš
03:34
created

ConsoleExtension::getConfigSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
		$builder = $this->getContainerBuilder();
48 1
		$builder->getDefinitionByType(Application::class)
49 1
			->addSetup('setHelperSet', ['@' . $builder->getByType(HelperSet::class)]);
50
51 1
		$this->registerConsoleCommands();
52 1
		$this->registerHelpers();
53 1
	}
54
55
56 1
	private function setupApplication(): void
57
	{
58
		/** @var stdClass $config */
59 1
		$config = (object) $this->config;
60 1
		$builder = $this->getContainerBuilder();
61 1
		$applicationDefinition = $builder->getDefinitionByType(Application::class);
62
63 1
		if ($config->autoExit !== null) {
64 1
			$applicationDefinition->addSetup('setAutoExit', [(bool) $config->autoExit]);
65
		}
66
67 1
		if ($config->catchExceptions !== null) {
68 1
			$applicationDefinition->addSetup('setCatchExceptions', [(bool) $config->catchExceptions]);
69
		}
70
71 1
		if ($config->url !== null && $builder->hasDefinition('http.request')) {
72 1
			$definition = $builder->getDefinition('http.request');
73 1
			$definition->setFactory(Request::class, [new Statement(UrlScript::class, [$config->url])]);
74
		}
75 1
	}
76
77
78 1
	private function registerConsoleCommands(): void
79
	{
80 1
		$builder = $this->getContainerBuilder();
81 1
		$applicationDefinition = $builder->getDefinitionByType(Application::class);
82
83 1
		$commandDefinitions = $builder->findByType(Command::class);
84 1
		foreach (array_keys($commandDefinitions) as $name) {
85 1
			$applicationDefinition->addSetup('add', ['@' . $name]);
86
		}
87 1
	}
88
89
90 1
	private function registerHelpers(): void
91
	{
92 1
		$builder = $this->getContainerBuilder();
93 1
		$helperSetDefinition = $builder->getDefinitionByType(HelperSet::class);
94
95 1
		foreach ($builder->findByType(HelperInterface::class) as $helperDefinition) {
96 1
			$helperSetDefinition->addSetup('set', ['@' . $helperDefinition->getType(), null]);
97
		}
98 1
	}
99
100
}
101