|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mediadevs\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use Mediadevs\Configuration\Traits\ReturnTypeTrait; |
|
6
|
|
|
use Mediadevs\Configuration\Traits\FileCheckerTrait; |
|
7
|
|
|
use Mediadevs\Configuration\Traits\DirectoryCheckerTrait; |
|
8
|
|
|
use Mediadevs\Configuration\Exceptions\InvalidReturnTypeException; |
|
9
|
|
|
|
|
10
|
|
|
class Configuration |
|
11
|
|
|
{ |
|
12
|
|
|
use ReturnTypeTrait; |
|
13
|
|
|
use FileCheckerTrait; |
|
14
|
|
|
use DirectoryCheckerTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Return types for collecting the configuration data. |
|
18
|
|
|
*/ |
|
19
|
|
|
public const RETURN_TYPE_JSON = 'TYPE_JSON'; |
|
20
|
|
|
public const RETURN_TYPE_ARRAY = 'TYPE_ARRAY'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Path to the configuration directory. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $path; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The file name of the desired configuration file. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $file; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The path to the config directory. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* |
|
41
|
|
|
* @return Configuration |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function directory(string $path): self |
|
44
|
|
|
{ |
|
45
|
2 |
|
$this->path = $path; |
|
46
|
|
|
|
|
47
|
2 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The name of the configuration file. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $file |
|
54
|
|
|
* |
|
55
|
|
|
* @return Configuration |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function config(string $file): self |
|
58
|
|
|
{ |
|
59
|
2 |
|
$this->file = $file; |
|
60
|
|
|
|
|
61
|
2 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returning the contents of the configuration file. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $returnType |
|
68
|
|
|
* |
|
69
|
|
|
* @throws InvalidReturnTypeException |
|
70
|
|
|
* @throws Exceptions\ConfigurationFileException |
|
71
|
|
|
* @throws Exceptions\ConfigurationDirectoryException |
|
72
|
|
|
* |
|
73
|
|
|
* @return array|string|void |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function get(string $returnType = self::RETURN_TYPE_JSON) |
|
76
|
|
|
{ |
|
77
|
2 |
|
$configuration = $this->path . DIRECTORY_SEPARATOR . $this->file . '.php'; |
|
78
|
|
|
|
|
79
|
|
|
// Validating whether the configuration file and directory exists |
|
80
|
2 |
|
if ($this->configurationDirectoryExists($this->path) && $this->configurationFileExists($configuration)) { |
|
81
|
2 |
|
$collection = include $configuration; |
|
82
|
|
|
|
|
83
|
|
|
// Returning the configuration in the desired format |
|
84
|
|
|
switch ($returnType) { |
|
85
|
2 |
|
case self::RETURN_TYPE_JSON: |
|
86
|
1 |
|
return $this->returnJson($collection); |
|
87
|
|
|
|
|
88
|
1 |
|
case self::RETURN_TYPE_ARRAY: |
|
89
|
1 |
|
return $this->returnArray($collection); |
|
90
|
|
|
|
|
91
|
|
|
default: |
|
92
|
|
|
throw new InvalidReturnTypeException(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|