1 | <?php |
||
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 | 123 | public function __construct(File $file) |
|
48 | |||
49 | /** |
||
50 | * Get the contents of this document. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | 3 | public function getContent() |
|
58 | |||
59 | /** |
||
60 | * @return string |
||
61 | */ |
||
62 | 52 | public function getIndexName() |
|
66 | |||
67 | /** |
||
68 | * When a document is compiled, all of its internals are finished being configured. |
||
69 | */ |
||
70 | 10 | final public function compile() |
|
81 | |||
82 | /** |
||
83 | * Determine whether or not this document has been compiled. |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | final protected function isCompiled() |
||
91 | |||
92 | /** |
||
93 | * Get the original File object given to this document. |
||
94 | * |
||
95 | * @return File |
||
96 | */ |
||
97 | 2 | final public function getFile() |
|
101 | |||
102 | /** |
||
103 | * Get the relative path to the file, with respect to the site root. |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 103 | final public function getRelativeFilePath() |
|
111 | |||
112 | /** |
||
113 | * Get the extension of the file. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 71 | final public function getExtension() |
|
121 | |||
122 | /** |
||
123 | * Get the name of the file without the extension. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 4 | final public function getBasename() |
|
131 | |||
132 | /** |
||
133 | * Get the absolute path to the file. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 14 | final public function getAbsoluteFilePath() |
|
141 | |||
142 | /** |
||
143 | * Get the name of the file with its extension. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | final public function getFilename() |
||
151 | |||
152 | /** |
||
153 | * Read the contents of this file and handle all of the necessary processing/setup for this document. |
||
154 | */ |
||
155 | 123 | final public function readContent() |
|
161 | |||
162 | /** |
||
163 | * Prepare the Document so it can handle the data that's about to be read in. |
||
164 | * |
||
165 | * @return mixed Any information that will be passed to the readContents() method. |
||
166 | */ |
||
167 | 6 | protected function beforeReadContents() |
|
171 | |||
172 | /** |
||
173 | * Read the contents of the file and store the information internally **only**. |
||
174 | * |
||
175 | * This method must **not** handle any parsing or processing of the file's content. That should be handled by the |
||
176 | * `afterReadContents()` method. |
||
177 | * |
||
178 | * @param mixed $mixed Any information returned from the beforeReadContents() method. |
||
179 | * |
||
180 | * @throws \RuntimeException When the file cannot be read. |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | abstract protected function readContents($mixed); |
||
185 | |||
186 | /** |
||
187 | * After the Document's content has been read, process the it and handle any parsing that's needed. |
||
188 | * |
||
189 | * @param mixed $mixed Any information returned from the readContents() method. |
||
190 | */ |
||
191 | protected function afterReadContents($mixed) {} |
||
192 | |||
193 | /** |
||
194 | * Functionality that needs to take place before this document is considered "compiled," meaning everything has been |
||
195 | * processed, configured, and built. |
||
196 | */ |
||
197 | protected function beforeCompile() {} |
||
198 | } |
||
199 |