Completed
Push — master ( 32da9f...5da014 )
by Sebastian
07:46
created

File::handleBootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
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 37
     */
43
    protected $bootstrapper;
44 37
45 37
    /**
46
     * File constructor.
47
     *
48
     * @param string                                $file
49
     * @param \phpbu\App\Configuration\Bootstrapper $bootstrapper
50
     */
51
    public function __construct(string $file, Configuration\Bootstrapper $bootstrapper)
52
    {
53 32
        $this->filename     = $file;
54
        $this->bootstrapper = $bootstrapper;
55
    }
56 32
57 32
    /**
58
     * Returns the phpbu Configuration.
59 32
     *
60
     * @param  \phpbu\App\Factory $factory
61 28
     * @return \phpbu\App\Configuration
62 28
     * @throws \phpbu\App\Exception
63 26
     */
64
    public function getConfiguration(AppFactory $factory)
65 12
    {
66
        // create configuration first so the working directory is available for all adapters
67
        $configuration = new Configuration();
68
        $configuration->setFilename($this->filename);
69
70
        $this->setAppSettings($configuration);
71
        $this->handleBootstrap($configuration);
72
        $this->setupAdapters($factory);
73 32
74
        $this->setLoggers($configuration);
75 32
        $this->setBackups($configuration);
76 3
77
        return $configuration;
78 28
    }
79
80
    /**
81
     * Load all available config adapters.
82
     *
83
     * @param  \phpbu\App\Factory $factory
84
     * @throws \phpbu\App\Exception
85
     */
86
    protected function setupAdapters(AppFactory $factory)
87 3
    {
88
        foreach ($this->getAdapterConfigs() as $config) {
89 3
            $this->adapters[$config->name] = $factory->createAdapter($config->type, $config->options);
90 1
        }
91
    }
92 2
93
    /**
94
     * Return a registered adapter.
95
     *
96
     * @param  string $name
97
     * @return \phpbu\App\Adapter
98
     * @throws \phpbu\App\Exception
99
     */
100
    protected function getAdapter($name)
101
    {
102
        if (!isset($this->adapters[$name])) {
103
            throw new Exception('no adapter registered with name: ' . $name);
104
        }
105
        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 27
     *
133
     * @param  \phpbu\App\Configuration $configuration
134 27
     * @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 28
    protected function handleBootstrap(Configuration $configuration)
145
    {
146 28
        $this->bootstrapper->run($configuration);
147 3
    }
148 3
149 2
    /**
150
     * Converts a path to an absolute one if necessary.
151 28
     *
152
     * @param  string  $path
153
     * @param  boolean $useIncludePath
154
     * @return string
155
     */
156
    protected function toAbsolutePath($path, $useIncludePath = false)
157
    {
158
        return Cli::toAbsolutePath($path, dirname($this->filename), $useIncludePath);
159
    }
160
161 37
    /**
162
     * Return option value.
163 37
     * Checks if the value should be fetched from an Adapter, if not it just returns the value.
164 37
     *
165 37
     * @param  string $value
166
     * @return string
167 37
     * @throws \phpbu\App\Exception
168 2
     */
169
    protected function getAdapterizedValue($value)
170 35
    {
171
        foreach (Util::getAdapterReplacements($value) as $replacement) {
172
            $search  = $replacement['search'];
173
            $replace = $this->getAdapter($replacement['adapter'])->getValue($replacement['path']);
174
            $value   = str_replace($search, $replace, $value);
175
        }
176
        return $value;
177
    }
178
179
    /**
180
     * Load the file.
181
     *
182
     * @param  string $filename
183
     * @throws \phpbu\App\Exception
184
     * @return \stdClass
185
     */
186
    protected function loadFile($filename)
187
    {
188
        $reporting = error_reporting(0);
189
        $contents  = file_get_contents($filename);
190
        error_reporting($reporting);
191
192
        if ($contents === false) {
193
            throw new Exception(sprintf('Could not read "%s".', $filename));
194
        }
195
        return $contents;
196
    }
197
}
198