Completed
Push — master ( 281eb8...04fe01 )
by Sebastian
10:51
created

File::setupAdapters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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