1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie\Modules\Thumbnailer;
13
14 use ICanBoogie\FileCache;
15 use ICanBoogie\I18n;
16
17 use Brickrouge\Element;
18 use Brickrouge\Form;
19 use Brickrouge\Text;
20
21 22 23 24 25
26 class CacheManager extends \ICanBoogie\Object implements \Icybee\Modules\Cache\CacheManagerInterface
27 {
28 public $title = "Thumbnails";
29 public $description = "Thumbnails created on the fly by the <q>Thumbnailer</q> module.";
30 public $group = 'resources';
31 public $state = null;
32
33 34 35 36 37 38 39
40 static public $config = array
41 (
42 'cleanup_interval' => 15,
43 'repository_size' => 8
44 );
45
46 protected function get_config_preview()
47 {
48 global $core;
49
50 $registry = $core->registry;
51
52 $rc = I18n\t("The cache size does not exceed :cache_sizeMb.", array('cache_size' => $registry['thumbnailer.cache_size'] ?: 8));
53 $rc .= ' ' . I18n\t("The cache is cleaned every :cleanup_interval minutes.", array('cleanup_interval' => $registry['thumbnailer.cleanup_interval'] ?: 15));
54
55 return $rc;
56 }
57
58 protected function get_editor()
59 {
60 global $core;
61
62 $registry = $core->registry;
63
64 return new Form
65 (
66 array
67 (
68 Form::RENDERER => 'Simple',
69 Element::CHILDREN => array
70 (
71 'cache_size' => new Text
72 (
73 array
74 (
75 Form::LABEL => "Maximum cache size",
76 Text::ADDON => 'Mb',
77
78 'size' => 5,
79 'class' => 'measure',
80 'value' => $registry['thumbnailer.cache_size'] ?: 8
81 )
82 ),
83
84 'cleanup_interval' => new Text
85 (
86 array
87 (
88 Form::LABEL => "Interval between cleanings",
89 Text::ADDON => 'minutes',
90
91 'size' => 5,
92 'class' => 'measure',
93 'value' => $registry['thumbnailer.cleanup_interval'] ?: 15
94 )
95 ),
96
97
98 ),
99
100 'class' => 'stacked'
101 )
102 );
103 }
104
105 106 107 108 109
110 protected function get_path()
111 {
112 return \ICanBoogie\REPOSITORY . 'thumbnailer' . DIRECTORY_SEPARATOR;
113 }
114
115 116 117 118 119
120 protected function get_handler()
121 {
122 return new FileCache
123 (
124 array
125 (
126 FileCache::T_REPOSITORY => $this->path,
127 FileCache::T_REPOSITORY_SIZE => self::$config['repository_size'] * 1024
128 )
129 );
130 }
131
132 public function enable()
133 {
134
135 }
136
137 public function disable()
138 {
139
140 }
141
142 public function stat()
143 {
144 return \Icybee\Modules\Cache\Module::get_files_stat(\ICanBoogie\REPOSITORY . 'thumbnailer');
145 }
146
147 public function clear()
148 {
149 global $core;
150
151 $files = glob(\ICanBoogie\REPOSITORY . 'thumbnailer/*');
152
153 foreach ($files as $file)
154 {
155 unlink($file);
156 }
157
158 return count($files);
159 }
160
161
162 163 164
165 public function clean()
166 {
167 $marker = \ICanBoogie\REPOSITORY . 'thumbnailer/.cleanup';
168
169 $time = file_exists($marker) ? filemtime($marker) : 0;
170 $interval = self::$config['cleanup_interval'] * 60;
171 $now = time();
172
173 if ($time + $interval > $now)
174 {
175 return;
176 }
177
178 $this->handler->clean();
179
180 touch($marker);
181 }
182
183 public function config($params)
184 {
185 global $core;
186
187 if (!empty($params['cache_size']))
188 {
189 $core->registry['thumbnailer.cache_size'] = (int) $params['cache_size'];
190 }
191
192 if (!empty($params['cleanup_interval']))
193 {
194 $core->registry['thumbnailer.cleanup_interval'] = (int) $params['cleanup_interval'];
195 }
196 }
197
198 public function retrieve($key, array $callback, array $userdata)
199 {
200 $this->clean();
201
202 return call_user_func_array(array($this->handler, 'get'), func_get_args());
203 }
204 }