1 | <?php |
||
22 | abstract class TrackingManager extends BaseManager |
||
23 | { |
||
24 | /** |
||
25 | * @var FileExplorer |
||
26 | */ |
||
27 | protected $fileExplorer; |
||
28 | |||
29 | /** |
||
30 | * An array corresponding with $folderDefinitions to store metadata regarding a specificc folder. |
||
31 | * |
||
32 | * $folderDefinitionsOption['<folder path>'] = array() |
||
33 | * |
||
34 | * @var string[] |
||
35 | */ |
||
36 | protected $folderDefinitionsOptions; |
||
|
|||
37 | |||
38 | /** |
||
39 | * An array of folders which tracked items are stored in. |
||
40 | * |
||
41 | * $folderDefinitions[] = '<folder path>' |
||
42 | * |
||
43 | * @var string[] |
||
44 | */ |
||
45 | protected $folderDefinitions; |
||
46 | |||
47 | /** |
||
48 | * The storage which contains the same information as $trackedItems but organized by relative file path instead of a |
||
49 | * namespace or file name without extension. |
||
50 | * |
||
51 | * $trackedItemsOptions['<relative file path>'] = mixed |
||
52 | * |
||
53 | * @var TrackableDocument[] |
||
54 | */ |
||
55 | protected $trackedItemsFlattened; |
||
56 | |||
57 | /** |
||
58 | * The storage used to cache any information needed for a specific FrontMatterObject or DataItem. |
||
59 | * |
||
60 | * For example, with a DataItem, which is just an array, the file path to the original file can be stored in this |
||
61 | * array to be accessible in the future to refresh the contents without parsing all of the files again. |
||
62 | * |
||
63 | * $trackedItemsOptions['<relative file path>'] = array |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $trackedItemsOptions; |
||
68 | |||
69 | /** |
||
70 | * The storage used for TrackableDocuments in the respective static classes. |
||
71 | * |
||
72 | * $trackedItems['<namespace>']['<file name w/o extension>'] = mixed |
||
73 | * $trackedItems['<file name w/o extension>'] = mixed |
||
74 | * |
||
75 | * @var TrackableDocument[] |
||
76 | */ |
||
77 | protected $trackedItems; |
||
78 | |||
79 | 42 | public function __construct() |
|
89 | |||
90 | /** |
||
91 | * @param SplFileInfo|string $filePath |
||
92 | * |
||
93 | * @return mixed|null |
||
94 | */ |
||
95 | public function createNewItem($filePath) |
||
99 | |||
100 | /** |
||
101 | * Check whether a file is already being tracked. |
||
102 | * |
||
103 | * @param string $filePath The relative path of the file |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 1 | public function isTracked($filePath) |
|
111 | |||
112 | /** |
||
113 | * Check to see if a given file path matches this tracker's definition and would be tracked. |
||
114 | * |
||
115 | * This function should be used to check whether or not to add a file to this tracker after the initial scan has |
||
116 | * already happened. |
||
117 | * |
||
118 | * @param string $filePath |
||
119 | * |
||
120 | * @return bool True if the file is inside a tracked folder |
||
121 | */ |
||
122 | public function shouldBeTracked($filePath) |
||
134 | |||
135 | /** |
||
136 | * Update the contents of a specified file. |
||
137 | * |
||
138 | * @param SplFileInfo|string $filePath The relative path of the file |
||
139 | * |
||
140 | * @return PageView |
||
141 | */ |
||
142 | public function refreshItem($filePath) |
||
146 | |||
147 | /// |
||
148 | // Internal object handling |
||
149 | /// |
||
150 | |||
151 | protected function addFileToTracker(SplFileInfo &$file) |
||
155 | |||
156 | protected function delFileFromTracker(SplFileInfo &$file) |
||
160 | |||
161 | /** |
||
162 | * Add a TrackableDocument to the tracker. |
||
163 | * |
||
164 | * @param TrackableDocument $trackedItem |
||
165 | * @param string|null $namespace |
||
166 | */ |
||
167 | 41 | protected function addObjectToTracker(TrackableDocument &$trackedItem, $namespace = null) |
|
180 | |||
181 | /** |
||
182 | * Remove an entry from the tracked items array. |
||
183 | * |
||
184 | * @param TrackableDocument $trackedItem |
||
185 | * @param string|null $namespace |
||
186 | */ |
||
187 | protected function delObjectFromTracker(TrackableDocument &$trackedItem, $namespace = null) |
||
200 | |||
201 | /// |
||
202 | // Extra options stored for future use |
||
203 | /// |
||
204 | |||
205 | /** |
||
206 | * Save a folder that is tracked by this manager and its respective options. |
||
207 | * |
||
208 | * @param string $folderPath |
||
209 | * @param array $options |
||
210 | */ |
||
211 | 39 | protected function saveFolderDefinition($folderPath, array $options = array()) |
|
216 | |||
217 | /** |
||
218 | * Save any options related to an item needed in order to refresh the content. |
||
219 | * |
||
220 | * @param string $filePath |
||
221 | * @param array $options |
||
222 | */ |
||
223 | 2 | protected function saveTrackerOptions($filePath, array $options = array()) |
|
227 | |||
228 | /** |
||
229 | * Delete any options that were saved corresponding to an item. |
||
230 | * |
||
231 | * @param string $filePath |
||
232 | */ |
||
233 | protected function forgetTrackerOptions($filePath) |
||
237 | |||
238 | /// |
||
239 | // Handling of trackable items |
||
240 | /// |
||
241 | |||
242 | /** |
||
243 | * Parse the specified folder for items to track. |
||
244 | * |
||
245 | * @param string $path |
||
246 | * @param array $options Special options that will be passed to the static::parseTrackableItem() implementation |
||
247 | * @param array $includes |
||
248 | * @param array $excludes |
||
249 | */ |
||
250 | 41 | protected function scanTrackableItems($path, array $options = array(), array $includes = array(), array $excludes = array()) |
|
265 | |||
266 | /** |
||
267 | * Handle a specific file type, parse it into the appropriate object type, and add it to the tracker. |
||
268 | * |
||
269 | * This function should make use of the appropriate functions: |
||
270 | * |
||
271 | * - TrackingManager::addObjectToTracker() |
||
272 | * - TrackingManager::addFileToTracker() |
||
273 | * - TrackingManager::saveTrackerOptions() |
||
274 | * |
||
275 | * @param SplFileInfo $filePath |
||
276 | * @param mixed $options |
||
277 | * |
||
278 | * @return mixed|null |
||
279 | */ |
||
280 | abstract protected function handleTrackableItem($filePath, array $options = array()); |
||
281 | |||
282 | /// |
||
283 | // Utility functions |
||
284 | /// |
||
285 | |||
286 | /** |
||
287 | * Return an array of JailedDocuments created from the tracked items |
||
288 | * |
||
289 | * @param array $elements |
||
290 | * |
||
291 | * @return JailedDocument[]|JailedDocument[][] |
||
292 | */ |
||
293 | 21 | protected static function getJailedTrackedItems(array &$elements) |
|
317 | } |
||
318 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.