ApiDocuExtension::_getConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright   Copyright (c) 2016 ublaboo <[email protected]>
7
 * @author      Pavel Janda <[email protected]>
8
 * @package     Ublaboo
9
 */
10
11
namespace Ublaboo\ApiDocu\DI;
12
13
use Nette\DI\CompilerExtension;
14
use Nette\DI\Helpers;
15
use Ublaboo\ApiDocu\Generator;
16
use Ublaboo\ApiDocu\Starter;
17
18
class ApiDocuExtension extends CompilerExtension
19
{
20
21
	/**
22
	 * @var array
23
	 */
24
	protected $config;
25
26
	/**
27
	 * @var array
28
	 */
29
	private $defaults = [
30
		'apiDir' => '%wwwDir%/api',
31
		'httpAuth' => [
32
			'user' => null,
33
			'password' => null,
34
		],
35
	];
36
37
38
	public function loadConfiguration(): void
39
	{
40
		$this->config = $this->_getConfig();
41
	}
42
43
44
	public function beforeCompile(): void
45
	{
46
		$builder = $this->getContainerBuilder();
47
		$config = $this->config;
48
49
		$builder->addDefinition($this->prefix('generator'))
50
			->setClass(Generator::class)
51
			->setArguments([$config['apiDir'], $config['httpAuth']]);
52
53
		$builder->addDefinition($this->prefix('starter'))
54
			->setClass(Starter::class)
55
			->setArguments([
56
				$builder->getDefinition($this->prefix('generator')),
57
				$builder->getDefinition('router'),
58
			])->addTag('run');
59
	}
60
61
62
	protected function _getConfig(): array
63
	{
64
		$config = $this->validateConfig($this->defaults, $this->config);
65
66
		$config['apiDir'] = Helpers::expand(
67
			$config['apiDir'],
68
			$this->getContainerBuilder()->parameters
69
		);
70
71
		return $config;
72
	}
73
}
74