Completed
Push — master ( 2386c7...c56f94 )
by Tomáš
02:42
created

SculpinCompilerExtension::loadConfiguration()   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
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\DI;
11
12
use Nette\DI\Compiler;
13
use Nette\DI\CompilerExtension;
14
use Symfony\Component\Console\Command\Command;
15
use Symplify\PHP7_Sculpin\Console\Application;
16
use Symplify\PHP7_Sculpin\Contract\Source\SourceFileFilter\SourceFileFilterInterface;
17
use Symplify\PHP7_Sculpin\DI\Helper\TypeAndCollectorTrait;
18
use Symplify\PHP7_Sculpin\Source\SourceFileStorage;
19
20
final class SculpinCompilerExtension extends CompilerExtension
21
{
22
    use TypeAndCollectorTrait;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $defaultConfig = [
28
        'sourceDir' => '%appDir%/../../../source',
29
        'outputDir' => '%appDir%/../../../output',
30
        'postRoute' => 'blog/:year/:month/:day/:filename',
31
    ];
32
33 7
    public function loadConfiguration()
34
    {
35 7
        $config = $this->validateAndReturnConfig($this->defaultConfig);
36 6
        $this->loadConfigToParameters($config);
37 6
        $this->loadServicesFromConfig();
38 6
    }
39
40 5
    public function beforeCompile()
41
    {
42 5
        $this->collectByType(Application::class, Command::class, 'add');
43 5
        $this->collectByType(SourceFileStorage::class, SourceFileFilterInterface::class, 'addSourceFileFilter');
44 5
    }
45
46 7
    private function validateAndReturnConfig(array $defaultConfig) : array
47
    {
48 7
        $defaultConfig = $this->validateConfig($defaultConfig);
49 7
        $defaultConfig['sourceDir'] = $this->expandParameter($defaultConfig['sourceDir']);
50
51 7
        $this->ensureDirectoryExists($defaultConfig['sourceDir']);
52 6
        $defaultConfig['sourceDir'] = realpath($defaultConfig['sourceDir']);
53
54 6
        return $defaultConfig;
55
    }
56
57 6
    private function loadConfigToParameters(array $config)
58
    {
59 6
        $this->getContainerBuilder()->parameters += $config;
60 6
    }
61
62 6
    private function loadServicesFromConfig()
63
    {
64 6
        Compiler::loadDefinitions(
65 6
            $this->getContainerBuilder(),
66 6
            $this->loadFromFile(__DIR__.'/../config/services.neon')['services']
67
        );
68 6
    }
69
70 7
    private function ensureDirectoryExists(string $sourceDir)
71
    {
72 7
        if (!is_dir($sourceDir)) {
73 1
            throw new \Exception(
74 1
                sprintf('Source directory "%s" was not found.', $sourceDir)
75
            );
76
        }
77 6
    }
78
79 7
    private function expandParameter(string $value) : string
80
    {
81 7
        return $this->getContainerBuilder()
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ContainerBuilder::expand() has been deprecated.

This method has been deprecated.

Loading history...
82 7
            ->expand($value);
83
    }
84
}
85