Completed
Push — master ( 5da014...0c1655 )
by Sebastian
02:43
created

File::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
namespace phpbu\App\Configuration\Loader;
3
4
use phpbu\App\Adapter\Util;
5
use phpbu\App\Configuration;
6
use phpbu\App\Exception;
7
use phpbu\App\Factory as AppFactory;
8
use phpbu\App\Runner\Bootstrap;
9
use phpbu\App\Util\Cli;
10
11
/**
12
 * Base class for file based phpbu configuration.
13
 *
14
 * @package    phpbu
15
 * @subpackage App
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       https://phpbu.de/
20
 * @since      Class available since Release 2.1.2
21
 */
22
abstract class File
23
{
24
    /**
25
     * Path to config file.
26
     *
27
     * @var string
28
     */
29
    protected $filename;
30
31
    /**
32
     * Path to config file.
33
     *
34
     * @var array
35
     */
36
    protected $adapters;
37
38
    /**
39
     * Handling the bootstrapping.
40
     *
41
     * @var \phpbu\App\Configuration\Bootstrapper
42
     */
43
    protected $bootstrapper;
44
45
    /**
46
     * File constructor.
47
     *
48
     * @param string                                $file
49
     * @param \phpbu\App\Configuration\Bootstrapper $bootstrapper
50
     */
51 39
    public function __construct(string $file, Configuration\Bootstrapper $bootstrapper)
52
    {
53 39
        $this->filename     = $file;
54 39
        $this->bootstrapper = $bootstrapper;
55 39
    }
56
57
    /**
58
     * Returns the phpbu Configuration.
59
     *
60
     * @param  \phpbu\App\Factory $factory
61
     * @return \phpbu\App\Configuration
62
     * @throws \phpbu\App\Exception
63
     */
64 33
    public function getConfiguration(AppFactory $factory)
65
    {
66
        // create configuration first so the working directory is available for all adapters
67 33
        $configuration = new Configuration();
68 33
        $configuration->setFilename($this->filename);
69
70 33
        $this->setAppSettings($configuration);
71 33
        $this->handleBootstrap($configuration);
72 33
        $this->setupAdapters($factory);
73
74 29
        $this->setLoggers($configuration);
75 27
        $this->setBackups($configuration);
76
77 13
        return $configuration;
78
    }
79
80
    /**
81
     * Load all available config adapters.
82
     *
83
     * @param  \phpbu\App\Factory $factory
84
     * @throws \phpbu\App\Exception
85
     */
86 33
    protected function setupAdapters(AppFactory $factory)
87
    {
88 33
        foreach ($this->getAdapterConfigs() as $config) {
89 4
            $this->adapters[$config->name] = $factory->createAdapter($config->type, $config->options);
90
        }
91 29
    }
92
93
    /**
94
     * Return a registered adapter.
95
     *
96
     * @param  string $name
97
     * @return \phpbu\App\Adapter
98
     * @throws \phpbu\App\Exception
99
     */
100 4
    protected function getAdapter($name)
101
    {
102 4
        if (!isset($this->adapters[$name])) {
103 1
            throw new Exception('no adapter registered with name: ' . $name);
104
        }
105 3
        return $this->adapters[$name];
106
    }
107
108
    /**
109
     * Return list of adapter configs.
110
     *
111
     * @return array
112
     */
113
    abstract protected function getAdapterConfigs();
114
115
    /**
116
     * Set the phpbu application settings.
117
     *
118
     * @param  \phpbu\App\Configuration $configuration
119
     */
120
    abstract public function setAppSettings(Configuration $configuration);
121
122
    /**
123
     * Set the log configuration.
124
     *
125
     * @param  \phpbu\App\Configuration $configuration
126
     * @throws \phpbu\App\Exception
127
     */
128
    abstract public function setLoggers(Configuration $configuration);
129
130
    /**
131
     * Set the backup configurations.
132
     *
133
     * @param  \phpbu\App\Configuration $configuration
134
     * @throws \phpbu\App\Exception
135
     */
136
    abstract public function setBackups(Configuration $configuration);
137
138
    /**
139
     * Handles the bootstrap file inclusion.
140
     *
141
     * @param  \phpbu\App\Configuration $configuration
142
     * @throws \phpbu\App\Exception
143
     */
144 33
    protected function handleBootstrap(Configuration $configuration)
145
    {
146 33
        $this->bootstrapper->run($configuration);
147 33
    }
148
149
    /**
150
     * Converts a path to an absolute one if necessary.
151
     *
152
     * @param  string  $path
153
     * @param  boolean $useIncludePath
154
     * @return string
155
     */
156 32
    protected function toAbsolutePath($path, $useIncludePath = false)
157
    {
158 32
        return Cli::toAbsolutePath($path, dirname($this->filename), $useIncludePath);
159
    }
160
161
    /**
162
     * Return option value.
163
     * Checks if the value should be fetched from an Adapter, if not it just returns the value.
164
     *
165
     * @param  string $value
166
     * @return string
167
     * @throws \phpbu\App\Exception
168
     */
169 29
    protected function getAdapterizedValue($value)
170
    {
171 29
        foreach (Util::getAdapterReplacements($value) as $replacement) {
172 4
            $search  = $replacement['search'];
173 4
            $replace = $this->getAdapter($replacement['adapter'])->getValue($replacement['path']);
174 3
            $value   = str_replace($search, $replace, $value);
175
        }
176 29
        return $value;
177
    }
178
179
    /**
180
     * Load the file.
181
     *
182
     * @param  string $filename
183
     * @throws \phpbu\App\Exception
184
     * @return \stdClass
185
     */
186 39
    protected function loadFile($filename)
187
    {
188 39
        $reporting = error_reporting(0);
189 39
        $contents  = file_get_contents($filename);
190 39
        error_reporting($reporting);
191
192 39
        if ($contents === false) {
193 2
            throw new Exception(sprintf('Could not read "%s".', $filename));
194
        }
195 37
        return $contents;
196
    }
197
}
198