1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
<?php /** * Recently items to display on home page */ class Loco_data_RecentItems extends Loco_data_Option {
/** * Global instance of recent items * @var Loco_data_RecentItems */ private static $current;
/** * {@inheritdoc} */ public function getKey(){ return 'recent'; }
/** * @return Loco_data_RecentItems */ public static function get(){ if( ! self::$current ){ self::$current = new Loco_data_RecentItems; self::$current->fetch(); } return self::$current; }
/** * Trash data and remove from memory */ public static function destroy(){ $tmp = new Loco_data_RecentItems; $tmp->remove(); self::$current = null; }
/** * @internal * @return Loco_data_RecentItems */ private function push( $object, array $indexes ){ foreach( $indexes as $key => $id ){ $stack = isset($this[$key]) ? $this[$key] : []; // remove before add ensures latest item appended to hashmap unset($stack[$id]); $stack[$id] = time(); $this[$key] = $stack; // TODO prune stack to maximum length } return $this; }
/** * @return array */ private function getItems( $key, $offset, $count ){ $stack = isset($this[$key]) ? $this[$key] : []; // hash map should automatically be in "push" order, meaning most recent last // sorting gives wrong order for same-second updates (only relevant in tests, but still..) // asort( $stack, SORT_NUMERIC ); $stack = array_reverse( array_keys( $stack ) ); if( is_null($count) && 0 === $offset ){ return $stack; } return array_slice( $stack, $offset, $count, false ); }
/** * @return int */ private function hasItem( $key, $id ){ if( isset($this[$key]) && ( $items = $this[$key] ) && isset($items[$id]) ){ return $items[$id]; } return 0; }
/** * Push bundle to the front of recent bundles stack * @return Loco_data_RecentItems */ public function pushBundle( Loco_package_Bundle $bundle ){ return $this->push( $bundle, [ 'bundle' => $bundle->getId() ] ); }
/** * Get bundle IDs * @return array */ public function getBundles( $offset = 0, $count = null ){ return $this->getItems('bundle', $offset, $count ); }
/** * Check if a bundle has been recently used * @return int timestamp item was added, 0 if absent */ public function hasBundle( $id ){ return $this->hasItem( 'bundle', $id ); }
/** * TODO other types of item * Push project to the front of recent bundles stack * @return Loco_data_RecentItems * public function pushProject( Loco_package_Project $project ){ return $this; }*/
}
|