Completed
Push — master ( e502a6...b659c5 )
by Michael
08:23
created

ConfigWiring   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 80
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B wire() 0 54 6
A gitVersionSetting() 0 9 4
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class ConfigWiring.
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) 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
 * @author    Michael Cummings <[email protected]>
32
 * @copyright 2016 Michael Cummings
33
 * @license   LGPL-3.0+
34
 */
35
namespace Yapeal\Configuration;
36
37
use Yapeal\Cli\Yapeal\YamlConfigFile;
38
use Yapeal\Container\ContainerInterface;
39
use Yapeal\Container\DicAwareInterface;
40
use Yapeal\Container\DicAwareTrait;
41
42
/**
43
 * Class ConfigWiring.
44
 */
45
class ConfigWiring implements WiringInterface, DicAwareInterface
46
{
47
    use ConfigFileProcessingTrait;
48
    use DicAwareTrait;
49
    /**
50
     * @param ContainerInterface $dic
51
     *
52
     * @throws \DomainException
53
     * @throws \InvalidArgumentException
54
     * @throws \LogicException
55
     */
56
    public function wire(ContainerInterface $dic)
57
    {
58
        $this->setDic($dic);
59
        if (empty($dic['Yapeal.Config.Yaml'])) {
60
            $dic['Yapeal.Config.Yaml'] = $dic->factory(function () {
61
                return new YamlConfigFile();
62
            });
63
        }
64
        $path = dirname(str_replace('\\', '/', __DIR__), 2) . '/';
65
        // These two paths are critical to Yapeal-ng working and can't be overridden.
66
        $dic['Yapeal.baseDir'] = $path;
67
        $dic['Yapeal.libDir'] = $path . 'lib/';
68
        $configFiles = [str_replace('\\', '/', __DIR__) . '/yapealDefaults.yaml'];
69
        $settings = [];
70
        /**
71
         * Do to the importance that the cache/ and log/ directories and the main configuration file _not_ point to
72
         * somewhere under Composer's vendor/ directory they are now forced to use either vendor parent directory or
73
         * Yapeal-ng's base directory depending on if Yapeal-ng finds itself under a vendor/ directory.
74
         *
75
         * If as an application developer you wish to use a different directory than cache/ or log/ in your
76
         * application's root directory you __MUST__ set the 'Yapeal.FileSystem.Cache.dir' and/or 'Yapeal.Log.dir'
77
         * setting(s) of the Container before you give it to the Wiring class to prevent them being changed here when
78
         * Wiring::wireAll() calls this class method during wireAll()'s normal processing. On the command line you have
79
         * the -c or --configFile option to override the settings as well.
80
         *
81
         * Read the existing [Configuration Files](../../docs/config/ConfigurationFiles.md) docs for more about how to
82
         * override the optional config/yapeal.yaml file as well.
83
         */
84
        if (false !== $vendorPos = strpos($path, 'vendor/')) {
85
            $dic['Yapeal.vendorParentDir'] = substr($path, 0, $vendorPos);
86
            $configFiles[] = $dic['Yapeal.vendorParentDir'] . 'config/yapeal.yaml';
87
            $settings['Yapeal.FileSystem.Cache.dir'] = '{Yapeal.vendorParentDir}cache/';
88
            $settings['Yapeal.Log.dir'] = '{Yapeal.vendorParentDir}log/';
89
        } else {
90
            $configFiles[] = $path . 'config/yapeal.yaml';
91
            $settings['Yapeal.FileSystem.Cache.dir'] = $path . 'cache/';
92
            $settings['Yapeal.Log.dir'] = $path . 'log/';
93
        }
94
        if (!empty($dic['Yapeal.Config.configFile'])) {
95
            $configFiles[] = $dic['Yapeal.Config.configFile'];
96
            unset($dic['Yapeal.Config.configFile']);
97
        }
98
        // Process each file in turn so any substitutions are done in a more
99
        // consistent way.
100
        foreach ($configFiles as $configFile) {
101
            $settings = $this->parserConfigFile($configFile, $settings);
102
        }
103
        $settings = $this->gitVersionSetting($settings);
104
        $settings = $this->doSubstitutions($settings, $dic);
105
        $additions = array_diff(array_keys($settings), $dic->keys());
106
        foreach ($additions as $add) {
107
            $dic[$add] = $settings[$add];
108
        }
109
    }
110
    /**
111
     * @param $settings
112
     *
113
     * @return array
114
     */
115
    private function gitVersionSetting($settings): array
116
    {
117
        $version = $settings['Yapeal.version'] ?? '0.0.0-0-dev+noGit-unknown';
118
        $gitVersion = trim(exec('git describe --always --long 2>&1', $junk, $status));
119
        if (0 === $status && '' !== $gitVersion && $gitVersion !== $version) {
120
            $settings['Yapeal.version'] = $gitVersion . '-dev';
121
        }
122
        return $settings;
123
    }
124
}
125