1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie\Modules\Thumbnailer;
13
14 const CACHE_VERSIONS = true;
15
16 class Versions implements \ArrayAccess, \IteratorAggregate
17 {
18 19 20 21 22 23 24 25 26
27 static public function prototype_get_thumbnailer_versions(\ICanBoogie\Core $core)
28 {
29 if (CACHE_VERSIONS)
30 {
31 $versions = $core->vars['cached_thumbnailer_versions'];
32
33 if (!$versions)
34 {
35 $versions = self::collect($core);
36
37 $core->vars['cached_thumbnailer_versions'] = $versions;
38 }
39 }
40 else
41 {
42 $versions = self::collect($core);
43 }
44
45 $instance = new static($versions);
46
47 new Versions\AlterEvent($instance);
48
49 return $instance;
50 }
51
52 53 54 55 56
57 static private function collect(\ICanBoogie\Core $core)
58 {
59 $versions = array();
60 $definitions = $core->registry
61 ->select('SUBSTR(name, LENGTH("thumbnailer.versions.") + 1) as name, value')
62 ->where('name LIKE ?', 'thumbnailer.versions.%')
63 ->pairs;
64
65 foreach ($definitions as $name => $options)
66 {
67 if (!$options || !is_string($options) || $options{0} != '{')
68 {
69 \ICanBoogie\log_error('Bad version: %name, :options', array('name' => $name, 'options' => $options));
70
71 continue;
72 }
73
74 $versions[$name] = Version::normalize(json_decode($options, true));
75 }
76
77 return $versions;
78 }
79
80 81 82 83 84
85 protected $versions;
86
87 88 89 90 91
92 public function __construct(array $versions=array())
93 {
94 foreach ($versions as $name => $version)
95 {
96 $this[$name] = $version;
97 }
98 }
99
100 101 102
103 public function save()
104 {
105 foreach ($this->versions as $name => $version)
106 {
107 $this->save_version($name, $version);
108 }
109 }
110
111 112 113 114 115 116 117 118 119
120 public function save_version($name, $version)
121 {
122 global $core;
123
124 if (!($version instanceof Versions))
125 {
126 $version = new Version($version);
127 }
128
129 $options = $version->to_array(Version::ARRAY_FILTER | Version::ARRAY_SHORTEN);
130 $core->registry["thumbnailer.versions.$name"] = json_encode($options);
131
132
133
134 unset($core->vars['cached_thumbnailer_versions']);
135
136 return $options;
137 }
138
139 140 141
142 public function offsetExists($version)
143 {
144 return isset($this->versions[$version]);
145 }
146
147 148 149 150 151
152 public function offsetGet($version)
153 {
154 if (!$this->offsetExists($version))
155 {
156 throw new VersionNotDefined($version);
157 }
158
159 $v = $this->versions[$version];
160
161 if (!($v instanceof Version))
162 {
163 $v = new Version($v);
164 $this->versions[$version] = $v;
165 }
166
167 return $v;
168 }
169
170 171 172 173 174 175
176 public function offsetSet($version, $options)
177 {
178 $this->versions[$version] = $options;
179 }
180
181 182 183
184 public function offsetUnset($version)
185 {
186 unset($this->versions[$version]);
187 }
188
189 public function getIterator()
190 {
191 return new \ArrayIterator($this->versions);
192 }
193 }
194
195 196 197
198
199 200 201 202 203
204 class VersionNotDefined extends \InvalidArgumentException
205 {
206 private $version;
207
208 public function __construct($version, $code=500, \Exception $previous=null)
209 {
210 $this->version = $version;
211
212 parent::__construct("Version not defined: $version.", $code, $previous);
213 }
214
215 public function __get($property)
216 {
217 if ($property == 'version')
218 {
219 return $this->version;
220 }
221 }
222 }
223
224 namespace ICanBoogie\Modules\Thumbnailer\Versions;
225
226 227 228
229 class AlterEvent extends \ICanBoogie\Event
230 {
231 232 233 234 235
236 public function __construct(\ICanBoogie\Modules\Thumbnailer\Versions $target)
237 {
238 parent::__construct($target, 'alter');
239 }
240 }