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 original File object given to this document. |
||
103 | * |
||
104 | * @return File |
||
105 | */ |
||
106 | 2 | final public function getFile() |
|
110 | |||
111 | /** |
||
112 | * Get the relative path to the file, with respect to the site root. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 104 | final public function getRelativeFilePath() |
|
120 | |||
121 | /** |
||
122 | * Get the extension of the file. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 71 | final public function getExtension() |
|
130 | |||
131 | /** |
||
132 | * Get the name of the file without the extension. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 96 | final public function getBasename() |
|
140 | |||
141 | /** |
||
142 | * Get the absolute path to the file. |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | 14 | final public function getAbsoluteFilePath() |
|
150 | |||
151 | /** |
||
152 | * Get the name of the file with its extension. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 92 | final public function getFilename() |
|
160 | |||
161 | /** |
||
162 | * Gets the last modified time. |
||
163 | * |
||
164 | * @return int The last modified time for the file, in a Unix timestamp |
||
165 | */ |
||
166 | final public function getLastModified() |
||
170 | |||
171 | /** |
||
172 | * Read the contents of this file and handle all of the necessary processing/setup for this document. |
||
173 | */ |
||
174 | 124 | final public function readContent() |
|
180 | |||
181 | /** |
||
182 | * Prepare the Document so it can handle the data that's about to be read in. |
||
183 | * |
||
184 | * @return mixed any information that will be passed to the readContents() method |
||
185 | */ |
||
186 | 6 | protected function beforeReadContents() |
|
190 | |||
191 | /** |
||
192 | * Read the contents of the file and store the information internally **only**. |
||
193 | * |
||
194 | * This method must **not** handle any parsing or processing of the file's content. That should be handled by the |
||
195 | * `afterReadContents()` method. |
||
196 | * |
||
197 | * @param mixed $mixed any information returned from the beforeReadContents() method |
||
198 | * |
||
199 | * @throws \RuntimeException when the file cannot be read |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | abstract protected function readContents($mixed); |
||
204 | |||
205 | /** |
||
206 | * After the Document's content has been read, process the it and handle any parsing that's needed. |
||
207 | * |
||
208 | * @param mixed $mixed any information returned from the readContents() method |
||
209 | */ |
||
210 | 6 | protected function afterReadContents($mixed) |
|
213 | |||
214 | /** |
||
215 | * Functionality that needs to take place before this document is considered "compiled," meaning everything has been |
||
216 | * processed, configured, and built. |
||
217 | */ |
||
218 | protected function beforeCompile() |
||
221 | } |
||
222 |