Completed
Push — master ( b378f9...f97949 )
by Michael
03:14
created

Wiring::parserConfigFile()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 1 Features 1
Metric Value
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 8.439
c 3
b 1
f 1
cc 6
eloc 20
nc 5
nop 2
crap 42
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains Wiring class.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2014-2016 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @copyright 2014-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Configuration;
36
37
use Yapeal\Container\ContainerInterface;
38
use Yapeal\DicAwareInterface;
39
use Yapeal\DicAwareTrait;
40
41
/**
42
 * Class Wiring
43
 */
44
class Wiring implements DicAwareInterface
45
{
46
    use ConfigFileProcessingTrait, DicAwareTrait;
47
    /**
48
     * @param ContainerInterface $dic
49
     */
50 1
    public function __construct(ContainerInterface $dic)
51
    {
52 1
        $this->setDic($dic);
53
    }
54
    /**
55
     * @return self Fluent interface.
56
     * @throws \LogicException
57
     */
58
    public function wireAll()
59
    {
60
        $dic = $this->dic;
61
        $base = 'Yapeal.Wiring.Handlers.';
62
        $names = ['Config', 'Error', 'Event', 'Log', 'Sql', 'Xml', 'Xsd', 'Xsl', 'FileSystem', 'Network', 'EveApi'];
63
        /**
64
         * @var WiringInterface $class
65
         */
66
        foreach ($names as $name) {
67
            $setting = $base . strtolower($name);
68
            if (!empty($dic[$setting])
69
                && is_subclass_of($dic[$setting], '\\Yapeal\\Configuration\\WiringInterface', true)
70
            ) {
71
                $class = new $dic[$setting];
72
                $class->wire($dic);
73
                continue;
74
            }
75
            $methodName = 'wire' . $name;
76
            if (method_exists($this, $methodName)) {
77
                $this->$methodName();
78
            } else {
79
                $mess = 'Could NOT find class or method for ' . $name;
80
                throw new \LogicException($mess);
81
            }
82
        }
83
        return $this;
84
    }
85
    /**
86
     * @return void
87
     * @throws \DomainException
88
     * @throws \LogicException
89
     * @throws \Yapeal\Exception\YapealException
90
     */
91
    protected function wireConfig()
92
    {
93
        $dic = $this->getDic();
94
        $path = str_replace('\\', '/', dirname(dirname(__DIR__))) . '/';
95
        // These two paths are critical to Yapeal-ng working and can't be overridden here.
96
        $dic['Yapeal.baseDir'] = $path;
97
        $dic['Yapeal.libDir'] = $path . 'lib/';
98
        $configFiles = [
99
            __DIR__ . '/yapeal_defaults.yaml',
100
            $path . 'config/yapeal.yaml'
101
        ];
102
        $vendorPos = strpos($path, 'vendor/');
103
        if (false !== $vendorPos) {
104
            $dic['Yapeal.vendorParentDir'] = substr($path, 0, $vendorPos);
105
            $configFiles[] = $dic['Yapeal.vendorParentDir'] . 'config/yapeal.yaml';
106
        }
107
        $settings = [];
108
        // Process each file in turn so any substitutions are done in a more
109
        // consistent way.
110
        foreach ($configFiles as $configFile) {
111
            $settings = $this->parserConfigFile($configFile, $settings);
112
        }
113
        $settings = $this->doSubstitutions($settings, $dic);
114
        if (0 !== count($settings)) {
115
            // Assure NOT overwriting already existing settings given by application.
116
            foreach ($settings as $key => $value) {
117
                $dic[$key] = $dic[$key] ?? $value;
118
            }
119
        }
120
    }
121
}
122