1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the php-utilities package. |
4
|
|
|
* |
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Asm\Config; |
11
|
|
|
|
12
|
|
|
use Asm\Exception\ConfigClassNotExistsException; |
13
|
|
|
use Asm\Exception\ConfigParameterMissingException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Config |
17
|
|
|
* |
18
|
|
|
* @package Asm\Config |
19
|
|
|
* @author Marc Aschmann <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class Config |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_CONFIG = 'ConfigDefault'; |
24
|
|
|
const DEFAULT_NAMESPACE = 'Asm'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get object of specific class. |
28
|
|
|
* |
29
|
|
|
* @param array $param config file name |
30
|
|
|
* @param string $class name of class without |
31
|
|
|
* @throws \ErrorException |
32
|
|
|
* @throws \InvalidArgumentException |
33
|
|
|
* @return ConfigInterface |
34
|
|
|
*/ |
35
|
|
|
public static function factory(array $param, string $class = self::DEFAULT_CONFIG) : ConfigInterface |
36
|
|
|
{ |
37
|
|
|
if (false === strpos($class, self::DEFAULT_NAMESPACE)) { |
38
|
|
|
$class = __NAMESPACE__ . '\\' . $class; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (class_exists($class)) { |
42
|
|
|
// allow config names without ending |
43
|
|
|
if (empty($param['file'])) { |
44
|
|
|
throw new ConfigParameterMissingException( |
45
|
|
|
'Config::factory() - config filename missing in param array!' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new $class($param); |
50
|
|
|
} else { |
51
|
|
|
throw new ConfigClassNotExistsException( |
52
|
|
|
"Config::factory() - could not instantiate {$class}, does not exist!" |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fordbid instantiation. |
59
|
|
|
* |
60
|
|
|
* @codeCoverageIgnore |
61
|
|
|
*/ |
62
|
|
|
private function __construct() |
63
|
|
|
{ |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Forbid cloning. |
68
|
|
|
* |
69
|
|
|
* @codeCoverageIgnore |
70
|
|
|
*/ |
71
|
|
|
private function __clone() |
72
|
|
|
{ |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|