Skip to content

Commit f3be1b6

Browse files
Merge branch 'main' into 6.0
2 parents dbb9a02 + 42fd399 commit f3be1b6

File tree

5 files changed

+170
-30
lines changed

5 files changed

+170
-30
lines changed

src/Adapter/PHPConstant.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace phpbu\App\Adapter;
3+
4+
use phpbu\App\Adapter;
5+
use phpbu\App\Configuration;
6+
use phpbu\App\Exception;
7+
use phpbu\App\Util as AppUtil;
8+
9+
/**
10+
* PHPConstant Adapter
11+
*
12+
* @package phpbu
13+
* @subpackage App
14+
* @author Sebastian Feldmann <sebastian@phpbu.de>
15+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
16+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
17+
* @link http://phpbu.de/
18+
* @since Class available since Release 6.0.11
19+
*/
20+
class PHPConstant implements Adapter
21+
{
22+
/**
23+
* Path to the config file
24+
*
25+
* @var string
26+
*/
27+
private $file;
28+
29+
/**
30+
* Setup the adapter.
31+
*
32+
* @param array $conf
33+
* @return void
34+
* @throws Exception
35+
*/
36+
public function setup(array $conf)
37+
{
38+
$path = AppUtil\Arr::getValue($conf, 'file', 'config.php');
39+
$this->file = AppUtil\Path::toAbsolutePath($path, Configuration::getWorkingDirectory());
40+
$this->load();
41+
}
42+
43+
/**
44+
* Load config file to local file.
45+
*
46+
* @throws Exception
47+
*/
48+
private function load()
49+
{
50+
if (!file_exists($this->file)) {
51+
throw new Exception('config file not found');
52+
}
53+
require $this->file;
54+
}
55+
56+
/**
57+
* Return value a the constant with the given name.
58+
*
59+
* @param string $path
60+
* @return string
61+
* @throws Exception
62+
*/
63+
public function getValue(string $path) : string
64+
{
65+
if (!defined($path)) {
66+
throw new Exception('constant not defined');
67+
}
68+
return (string) constant($path);
69+
}
70+
}

