예제 #1
0
 /**
  * Loads the composite driver from constants
  * @param $level
  * @return \Stash\Interfaces\DriverInterface
  */
 protected function loadConfig($level)
 {
     $drivers = array();
     $driver_configs = Config::get("concrete.cache.levels.{$level}.drivers", array());
     foreach ($driver_configs as $driver_build) {
         if (!$driver_build) {
             continue;
         }
         $class = array_get($driver_build, 'class', '');
         if ($class && class_exists($class)) {
             $implements = class_implements($class);
             // Make sure that the provided class implements the DriverInterface
             if (isset($implements['Stash\\Interfaces\\DriverInterface'])) {
                 /** @type \Stash\Interfaces\DriverInterface $temp_driver */
                 $temp_driver = new $class();
                 if ($options = array_get($driver_build, 'options', null)) {
                     $temp_driver->setOptions($options);
                 }
                 $drivers[] = $temp_driver;
             } else {
                 throw new \RuntimeException('Cache driver class must implement \\Stash\\Interfaces\\DriverInterface.');
             }
         }
     }
     $count = count($drivers);
     if ($count > 1) {
         $driver = new Composite();
         $driver->setOptions(array('drivers' => $drivers));
     } elseif ($count === 1) {
         $driver = $drivers[0];
     } else {
         $driver = new BlackHole();
     }
     return $driver;
 }
예제 #2
0
파일: Cache.php 프로젝트: ngreimel/kovent
 /**
  * Loads the composite driver from constants
  * @param $level
  * @return \Stash\Interfaces\DriverInterface
  */
 protected function loadConfig($level)
 {
     $drivers = array();
     $level_config = Config::get("concrete.cache.levels.{$level}", array());
     if (isset($level_config['drivers'])) {
         foreach ($level_config['drivers'] as $driver_build) {
             if (class_exists($driver_build['class'])) {
                 $temp_driver = new $driver_build['class']();
                 if (isset($driver_build['options'])) {
                     $temp_driver->setOptions($driver_build['options']);
                 }
                 $drivers[] = $temp_driver;
             }
         }
     }
     $count = count($drivers);
     if ($count > 1) {
         $driver = new Composite();
         $driver->setOptions(array('drivers' => $drivers));
     } elseif ($count === 1) {
         $driver = $drivers[0];
     } else {
         $driver = new BlackHole();
     }
     return $driver;
 }
예제 #3
0
 /**
  * Loads the composite driver from constants
  * @param $level
  * @return \Stash\Interfaces\DriverInterface
  */
 protected function loadConfig($level)
 {
     $drivers = array();
     $driversBuild = array();
     $constants = array_keys(get_defined_constants());
     foreach ($constants as $constant) {
         if (preg_match('/^CACHE_' . strtoupper($level) . '(?:_([0-9]+))?_DRIVER$/', $constant, $matches) === 1) {
             if (isset($matches[1])) {
                 $index = $matches[1];
             } else {
                 $index = 0;
             }
             $driversBuild[$index]['driver'] = constant($constant);
         } elseif (preg_match('/^CACHE_' . strtoupper($level) . '(?:_([0-9]+))?_OPTION_(.*)$/', $constant, $matches) === 1) {
             if (isset($matches[1])) {
                 $index = $matches[1];
             } else {
                 $index = 0;
             }
             $options = explode('__', $matches[2]);
             if (!isset($driversBuild[$index])) {
                 $driversBuild[$index] = array();
             }
             if (!isset($driversBuild[$index]['options'])) {
                 $driversBuild[$index]['options'] = array();
             }
             $optionsBuild =& $driversBuild[$index]['options'];
             for ($i = 0; $i < count($options); $i++) {
                 if (!isset($optionsBuild[$options[$i]])) {
                     $optionsBuild[$options[$i]] = array();
                 }
                 $optionsBuild =& $optionsBuild[$options[$i]];
             }
             $optionsBuild = constant($constant);
         }
     }
     ksort($driversBuild);
     foreach ($driversBuild as $driverBuild) {
         $tempDriver = new $driverBuild['driver']();
         if (isset($driverBuild['options'])) {
             $tempDriver->setOptions($driverBuild['options']);
         }
         $drivers[] = $tempDriver;
     }
     $numDrivers = count($drivers);
     if ($numDrivers > 1) {
         $driver = new Composite();
         $driver->setOptions(array('drivers' => $drivers));
     } elseif ($numDrivers === 1) {
         $driver = $drivers[0];
     } else {
         $driver = new BlackHole();
     }
     return $driver;
 }
예제 #4
0
파일: CompositeTest.php 프로젝트: n8b/VMN
 public function testIsPersistent()
 {
     $fileDriver = new FileSystem();
     $ephemeralDriver = new Ephemeral();
     $drivers = array($fileDriver, $ephemeralDriver);
     $driver = new Composite(array('drivers' => $drivers));
     $this->assertTrue($driver->isPersistent());
     $drivers = array($ephemeralDriver, $fileDriver);
     $driver = new Composite(array('drivers' => $drivers));
     $this->assertTrue($driver->isPersistent());
     $drivers = array($fileDriver, $fileDriver);
     $driver = new Composite(array('drivers' => $drivers));
     $this->assertTrue($driver->isPersistent());
     $drivers = array($ephemeralDriver, $ephemeralDriver);
     $driver = new Composite(array('drivers' => $drivers));
     $this->assertFalse($driver->isPersistent());
 }
예제 #5
0
 /**
  * Производит конфигурирование кэшера
  *
  * @param array $config Опции конфигурации
  */
 private function configureCacher(array $config = [])
 {
     if (!array_key_exists('cacher', $config)) {
         return;
     }
     $cacherConfig = $config['cacher'];
     if (is_object($cacherConfig)) {
         $this->cacher = $cacherConfig;
     }
     if (is_array($cacherConfig)) {
         if (array_key_exists('drivers', $cacherConfig)) {
             $drivers = [];
             foreach ($cacherConfig['drivers'] as $driverConfig) {
                 /** @var DriverInterface $driver */
                 $driver = new $driverConfig['class']();
                 if (array_key_exists('options', $driverConfig)) {
                     $driver->setOptions($driverConfig['options']);
                 }
                 $drivers[] = $driver;
             }
             $driver = new Composite();
             $driver->setOptions(['drivers' => $drivers]);
             $this->cacher->setDriver($driver);
         }
         if (array_key_exists('expires', $cacherConfig) && is_array($cacherConfig['expires'])) {
             $this->cacherExpires = $cacherConfig['expires'];
         }
     }
 }