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 | 126 | public function __construct(File $file) |
|
39 | { |
||
40 | 126 | $filePath = (string)$file; |
|
41 | |||
42 | 126 | if (!fs::exists($filePath)) |
|
43 | 126 | { |
|
44 | 2 | throw new FileNotFoundException(null, 0, null, $filePath); |
|
45 | } |
||
46 | |||
47 | 124 | $this->metadata = new NullableArray(); |
|
48 | 124 | $this->file = $file; |
|
49 | |||
50 | 124 | if (!$this->noReadOnConstructor) |
|
51 | 124 | { |
|
52 | 118 | $this->readContent(); |
|
53 | 107 | } |
|
54 | 113 | } |
|
55 | |||
56 | /** |
||
57 | * Get the contents of this document. |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 3 | public function getContent() |
|
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | 52 | public function getIndexName() |
|
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 | 10 | { |
|
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 | 118 | final protected function isCompiled() |
|
95 | 118 | { |
|
96 | return $this->compiled; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get the original File object given to this document. |
||
101 | * |
||
102 | * @return File |
||
103 | */ |
||
104 | 2 | final public function getFile() |
|
108 | |||
109 | /** |
||
110 | * Get the relative path to the file, with respect to the site root. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 104 | final public function getRelativeFilePath() |
|
118 | |||
119 | /** |
||
120 | * Get the extension of the file. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 15 | final public function getExtension() |
|
128 | |||
129 | /** |
||
130 | * Get the name of the file without the extension. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 4 | final public function getBasename() |
|
138 | |||
139 | /** |
||
140 | * Get the absolute path to the file. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 14 | final public function getAbsoluteFilePath() |
|
148 | |||
149 | /** |
||
150 | * Get the name of the file with its extension. |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | final public function getFilename() |
||
158 | |||
159 | /** |
||
160 | * Read the contents of this file and handle all of the necessary processing/setup for this document. |
||
161 | */ |
||
162 | 124 | final public function readContent() |
|
168 | |||
169 | /** |
||
170 | * Prepare the Document so it can handle the data that's about to be read in. |
||
171 | * |
||
172 | * @return mixed Any information that will be passed to the readContents() method. |
||
173 | */ |
||
174 | 6 | protected function beforeReadContents() |
|
178 | |||
179 | /** |
||
180 | * Read the contents of the file and store the information internally **only**. |
||
181 | * |
||
182 | * @param mixed $mixed Any information returned from the beforeReadContents() method. |
||
183 | * |
||
184 | * @throws \RuntimeException When the file cannot be read. |
||
185 | * |
||
186 | * @return mixed |
||
187 | */ |
||
188 | abstract protected function readContents($mixed); |
||
189 | |||
190 | /** |
||
191 | * After the Document's content has been read, process the it and handle any parsing that's needed. |
||
192 | * |
||
193 | * @param mixed $mixed Any information returned from the readContents() method. |
||
194 | */ |
||
195 | protected function afterReadContents($mixed) {} |
||
196 | |||
197 | /** |
||
198 | * Functionality that needs to take place before this document is considered "compiled," meaning everything has been |
||
199 | * processed, configured, and built. |
||
200 | */ |
||
201 | protected function beforeCompile() {} |
||
202 | } |
||
203 |