1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Document; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Filesystem\File; |
11
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
12
|
|
|
use allejo\stakx\Utilities\NullableArray; |
13
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
14
|
|
|
|
15
|
|
|
abstract class ReadableDocument |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Do not read the file immediately at construction. The object will need to execute the self::refreshFileContent() |
19
|
|
|
* manually at some point. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $noReadOnConstructor = false; |
24
|
|
|
|
25
|
|
|
/** @var string The content of the document's body. */ |
26
|
|
|
protected $bodyContent = ''; |
27
|
|
|
|
28
|
|
|
protected $metadata; |
29
|
|
|
protected $file; |
30
|
|
|
|
31
|
|
|
private $compiled = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ReadableDocument Constructor. |
35
|
|
|
* |
36
|
|
|
* @throws FileNotFoundException When the file given in the constructor does not exist or may not be accessible. |
37
|
|
|
*/ |
38
|
127 |
|
public function __construct(File $file) |
39
|
|
|
{ |
40
|
127 |
|
$filePath = (string)$file; |
41
|
|
|
|
42
|
127 |
|
if (!fs::exists($filePath)) |
43
|
|
|
{ |
44
|
2 |
|
throw new FileNotFoundException(null, 0, null, $filePath); |
45
|
|
|
} |
46
|
|
|
|
47
|
125 |
|
$this->metadata = new NullableArray(); |
48
|
125 |
|
$this->file = $file; |
49
|
|
|
|
50
|
125 |
|
if (!$this->noReadOnConstructor) |
51
|
|
|
{ |
52
|
119 |
|
$this->readContent(); |
53
|
|
|
} |
54
|
114 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the contents of this document. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
4 |
|
public function getContent() |
62
|
|
|
{ |
63
|
4 |
|
return $this->bodyContent; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
53 |
|
public function getIndexName() |
70
|
|
|
{ |
71
|
53 |
|
return $this->getRelativeFilePath(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* When a document is compiled, all of its internals are finished being configured. |
76
|
|
|
*/ |
77
|
10 |
|
final public function compile() |
78
|
|
|
{ |
79
|
10 |
|
if ($this->compiled) |
80
|
|
|
{ |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
10 |
|
$this->beforeCompile(); |
85
|
|
|
|
86
|
10 |
|
$this->compiled = true; |
87
|
10 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Determine whether or not this document has been compiled. |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
final protected function isCompiled() |
95
|
|
|
{ |
96
|
|
|
return $this->compiled; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the relative path to the file, with respect to the site root. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
105 |
|
final public function getRelativeFilePath() |
105
|
|
|
{ |
106
|
105 |
|
return $this->file->getRelativeFilePath(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the extension of the file. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
14 |
|
final public function getExtension() |
115
|
|
|
{ |
116
|
14 |
|
return $this->file->getExtension(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the name of the file without the extension. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
4 |
|
final public function getBasename() |
125
|
|
|
{ |
126
|
4 |
|
return $this->file->getBasename(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the absolute path to the file. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
16 |
|
final public function getAbsoluteFilePath() |
135
|
|
|
{ |
136
|
16 |
|
return $this->file->getAbsolutePath(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the name of the file with its extension. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
final public function getFilename() |
145
|
|
|
{ |
146
|
|
|
return $this->file->getFilename(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Read the contents of this file and handle all of the necessary processing/setup for this document. |
151
|
|
|
*/ |
152
|
125 |
|
final public function readContent() |
153
|
|
|
{ |
154
|
125 |
|
$beforeEvent = $this->beforeReadContents(); |
155
|
125 |
|
$actualEvent = $this->readContents($beforeEvent); |
156
|
115 |
|
$this->afterReadContents($actualEvent); |
157
|
114 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Prepare the Document so it can handle the data that's about to be read in. |
161
|
|
|
* |
162
|
|
|
* @return mixed Any information that will be passed to the readContents() method. |
163
|
|
|
*/ |
164
|
6 |
|
protected function beforeReadContents() |
165
|
|
|
{ |
166
|
6 |
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Read the contents of the file and store the information internally **only**. |
171
|
|
|
* |
172
|
|
|
* @param mixed $mixed Any information returned from the beforeReadContents() method. |
173
|
|
|
* |
174
|
|
|
* @throws \RuntimeException When the file cannot be read. |
175
|
|
|
* |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
abstract protected function readContents($mixed); |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* After the Document's content has been read, process the it and handle any parsing that's needed. |
182
|
|
|
* |
183
|
|
|
* @param mixed $mixed Any information returned from the readContents() method. |
184
|
|
|
*/ |
185
|
|
|
protected function afterReadContents($mixed) {} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Functionality that needs to take place before this document is considered "compiled," meaning everything has been |
189
|
|
|
* processed, configured, and built. |
190
|
|
|
*/ |
191
|
|
|
protected function beforeCompile() {} |
192
|
|
|
} |
193
|
|
|
|