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 | 123 | 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 | public function setContent($content) |
||
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 52 | 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 | 103 | 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 | 95 | 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 | 91 | final public function getFilename() |
|
160 | |||
161 | /** |
||
162 | * Read the contents of this file and handle all of the necessary processing/setup for this document. |
||
163 | */ |
||
164 | 123 | final public function readContent() |
|
170 | |||
171 | /** |
||
172 | * Prepare the Document so it can handle the data that's about to be read in. |
||
173 | * |
||
174 | * @return mixed any information that will be passed to the readContents() method |
||
175 | */ |
||
176 | 6 | protected function beforeReadContents() |
|
180 | |||
181 | /** |
||
182 | * Read the contents of the file and store the information internally **only**. |
||
183 | * |
||
184 | * This method must **not** handle any parsing or processing of the file's content. That should be handled by the |
||
185 | * `afterReadContents()` method. |
||
186 | * |
||
187 | * @param mixed $mixed any information returned from the beforeReadContents() method |
||
188 | * |
||
189 | * @throws \RuntimeException when the file cannot be read |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | abstract protected function readContents($mixed); |
||
194 | |||
195 | /** |
||
196 | * After the Document's content has been read, process the it and handle any parsing that's needed. |
||
197 | * |
||
198 | * @param mixed $mixed any information returned from the readContents() method |
||
199 | */ |
||
200 | 6 | protected function afterReadContents($mixed) |
|
203 | |||
204 | /** |
||
205 | * Functionality that needs to take place before this document is considered "compiled," meaning everything has been |
||
206 | * processed, configured, and built. |
||
207 | */ |
||
208 | protected function beforeCompile() |
||
211 | } |
||
212 |