1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Cache;
13:
14: use Stash\Pool;
15: use Stash\Interfaces\DriverInterface;
16: use Xoops\Core\Cache\DriverList;
17: use Xoops\Core\Cache\Access;
18: use Xoops\Core\Yaml;
19:
20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class CacheManager
31: {
32: 33: 34: 35: 36:
37: protected $pools = array();
38:
39: 40: 41: 42: 43:
44: protected $poolDefs = array(
45: );
46:
47: 48: 49: 50: 51:
52: protected $xoops;
53:
54: 55: 56:
57: public function __construct()
58: {
59: $this->xoops = \Xoops::getInstance();
60: $defaults = $this->getDefaults();
61: $xoops_var_path = \XoopsBaseConfig::get('var-path');
62: $cache_file = $xoops_var_path . '/configs/cache.php';
63: $poolDefs = Yaml::readWrapped($cache_file);
64: if (empty($poolDefs)) {
65: Yaml::saveWrapped($defaults, $cache_file);
66: }
67: $poolDefs = is_array($poolDefs) ? $poolDefs : array();
68: $this->poolDefs = array_merge($defaults, $poolDefs);
69: }
70:
71: 72: 73: 74: 75:
76: private static function getDefaults()
77: {
78: $defaults = array(
79: 'default' => array(
80: 'driver' => 'FileSystem',
81: 'options' => array(
82: 'path' => \XoopsBaseConfig::get('var-path') . '/stash/',
83: ),
84: 'dirSplit' => 1,
85: ),
86: 'temp' => array(
87: 'driver' => 'Ephemeral',
88: 'options' => array(),
89: ),
90: );
91: return $defaults;
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
107: public static function createDefaultConfig()
108: {
109: $configFile = \XoopsBaseConfig::get('var-path') . '/configs/cache.php';
110: if (file_exists($configFile)) {
111: return;
112: }
113: $defaults = self::getDefaults();
114: if (false !== stripos(PHP_OS, 'WIN')) {
115: $pathLen = strlen($defaults['default']['options']['path']);
116: if (260 <= ($pathLen+202)) {
117:
118: if (array_key_exists("SQLite", \Stash\DriverList::getAvailableDrivers())) {
119: $defaults['default']['driver'] = 'SQLite';
120: unset($defaults['default']['options']['dirSplit']);
121: @mkdir($defaults['default']['options']['path']);
122: } else {
123: trigger_error("Manual cache configuration required.");
124: }
125: }
126: }
127: Yaml::saveWrapped($defaults, $configFile);
128: }
129:
130: 131: 132: 133: 134: 135: 136:
137: public function getCache($name)
138: {
139: $pool = false;
140: if (array_key_exists($name, $this->pools)) {
141: $pool = $this->pools[$name];
142: } elseif (array_key_exists($name, $this->poolDefs)) {
143: $pool = $this->startPoolAccess($name);
144: }
145: if ($pool === false) {
146: $pool = $this->getDefaultPool($name);
147: }
148:
149: $this->pools[$name] = $pool;
150:
151: return $pool;
152: }
153:
154: 155: 156: 157: 158: 159: 160: 161:
162: protected function startPoolAccess($name)
163: {
164: $pool = false;
165: $options = false;
166: if (isset($this->poolDefs[$name]['options'])) {
167: $options = $this->poolDefs[$name]['options'];
168: }
169: $driverName = $this->poolDefs[$name]['driver'];
170: if (0 === strcasecmp($driverName, 'Composite')) {
171: $drivers = array();
172: foreach ($this->poolDefs[$name]['options']['drivers'] as $subDriver) {
173: $drivers[] = $this->getDriver($subDriver['driver'], $subDriver['options']);
174: }
175: $options['drivers'] = $drivers;
176: }
177:
178: $driver = $this->getDriver($driverName, $options);
179: if ($driver!==false) {
180: $pool = new Pool($driver);
181: if (is_object($pool)) {
182: $pool->setLogger($this->xoops->logger());
183: $pool->setNamespace($this->xoops->db()->prefix());
184: }
185: }
186: if (!$pool) {
187: $this->xoops->logger()->warn('Could not create cache pool '.$name);
188: return $pool;
189: }
190:
191: return new Access($pool);
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201:
202: protected function getDriver($driverName, $options)
203: {
204: $driver = false;
205: $driverClass = DriverList::getDriverClass($driverName);
206:
207: if ($driverClass!==false && $driverClass::isAvailable()) {
208: $options = is_array($options) ? $options : array();
209: $driver = new $driverClass();
210: $driver->setOptions($options);
211: }
212: return ($driver instanceof DriverInterface) ? $driver : false;
213: }
214:
215: 216: 217: 218: 219: 220: 221: 222:
223: protected function getDefaultPool($originalName)
224: {
225: $this->xoops->events()->triggerEvent('debug.log', 'Substituting default cache pool for '.$originalName);
226: $name = 'default';
227: if (array_key_exists($name, $this->pools)) {
228: return $this->pools[$name];
229: }
230: $pool = $this->startPoolAccess($name);
231: if ($pool===false) {
232: $this->xoops->logger()->error('Could not create default cache pool');
233: $pool = new Access(new \Stash\Pool());
234: }
235: $this->pools[$name] = $pool;
236: return $pool;
237: }
238: }
239: