1 | <?php |
||
14 | abstract class ReadableDocument |
||
15 | { |
||
16 | /** |
||
17 | * Do not read the file immediately at construction. The object will need to execute the self::refreshFileContent() |
||
18 | * manually at some point. |
||
19 | * |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $noReadOnConstructor = false; |
||
23 | |||
24 | /** @var string The content of the document's body. */ |
||
25 | protected $bodyContent = ''; |
||
26 | |||
27 | protected $metadata; |
||
28 | protected $file; |
||
29 | |||
30 | private $compiled = false; |
||
31 | |||
32 | /** |
||
33 | * ReadableDocument Constructor. |
||
34 | * |
||
35 | * @throws FileNotFoundException when the file given in the constructor does not exist or may not be accessible |
||
36 | */ |
||
37 | 124 | public function __construct(File $file) |
|
47 | |||
48 | /** |
||
49 | * Get the contents of this document. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | 3 | public function getContent() |
|
57 | |||
58 | /** |
||
59 | * Manually set the contents of this document. |
||
60 | * |
||
61 | * @param string $content |
||
62 | */ |
||
63 | 1 | public function setContent($content) |
|
64 | { |
||
65 | 1 | $this->bodyContent = $content; |
|
66 | 1 | } |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 53 | public function getIndexName() |
|
75 | |||
76 | /** |
||
77 | * When a document is compiled, all of its internals are finished being configured. |
||
78 | */ |
||
79 | 10 | final public function compile() |
|
80 | { |
||
81 | 10 | if ($this->compiled) |
|
82 | { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | 10 | $this->beforeCompile(); |
|
87 | |||
88 | 10 | $this->compiled = true; |
|
89 | 10 | } |
|
90 | |||
91 | /** |
||
92 | * Determine whether or not this document has been compiled. |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | final protected function isCompiled() |
||
100 | |||
101 | /** |
||
102 | * Get the value stored under this key in the file's internal metadata only available to stakx. |
||
103 | * |
||
104 | * @param string $key |
||
105 | * |
||
106 | * @return mixed|null |
||
107 | */ |
||
108 | final public function getMetadata($key) |
||
112 | |||
113 | /** |
||
114 | * Set a value in the file's internal metadata only available to stakx. |
||
115 | * |
||
116 | * @param string $key |
||
117 | * @param mixed $value |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | final public function setMetadata($key, $value) |
||
125 | |||
126 | /** |
||
127 | * Get the original File object given to this document. |
||
128 | * |
||
129 | * @return File |
||
130 | */ |
||
131 | 2 | final public function getFile() |
|
135 | |||
136 | /** |
||
137 | * Get the relative path to the file, with respect to the site root. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 104 | final public function getRelativeFilePath() |
|
145 | |||
146 | /** |
||
147 | * Get the extension of the file. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 71 | final public function getExtension() |
|
155 | |||
156 | /** |
||
157 | * Get the name of the file without the extension. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 96 | final public function getBasename() |
|
165 | |||
166 | /** |
||
167 | * Get the absolute path to the file. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 14 | final public function getAbsoluteFilePath() |
|
175 | |||
176 | /** |
||
177 | * Get the name of the file with its extension. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 92 | final public function getFilename() |
|
185 | |||
186 | /** |
||
187 | * Gets the last modified time. |
||
188 | * |
||
189 | * @return int The last modified time for the file, in a Unix timestamp |
||
190 | */ |
||
191 | final public function getLastModified() |
||
195 | |||
196 | /** |
||
197 | * Read the contents of this file and handle all of the necessary processing/setup for this document. |
||
198 | */ |
||
199 | 124 | final public function readContent() |
|
205 | |||
206 | /** |
||
207 | * Prepare the Document so it can handle the data that's about to be read in. |
||
208 | * |
||
209 | * @return mixed any information that will be passed to the readContents() method |
||
210 | */ |
||
211 | 6 | protected function beforeReadContents() |
|
215 | |||
216 | /** |
||
217 | * Read the contents of the file and store the information internally **only**. |
||
218 | * |
||
219 | * This method must **not** handle any parsing or processing of the file's content. That should be handled by the |
||
220 | * `afterReadContents()` method. |
||
221 | * |
||
222 | * @param mixed $mixed any information returned from the beforeReadContents() method |
||
223 | * |
||
224 | * @throws \RuntimeException when the file cannot be read |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | abstract protected function readContents($mixed); |
||
229 | |||
230 | /** |
||
231 | * After the Document's content has been read, process the it and handle any parsing that's needed. |
||
232 | * |
||
233 | * @param mixed $mixed any information returned from the readContents() method |
||
234 | */ |
||
235 | 6 | protected function afterReadContents($mixed) |
|
238 | |||
239 | /** |
||
240 | * Functionality that needs to take place before this document is considered "compiled," meaning everything has been |
||
241 | * processed, configured, and built. |
||
242 | */ |
||
243 | protected function beforeCompile() |
||
246 | } |
||
247 |