src/Factory.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ class Factory
3131
// type
3232
// alias => fqcn
3333
'adapter' => [
34-
'array' => '\\phpbu\\App\\Adapter\\PHPArray',
35-
'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv',
36-
'env' => '\\phpbu\\App\\Adapter\\Env',
34+
'array' => '\\phpbu\\App\\Adapter\\PHPArray',
35+
'constants' => '\\phpbu\\App\\Adapter\\PHPConstant',
36+
'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv',
37+
'env' => '\\phpbu\\App\\Adapter\\Env',
3738
],
3839
'logger' => [
3940
'json' => '\\phpbu\\App\\Log\\Json',
@@ -94,8 +95,8 @@ class Factory
9495
*
9596
* @param string $type
9697
* @param string $alias
97-
* @throws \phpbu\App\Exception
9898
* @return mixed
99+
*@throws Exception
99100
*/
100101
protected function create($type, $alias)
101102
{
@@ -114,12 +115,12 @@ protected function create($type, $alias)
114115
*
115116
* @param string $alias
116117
* @param array $conf
117-
* @throws \phpbu\App\Exception
118-
* @return \phpbu\App\Adapter
118+
* @return Adapter
119+
*@throws Exception
119120
*/
120121
public function createAdapter($alias, $conf = []) : Adapter
121122
{
122-
/** @var \phpbu\App\Adapter $adapter */
123+
/** @var Adapter $adapter */
123124
$adapter = $this->create('adapter', $alias);
124125
if (!($adapter instanceof Adapter)) {
125126
throw new Exception(sprintf('adapter \'%s\' has to implement the \'Adapter\' interfaces', $alias));
@@ -133,12 +134,12 @@ public function createAdapter($alias, $conf = []) : Adapter
133134
*
134135
* @param string $alias
135136
* @param array $conf
136-
* @throws \phpbu\App\Exception
137-
* @return \phpbu\App\Log\Logger
137+
* @return Logger
138+
*@throws Exception
138139
*/
139140
public function createLogger($alias, $conf = []) : Logger
140141
{
141-
/** @var \phpbu\App\Log\Logger $logger */
142+
/** @var Logger $logger */
142143
$logger = $this->create('logger', $alias);
143144
if (!($logger instanceof Logger)) {
144145
throw new Exception(sprintf('logger \'%s\' has to implement the \'Logger\' interfaces', $alias));
@@ -153,9 +154,9 @@ public function createLogger($alias, $conf = []) : Logger
153154
/**
154155
* Create a backup target
155156
*
156-
* @param \phpbu\App\Configuration\Backup\Target $conf
157-
* @return \phpbu\App\Backup\Target
158-
* @throws \phpbu\App\Exception
157+
* @param Configuration\Backup\Target $conf
158+
* @return Target
159+
* @throws Exception
159160
*/
160161
public function createTarget(Configuration\Backup\Target $conf) : Target
161162
{
@@ -174,12 +175,12 @@ public function createTarget(Configuration\Backup\Target $conf) : Target
174175
*
175176
* @param string $alias
176177
* @param array $conf
177-
* @throws \phpbu\App\Exception
178-
* @return \phpbu\App\Backup\Source
178+
* @return Source
179+
*@throws Exception
179180
*/
180181
public function createSource($alias, $conf = []) : Source
181182
{
182-
/** @var \phpbu\App\Backup\Source $source */
183+
/** @var Source $source */
183184
$source = $this->create('source', $alias);
184185
if (!($source instanceof Source)) {
185186
throw new Exception(sprintf('source \'%s\' has to implement the \'Source\' interface', $alias));
@@ -192,12 +193,12 @@ public function createSource($alias, $conf = []) : Source
192193
* Check Factory
193194
*
194195
* @param string $alias
195-
* @throws \phpbu\App\Exception
196-
* @return \phpbu\App\Backup\Check
196+
* @return Check
197+
*@throws Exception
197198
*/
198199
public function createCheck($alias) : Check
199200
{
200-
/** @var \phpbu\App\Backup\Check $check */
201+
/** @var Check $check */
201202
$check = $this->create('check', $alias);
202203
if (!($check instanceof Check)) {
203204
throw new Exception(sprintf('Check \'%s\' has to implement the \'Check\' interface', $alias));
@@ -210,12 +211,12 @@ public function createCheck($alias) : Check
210211
*
211212
* @param string $alias
212213
* @param array $conf
213-
* @throws \phpbu\App\Exception
214-
* @return \phpbu\App\Backup\Crypter
214+
* @return Crypter
215+
*@throws Exception
215216
*/
216217
public function createCrypter($alias, $conf = []) : Crypter
217218
{
218-
/** @var \phpbu\App\Backup\Crypter $crypter */
219+
/** @var Crypter $crypter */
219220
$crypter = $this->create('crypter', $alias);
220221
if (!($crypter instanceof Crypter)) {
221222
throw new Exception(sprintf('Crypter \'%s\' has to implement the \'Crypter\' interface', $alias));
@@ -229,12 +230,12 @@ public function createCrypter($alias, $conf = []) : Crypter
229230
*
230231
* @param string $alias
231232
* @param array $conf
232-
* @throws \phpbu\App\Exception
233-
* @return \phpbu\App\Backup\Sync
233+
* @return Sync
234+
*@throws Exception
234235
*/
235236
public function createSync($alias, $conf = []) : Sync
236237
{
237-
/** @var \phpbu\App\Backup\Sync $sync */
238+
/** @var Sync $sync */
238239
$sync = $this->create('sync', $alias);
239240
if (!($sync instanceof Sync)) {
240241
throw new Exception(sprintf('sync \'%s\' has to implement the \'Sync\' interface', $alias));
@@ -248,12 +249,12 @@ public function createSync($alias, $conf = []) : Sync
248249
*
249250
* @param string $alias
250251
* @param array $conf
251-
* @throws \phpbu\App\Exception
252-
* @return \phpbu\App\Backup\Cleaner
252+
* @return Cleaner
253+
*@throws Exception
253254
*/
254255
public function createCleaner($alias, $conf = []) : Cleaner
255256
{
256-
/** @var \phpbu\App\Backup\Cleaner $cleaner */
257+
/** @var Cleaner $cleaner */
257258
$cleaner = $this->create('cleaner', $alias);
258259
if (!($cleaner instanceof Cleaner)) {
259260
throw new Exception(sprintf('cleaner \'%s\' has to implement the \'Cleaner\' interface', $alias));
@@ -269,7 +270,7 @@ public function createCleaner($alias, $conf = []) : Cleaner
269270
* @param string $alias Name the class is registered at
270271
* @param string $fqcn Full Qualified Class Name
271272
* @param boolean $force Overwrite already registered class
272-
* @throws \phpbu\App\Exception
273+
* @throws Exception
273274
*/
274275
public static function register($type, $alias, $fqcn, $force = false)
275276
{
@@ -286,7 +287,7 @@ public static function register($type, $alias, $fqcn, $force = false)
286287
* Throws an exception if type is invalid
287288
*
288289
* @param string $type
289-
* @throws \phpbu\App\Exception
290+
* @throws Exception
290291
*/
291292
private static function checkType($type)
292293
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
define('PHPBU_TEST_ADAPTER', 'fiz');

tests/_files/misc/empty.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace phpbu\App\Adapter;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* Env test
8+
*
9+
* @package phpbu
10+
* @subpackage tests
11+
* @author Sebastian Feldmann <sebastian@phpbu.de>
12+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
13+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
14+
* @link http://www.phpbu.de/
15+
* @since Class available since Release 6.0.11
16+
*/
17+
class PHPConstantTest extends TestCase
18+
{
19+
/**
20+
* Tests PHPConstant::setup
21+
*/
22+
public function testSetup()
23+
{
24+
$arr = new PHPConstant();
25+
$arr->setup(['file' => PHPBU_TEST_FILES . '/misc/empty.php']);
26+
27+
$this->assertTrue(true);
28+
}
29+
30+
/**
31+
* Tests PHPConstant::setup
32+
*/
33+
public function testSetupFail()
34+
{
35+
$this->expectException('phpbu\App\Exception');
36+
$arr = new PHPConstant();
37+
$arr->setup(['file' => 'constant.config.php']);
38+
}
39+
40+
/**
41+
* Tests PHPConstant::getValue
42+
*/
43+
public function testGetValue()
44+
{
45+
$arr = new PHPConstant();
46+
$arr->setup(['file' => PHPBU_TEST_FILES . '/misc/constant.config.php']);
47+
48+
$fiz = $arr->getValue('PHPBU_TEST_ADAPTER');
49+
50+
$this->assertEquals('fiz', $fiz);
51+
}
52+
53+
54+
/**
55+
* Tests PHPConstant::getValue
56+
*/
57+
public function testGetValueFail()
58+
{
59+
$this->expectException('phpbu\App\Exception');
60+
$arr = new PHPConstant();
61+
$arr->setup(['file' => PHPBU_TEST_FILES . '/misc/empty.php']);
62+
63+
$arr->getValue('FOO_BAR_BAZ');
64+
}
65+
}

0 commit comments

Comments
 (0)