1 | <?php |
||
23 | abstract class TrackingManager extends BaseManager |
||
24 | { |
||
25 | /** |
||
26 | * @var FileExplorer |
||
27 | */ |
||
28 | protected $fileExplorer = null; |
||
29 | |||
30 | /** |
||
31 | * An array corresponding with $folderDefinitions to store metadata regarding a specificc folder. |
||
32 | * |
||
33 | * $folderDefinitionsOption['<folder path>'] = array() |
||
34 | * |
||
35 | * @var string[] |
||
36 | */ |
||
37 | protected $folderDefinitionsOptions = []; |
||
|
|||
38 | |||
39 | /** |
||
40 | * An array of folders which tracked items are stored in. |
||
41 | * |
||
42 | * $folderDefinitions[] = '<folder path>' |
||
43 | * |
||
44 | * @var string[] |
||
45 | */ |
||
46 | protected $folderDefinitions = []; |
||
47 | |||
48 | /** |
||
49 | * The storage which contains the same information as $trackedItems but organized by relative file path instead of a |
||
50 | * namespace or file name without extension. |
||
51 | * |
||
52 | * $trackedItemsOptions['<relative file path>'] = mixed |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $trackedItemsFlattened = []; |
||
57 | |||
58 | /** |
||
59 | * The storage used to cache any information needed for a specific FrontMatterObject or DataItem. |
||
60 | * |
||
61 | * For example, with a DataItem, which is just an array, the file path to the original file can be stored in this |
||
62 | * array to be accessible in the future to refresh the contents without parsing all of the files again. |
||
63 | * |
||
64 | * $trackedItemsOptions['<relative file path>'] = array |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $trackedItemsOptions = []; |
||
69 | |||
70 | /** |
||
71 | * The storage used for ReadableDocument in the respective static classes. |
||
72 | * |
||
73 | * $trackedItems['<namespace>']['<file name w/o extension>'] = mixed |
||
74 | * $trackedItems['<file name w/o extension>'] = mixed |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $trackedItems = []; |
||
79 | |||
80 | /** |
||
81 | * @param File|string $filePath |
||
82 | * |
||
83 | * @return mixed|null |
||
84 | */ |
||
85 | public function createNewItem($filePath) |
||
89 | |||
90 | /** |
||
91 | * Check whether a file is already being tracked. |
||
92 | * |
||
93 | * @param string $filePath The relative path of the file |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function isTracked($filePath) |
||
98 | { |
||
99 | return array_key_exists((string)$filePath, $this->trackedItemsFlattened); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Check to see if a given file path matches this tracker's definition and would be tracked. |
||
104 | * |
||
105 | * This function should be used to check whether or not to add a file to this tracker after the initial scan has |
||
106 | * already happened. |
||
107 | * |
||
108 | * @param string $filePath |
||
109 | * |
||
110 | * @return bool True if the file is inside a tracked folder |
||
111 | */ |
||
112 | public function shouldBeTracked($filePath) |
||
124 | |||
125 | /** |
||
126 | * Update the contents of a specified file. |
||
127 | * |
||
128 | * @param File|string $filePath The relative path of the file |
||
129 | * |
||
130 | * @return mixed|null |
||
131 | */ |
||
132 | public function refreshItem($filePath) |
||
136 | |||
137 | /// |
||
138 | // Internal object handling |
||
139 | /// |
||
140 | |||
141 | protected function addFileToTracker(File &$file) |
||
145 | |||
146 | protected function delFileFromTracker(File &$file) |
||
150 | |||
151 | /** |
||
152 | * Add a ReadableDocument to the tracker. |
||
153 | * |
||
154 | * @param ReadableDocument $trackedItem |
||
155 | * @param string|null $namespace |
||
156 | */ |
||
157 | 16 | protected function addObjectToTracker(ReadableDocument &$trackedItem, $namespace = null) |
|
170 | |||
171 | /** |
||
172 | * Remove an entry from the tracked items array. |
||
173 | * |
||
174 | * @param ReadableDocument $trackedItem |
||
175 | * @param string|null $namespace |
||
176 | */ |
||
177 | protected function delObjectFromTracker(ReadableDocument &$trackedItem, $namespace = null) |
||
190 | |||
191 | /// |
||
192 | // Extra options stored for future use |
||
193 | /// |
||
194 | |||
195 | /** |
||
196 | * Save a folder that is tracked by this manager and its respective options. |
||
197 | * |
||
198 | * @param string $folderPath |
||
199 | * @param array $options |
||
200 | */ |
||
201 | 2 | protected function saveFolderDefinition($folderPath, array $options = array()) |
|
206 | |||
207 | /** |
||
208 | * Save any options related to an item needed in order to refresh the content. |
||
209 | * |
||
210 | * @param string $filePath |
||
211 | * @param array $options |
||
212 | */ |
||
213 | 2 | protected function saveTrackerOptions($filePath, array $options = array()) |
|
217 | |||
218 | /** |
||
219 | * Delete any options that were saved corresponding to an item. |
||
220 | * |
||
221 | * @param string $filePath |
||
222 | */ |
||
223 | protected function forgetTrackerOptions($filePath) |
||
227 | |||
228 | /// |
||
229 | // Handling of trackable items |
||
230 | /// |
||
231 | |||
232 | /** |
||
233 | * Parse the specified folder for items to track. |
||
234 | * |
||
235 | * @param string $path |
||
236 | * @param array $options Special options that will be passed to the static::parseTrackableItem() implementation |
||
237 | * @param array $includes |
||
238 | * @param array $excludes |
||
239 | */ |
||
240 | 16 | protected function scanTrackableItems($path, array $options = array(), array $includes = array(), array $excludes = array()) |
|
255 | |||
256 | /** |
||
257 | * Handle a specific file type, parse it into the appropriate object type, and add it to the tracker. |
||
258 | * |
||
259 | * This function should make use of the appropriate functions: |
||
260 | * |
||
261 | * - TrackingManager::addObjectToTracker() |
||
262 | * - TrackingManager::addFileToTracker() |
||
263 | * - TrackingManager::saveTrackerOptions() |
||
264 | * |
||
265 | * @param File $filePath |
||
266 | * @param array $options |
||
267 | * |
||
268 | * @return mixed|null |
||
269 | */ |
||
270 | abstract protected function handleTrackableItem(File $filePath, array $options = array()); |
||
271 | |||
272 | /// |
||
273 | // Utility functions |
||
274 | /// |
||
275 | |||
276 | /** |
||
277 | * Return an array of JailedDocuments created from the tracked items |
||
278 | * |
||
279 | * @param array $elements |
||
280 | * @param \Closure $name |
||
281 | * |
||
282 | * @return JailedDocument[]|JailedDocument[][] |
||
283 | */ |
||
284 | 2 | protected static function getJailedTrackedItems(array &$elements, \Closure $name = null) |
|
316 | } |
||
317 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.