XOOPS  2.6.0
CacheManager.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core\Cache;
13 
14 use Stash\Pool;
18 use Xoops\Core\Yaml;
19 
31 {
37  protected $pools = array();
38 
44  protected $poolDefs = array(
45  );
46 
52  protected $xoops;
53 
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 
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 
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  // try alternative driver as filesystem has max path length issues on Windows
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 
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 
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  }
184  }
185  if (!$pool) {
186  $this->xoops->logger()->warn('Could not create cache pool '.$name);
187  return $pool;
188  }
189 
190  return new Access($pool);
191  }
192 
201  protected function getDriver($driverName, $options)
202  {
203  $driver = false;
204  $driverClass = DriverList::getDriverClass($driverName);
205 
206  if ($driverClass!==false && $driverClass::isAvailable()) {
207  $options = is_array($options) ? $options : array();
208  $driver = new $driverClass();
209  $driver->setOptions($options);
210  }
211  return ($driver instanceof DriverInterface) ? $driver : false;
212  }
213 
222  protected function getDefaultPool($originalName)
223  {
224  $this->xoops->events()->triggerEvent('debug.log', 'Substituting default cache pool for '.$originalName);
225  $name = 'default';
226  if (array_key_exists($name, $this->pools)) {
227  return $this->pools[$name];
228  }
229  $pool = $this->startPoolAccess($name);
230  if ($pool===false) {
231  $this->xoops->logger()->error('Could not create default cache pool');
232  $pool = new Access(new \Stash\Pool());
233  }
234  $this->pools[$name] = $pool;
235  return $pool;
236  }
237 }
static getInstance()
Definition: Xoops.php:160
getDriver($driverName, $options)
$options['editor']
static getDriverClass($name)
Definition: DriverList.php:30
static readWrapped($yamlFile)
Definition: Yaml.php:185
static get($name)
static saveWrapped($var, $yamlFile, $inline=4, $indent=4)
Definition: Yaml.php:212