Completed
Push — master ( 54c8d8...9da0fd )
by Tomáš
9s
created

SculpinCompilerExtension::expandParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 4
    public function loadConfiguration()
34
    {
35 4
        $config = $this->validateAndReturnConfig($this->defaultConfig);
36 4
        $this->loadConfigToParameters($config);
37 4
        $this->loadServicesFromConfig();
38 4
    }
39
40 3
    public function beforeCompile()
41
    {
42 3
        $this->collectByType(Application::class, Command::class, 'add');
43 3
        $this->collectByType(SourceFileStorage::class, SourceFileFilterInterface::class, 'addSourceFileFilter');
44 3
    }
45
46 4
    private function validateAndReturnConfig(array $defaultConfig) : array
47
    {
48 4
        $defaultConfig = $this->validateConfig($defaultConfig);
49 4
        $defaultConfig['sourceDir'] = $this->expandParameter($defaultConfig['sourceDir']);
50
51 4
        $this->ensureDirectoryExists($defaultConfig['sourceDir']);
52 4
        $defaultConfig['sourceDir'] = realpath($defaultConfig['sourceDir']);
53
54 4
        return $defaultConfig;
55
    }
56
57 4
    private function loadConfigToParameters(array $config)
58
    {
59 4
        $this->getContainerBuilder()->parameters += $config;
60 4
    }
61
62 4
    private function loadServicesFromConfig()
63
    {
64 4
        Compiler::loadDefinitions(
65 4
            $this->getContainerBuilder(),
66 4
            $this->loadFromFile(__DIR__.'/../config/services.neon')['services']
67
        );
68 4
    }
69
70 4
    private function ensureDirectoryExists(string $sourceDir)
71
    {
72 4
        if (!is_dir($sourceDir)) {
73
            throw new \Exception(
74
                sprintf('Source directory "%s" was not found.', $sourceDir)
75
            );
76
        }
77 4
    }
78
79 4
    private function expandParameter(string $value) : string
80
    {
81 4
        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 4
            ->expand($value);
83
    }
84
}
85