|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2016 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\Manager; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Object\FrontMatterObject; |
|
11
|
|
|
use Symfony\Component\Finder\Finder; |
|
12
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class TrackingManager |
|
16
|
|
|
* |
|
17
|
|
|
* @package allejo\stakx\Manager |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class TrackingManager extends BaseManager implements Trackable |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The storage which contains the same information as $trackedItems but organized by relative file path instead of a |
|
23
|
|
|
* namespace or file name without extension. |
|
24
|
|
|
* |
|
25
|
|
|
* $trackedItemsOptions['<relative file path>'] = mixed |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $trackedItemsFlattened; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The storage used to cache any information needed for a specific FrontMatterObject or DataItem. |
|
33
|
|
|
* |
|
34
|
|
|
* For example, with a DataItem, which is just an array, the file path to the original file can be stored in this |
|
35
|
|
|
* array to be accessible in the future to refresh the contents without parsing all of the files again. |
|
36
|
|
|
* |
|
37
|
|
|
* $trackedItemsOptions['<relative file path>'] = array |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $trackedItemsOptions; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The storage used for either FrontMatterObjects or DataItems in the respective static classes |
|
45
|
|
|
* |
|
46
|
|
|
* $trackedItems['<namespace>']['<file name w/o extension>'] = mixed |
|
47
|
|
|
* $trackedItems['<file name w/o extension>'] = mixed |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $trackedItems; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set to true when file tracking is enabled |
|
55
|
|
|
* |
|
56
|
|
|
* @var bool |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $tracking; |
|
59
|
|
|
|
|
60
|
4 |
|
public function __construct() |
|
61
|
|
|
{ |
|
62
|
4 |
|
parent::__construct(); |
|
63
|
|
|
|
|
64
|
4 |
|
$this->trackedItemsFlattened = array(); |
|
65
|
4 |
|
$this->trackedItemsOptions = array(); |
|
66
|
4 |
|
$this->trackedItems = array(); |
|
67
|
4 |
|
$this->tracking = false; |
|
68
|
4 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addArrayToTracker ($key, $data, $filePath, $namespace = null) |
|
74
|
|
|
{ |
|
75
|
|
|
if (is_null($namespace)) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->trackedItems[$key] = $data; |
|
78
|
|
|
} |
|
79
|
|
|
else |
|
80
|
|
|
{ |
|
81
|
|
|
$this->trackedItems[$namespace][$key] = $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->trackedItemsFlattened[$filePath] = $data; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function addObjectToTracker (&$trackedItem, $namespace = null) |
|
91
|
|
|
{ |
|
92
|
4 |
|
if (!($trackedItem instanceof FrontMatterObject)) |
|
93
|
4 |
|
{ |
|
94
|
|
|
throw new \InvalidArgumentException('Only objects can be added to the tracker'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
if (is_null($namespace)) |
|
98
|
4 |
|
{ |
|
99
|
|
|
$this->trackedItems[$trackedItem->getName()] = &$trackedItem; |
|
100
|
|
|
} |
|
101
|
|
|
else |
|
102
|
|
|
{ |
|
103
|
4 |
|
$this->trackedItems[$namespace][$trackedItem->getName()] = &$trackedItem; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
$this->trackedItemsFlattened[$trackedItem->getRelativeFilePath()] = &$trackedItem; |
|
107
|
4 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function delArrayFromTracker($key, $filePath, $namespace = null) |
|
113
|
|
|
{ |
|
114
|
|
|
if (is_null($namespace)) |
|
115
|
|
|
{ |
|
116
|
|
|
unset($this->trackedItems[$key]); |
|
117
|
|
|
} |
|
118
|
|
|
else |
|
119
|
|
|
{ |
|
120
|
|
|
unset($this->trackedItems[$namespace][$key]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
unset($this->trackedItemsFlattened[$filePath]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function delObjectFromTracker ($trackedItem, $namespace = null) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->delArrayFromTracker( |
|
132
|
|
|
$trackedItem->getFileName(), |
|
133
|
|
|
$trackedItem->getRelativeFilePath(), |
|
134
|
|
|
$namespace |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function enableTracking ($enabled) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->tracking = $enabled; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* {@inheritdoc} |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function isTracked ($filePath) |
|
150
|
|
|
{ |
|
151
|
1 |
|
return array_key_exists($filePath, $this->trackedItemsFlattened); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function refreshItem ($filePath) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->handleTrackableItem( |
|
160
|
|
|
$filePath, |
|
|
|
|
|
|
161
|
|
|
$this->trackedItemsOptions[$filePath] |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Save any options related to an item needed in order to refresh the content |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $filePath |
|
169
|
|
|
* @param array $options |
|
170
|
|
|
*/ |
|
171
|
|
|
public function saveOptions ($filePath, $options = array()) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->trackedItemsOptions[$filePath] = $options; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Parse the specified folder for items to track |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $folder |
|
180
|
|
|
* @param mixed $options Special options that will be passed to the static::parseTrackableItem() implementation |
|
181
|
|
|
* @param array $includes |
|
182
|
|
|
* @param array $excludes |
|
183
|
|
|
*/ |
|
184
|
4 |
|
protected function scanTrackableItems ($folder, $options = array(), $includes = array(), $excludes = array()) |
|
185
|
|
|
{ |
|
186
|
4 |
|
$finder = $this->fs->getFinder( |
|
187
|
4 |
|
$includes, |
|
188
|
4 |
|
$excludes, |
|
189
|
4 |
|
$this->fs->absolutePath($folder) |
|
190
|
4 |
|
); |
|
191
|
|
|
|
|
192
|
|
|
/** @var SplFileInfo $file */ |
|
193
|
4 |
|
foreach ($finder as $file) |
|
194
|
|
|
{ |
|
195
|
4 |
|
$this->handleTrackableItem($file, $options); |
|
196
|
4 |
|
} |
|
197
|
4 |
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param SplFileInfo $filePath |
|
201
|
|
|
* @param mixed $options |
|
202
|
|
|
* |
|
203
|
|
|
* @return mixed |
|
204
|
|
|
*/ |
|
205
|
|
|
abstract protected function handleTrackableItem ($filePath, $options = array()); |
|
206
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.