Completed
Push — master ( 0c38e0...116503 )
by Sebastian
03:25
created

File::setPhpSettings()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
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
     * File constructor.
39 13
     *
40 13
     * @param string $file
41 13
     */
42 12
    public function __construct($file)
43
    {
44 6
        $this->filename = $file;
45
    }
46
47
    /**
48
     * Returns the phpbu Configuration.
49
     *
50
     * @param  \phpbu\App\Factory $factory
51
     * @return \phpbu\App\Configuration
52
     */
53
    public function getConfiguration(AppFactory $factory)
54
    {
55
        // create configuration first so the working directory is available for all adapters
56
        $configuration = new Configuration();
57
        $configuration->setFilename($this->filename);
58
59
        $this->setupAdapters($factory);
60
61
        $this->setAppSettings($configuration);
62
        $this->setPhpSettings($configuration);
63
        $this->setLoggers($configuration);
64
        $this->setBackups($configuration);
65
66
        return $configuration;
67
    }
68
69
    /**
70
     * Load all available config adapters.
71
     *
72
     * @param \phpbu\App\Factory $factory
73
     */
74
    protected function setupAdapters(AppFactory $factory)
75
    {
76
        foreach ($this->getAdapterConfigs() as $config) {
77
            $this->adapters[$config->name] = $factory->createAdapter($config->type, $config->options);
78
        }
79
    }
80
81
    /**
82
     * Return a registered adapter.
83
     *
84
     * @param  string $name
85 13
     * @return \phpbu\App\Adapter
86
     * @throws \phpbu\App\Exception
87 13
     */
88
    protected function getAdapter($name)
89
    {
90
        if (!isset($this->adapters[$name])) {
91
            throw new Exception('no adapter registered with name: ' . $name);
92
        }
93
        return $this->adapters[$name];
94
    }
95
96
    /**
97 15
     * Return list of adapter configs.
98
     *
99 15
     * @return array
100 15
     */
101 15
    abstract protected function getAdapterConfigs();
102
103 15
    /**
104 1
     * Set the phpbu application settings.
105
     *
106 14
     * @param  \phpbu\App\Configuration $configuration
107
     */
108
    abstract public function setAppSettings(Configuration $configuration);
109
110
    /**
111
     * Set the php settings.
112
     * Checking for include_path and ini settings.
113
     *
114
     * @param  \phpbu\App\Configuration $configuration
115
     */
116
    abstract public function setPhpSettings(Configuration $configuration);
117
118
    /**
119
     * Set the log configuration.
120
     *
121
     * @param  \phpbu\App\Configuration $configuration
122
     * @throws \phpbu\App\Exception
123
     */
124
    abstract public function setLoggers(Configuration $configuration);
125
126
    /**
127
     * Set the backup configurations.
128
     *
129
     * @param  \phpbu\App\Configuration $configuration
130
     * @throws \phpbu\App\Exception
131
     */
132
    abstract public function setBackups(Configuration $configuration);
133
134
    /**
135
     * Converts a path to an absolute one if necessary.
136
     *
137
     * @param  string  $path
138
     * @param  boolean $useIncludePath
139
     * @return string
140
     */
141
    protected function toAbsolutePath($path, $useIncludePath = false)
142
    {
143
        return Cli::toAbsolutePath($path, dirname($this->filename), $useIncludePath);
144
    }
145
146
    /**
147
     * Return option value.
148
     * Checks if the value should be fetched from an Adapter, if not it just returns the value.
149
     *
150
     * @param  string $value
151
     * @return string
152
     */
153
    protected function getOptionValue($value)
154
    {
155
        $match = [];
156
        if (preg_match('#^adapter:([a-z0-9_\-]+):(.+)#i', $value, $match)) {
157
            $adapter = $match[1];
158
            $path    = $match[2];
159
            $value   = $this->getAdapter($adapter)->getValue($path);
160
        }
161
        return $value;
162
    }
163
164
    /**
165
     * Load the file.
166
     *
167
     * @param  string $filename
168
     * @throws \phpbu\App\Exception
169
     * @return \stdClass
170
     */
171
    protected function loadFile($filename)
172
    {
173
        $reporting = error_reporting(0);
174
        $contents  = file_get_contents($filename);
175
        error_reporting($reporting);
176
177
        if ($contents === false) {
178
            throw new Exception(sprintf('Could not read "%s".', $filename));
179
        }
180
        return $contents;
181
    }
182
}
183