1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Containers\Modules\DataStructures\Module\Theme\Layout; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Psr\Patterns\Structural\Repository\AbstractRepository; |
19
|
|
|
use O2System\Spl\Info\SplFileInfo; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Partials |
23
|
|
|
* |
24
|
|
|
* @package O2System\Framework\Containers\Modules\DataStructures\Module\Theme\Layout |
25
|
|
|
*/ |
26
|
|
|
class Partials extends AbstractRepository |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Partials::$path |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $path; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Partials::$extension |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $extension; |
41
|
|
|
|
42
|
|
|
// ------------------------------------------------------------------------ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Partials::setPath |
46
|
|
|
* |
47
|
|
|
* @param string $path |
48
|
|
|
* |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
public function setPath($path) |
52
|
|
|
{ |
53
|
|
|
if (is_dir($path)) { |
54
|
|
|
$this->path = $path; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// ------------------------------------------------------------------------ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Partials::setExtension |
64
|
|
|
* |
65
|
|
|
* @param string $extension |
66
|
|
|
* |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
|
|
public function setExtension($extension) |
70
|
|
|
{ |
71
|
|
|
$this->extension = '.' . rtrim($extension, '.'); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// ------------------------------------------------------------------------ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Partials::autoload |
80
|
|
|
*/ |
81
|
|
|
public function autoload() |
82
|
|
|
{ |
83
|
|
|
if (is_dir($this->path)) { |
84
|
|
|
$this->loadDir($this->path); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// ------------------------------------------------------------------------ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Partials::loadDir |
92
|
|
|
* |
93
|
|
|
* @param string $dir |
94
|
|
|
*/ |
95
|
|
|
public function loadDir($dir) |
96
|
|
|
{ |
97
|
|
|
$partialsFiles = scandir($dir); |
98
|
|
|
$partialsFiles = array_slice($partialsFiles, 2); |
99
|
|
|
|
100
|
|
|
foreach ($partialsFiles as $partialsFile) { |
101
|
|
|
|
102
|
|
|
$partialsFilePath = $dir . $partialsFile; |
103
|
|
|
|
104
|
|
|
if (is_file($partialsFilePath)) { |
105
|
|
|
$this->loadFile($partialsFilePath); |
106
|
|
|
} elseif (is_dir($partialsFilePath)) { |
107
|
|
|
$this->loadDir($partialsFilePath . DIRECTORY_SEPARATOR); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// ------------------------------------------------------------------------ |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Partials::loadFile |
116
|
|
|
* |
117
|
|
|
* @param string $filePath |
118
|
|
|
*/ |
119
|
|
|
public function loadFile($filePath) |
120
|
|
|
{ |
121
|
|
|
if (strrpos($filePath, $this->extension) !== false) { |
122
|
|
|
$fileKey = str_replace([$this->path, $this->extension, DIRECTORY_SEPARATOR], ['', '', '-'], $filePath); |
123
|
|
|
$this->store(camelcase($fileKey), new SplFileInfo($filePath)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// ------------------------------------------------------------------------ |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Partials::hasPartial |
131
|
|
|
* |
132
|
|
|
* @param string $partialOffset |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function hasPartial($partialOffset) |
137
|
|
|
{ |
138
|
|
|
return $this->__isset($partialOffset); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// ------------------------------------------------------------------------ |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Partials::get |
145
|
|
|
* |
146
|
|
|
* @param string $partial |
147
|
|
|
* |
148
|
|
|
* @return false|mixed|string|null |
149
|
|
|
*/ |
150
|
|
|
public function get($partial) |
151
|
|
|
{ |
152
|
|
|
$partialContent = parent::get($partial); |
153
|
|
|
|
154
|
|
|
if (is_file($partialContent)) { |
155
|
|
|
parser()->loadFile($partialContent); |
156
|
|
|
|
157
|
|
|
return parser()->parse(); |
158
|
|
|
} elseif (is_string($partialContent)) { |
159
|
|
|
return $partialContent; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
} |