|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Config\Loaders; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Config\Exception\RuntimeException; |
|
6
|
|
|
use Nip\Config\FileParser\AbstractFileParser; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class FileLoader |
|
10
|
|
|
* @package Nip\Config\Loaders |
|
11
|
|
|
*/ |
|
12
|
|
|
class FileLoader extends AbstractLoader |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Registered config file extensions. |
|
17
|
|
|
* key is extension, value is reader instance or plugin name |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $extensions = [ |
|
22
|
|
|
'ini' => 'Ini', |
|
23
|
|
|
'php' => 'Php', |
|
24
|
|
|
'json' => 'Json', |
|
25
|
|
|
'xml' => 'Xml', |
|
26
|
|
|
'yaml' => 'Yaml', |
|
27
|
|
|
]; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $extension = null; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var AbstractFileParser[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $fileParsers = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
1 |
|
protected function getData() |
|
41
|
|
|
{ |
|
42
|
1 |
|
$type = self::$extensions[$this->getExtension()]; |
|
43
|
|
|
|
|
44
|
1 |
|
return $this->getFileParser($type)->setPath($this->getResolvedPath())->parse(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function getExtension() |
|
51
|
|
|
{ |
|
52
|
1 |
|
if ($this->extension === null) { |
|
53
|
1 |
|
$this->initExtension(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $this->extension; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $extension |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function setExtension($extension) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$this->extension = $extension; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function initExtension() |
|
68
|
|
|
{ |
|
69
|
1 |
|
$filename = $this->getResolvedPath(); |
|
70
|
1 |
|
$pathInfo = pathinfo($filename); |
|
71
|
1 |
|
if (!isset($pathInfo['extension'])) { |
|
72
|
|
|
throw new RuntimeException(sprintf( |
|
73
|
|
|
'Filename "%s" is missing an extension and cannot be auto-detected', |
|
74
|
|
|
$filename |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$this->setExtension(strtolower($pathInfo['extension'])); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $type |
|
83
|
|
|
* @return AbstractFileParser |
|
84
|
|
|
*/ |
|
85
|
1 |
|
protected function getFileParser($type) |
|
86
|
|
|
{ |
|
87
|
1 |
|
if (!isset($this->fileParsers[$type])) { |
|
88
|
1 |
|
$this->initFileParser($type); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
return $this->fileParsers[$type]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $type |
|
96
|
|
|
*/ |
|
97
|
1 |
|
protected function initFileParser($type) |
|
98
|
|
|
{ |
|
99
|
1 |
|
$class = 'Nip\Config\FileParser\\' . $type; |
|
100
|
1 |
|
$parser = new $class(); |
|
101
|
1 |
|
$this->fileParsers[$type] = $parser; |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @throws RuntimeException |
|
106
|
|
|
*/ |
|
107
|
1 |
|
protected function resolvePath() |
|
108
|
|
|
{ |
|
109
|
1 |
|
$filename = $this->getPath(); |
|
110
|
1 |
|
if (file_exists($filename)) { |
|
111
|
1 |
|
return $filename; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (!$this->useIncludePath()) { |
|
115
|
|
|
throw new RuntimeException(sprintf( |
|
116
|
|
|
'Filename "%s" cannot be found relative to the working directory', |
|
117
|
|
|
$filename |
|
118
|
|
|
)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$fromIncludePath = stream_resolve_include_path($filename); |
|
122
|
|
|
if (!$fromIncludePath) { |
|
123
|
|
|
throw new RuntimeException(sprintf( |
|
124
|
|
|
'Filename "%s" cannot be found relative to the working directory or the include_path ("%s")', |
|
125
|
|
|
$filename, |
|
126
|
|
|
get_include_path() |
|
127
|
|
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $fromIncludePath; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.