Example #1
0
 /**
  * Given a list of cache types and options, creates a CompositeDrivers wrapping the specified drivers.
  *
  * @param $types
  * @param $options
  * @throws \RuntimeException
  * @return DriverInterface
  */
 public static function createDriver($types, $options)
 {
     $drivers = DriverList::getAvailableDrivers();
     $h = array();
     foreach ($types as $type) {
         if (!isset($drivers[$type])) {
             $allDrivers = DriverList::getAllDrivers();
             if (isset($allDrivers[$type])) {
                 throw new \RuntimeException('Driver currently unavailable.');
             } else {
                 throw new \RuntimeException('Driver does not exist.');
             }
         }
         $class = $drivers[$type];
         if ($type === 'Memcache' && isset($options[$type])) {
             // Fix servers spec since underlying drivers expect plain arrays, not hashes.
             $servers = array();
             foreach ($options[$type]['servers'] as $serverSpec) {
                 $servers[] = array($serverSpec['server'], $serverSpec['port'], isset($serverSpec['weight']) ? $serverSpec['weight'] : null);
             }
             $options[$type]['servers'] = $servers;
         }
         $opts = isset($options[$type]) ? $options[$type] : array();
         $driver = new $class();
         $driver->setOptions($opts);
         $h[] = $driver;
     }
     if (count($h) == 1) {
         return reset($h);
     }
     $class = $drivers['Composite'];
     $driver = new $class();
     $driver->setOptions(array('drivers' => $h));
     return $driver;
 }
 /**
  * Creates the driver configuration tree.
  *
  * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
  */
 public function getCachesNode()
 {
     $drivers = array_keys(DriverList::getAvailableDrivers());
     $treeBuilder = new TreeBuilder();
     $node = $treeBuilder->root('caches');
     $childNode = $node->fixXmlConfig('handler')->fixXmlConfig('driver')->beforeNormalization()->ifTrue(function ($v) {
         return is_array($v) && array_key_exists('handlers', $v);
     })->then(function ($v) {
         return Configuration::normalizeHandlerToDriverConfig($v);
     })->end()->requiresAtLeastOneElement()->useAttributeAsKey('name')->prototype('array')->children()->arrayNode('drivers')->requiresAtLeastOneElement()->defaultValue(array('FileSystem'))->prototype('scalar')->validate()->ifNotInArray($drivers)->thenInvalid('A driver of that name is not registered.')->end()->end()->end()->booleanNode('registerDoctrineAdapter')->defaultFalse()->end()->booleanNode('registerSessionHandler')->defaultFalse()->end()->booleanNode('inMemory')->defaultTrue()->end()->scalarNode('logger')->defaultNull()->end();
     foreach ($drivers as $driver) {
         if ($driver !== 'Composite') {
             $this->addDriverSettings($driver, $childNode);
         }
     }
     $childNode->end();
     return $node;
 }
 /**
  * {@inheritDoc}
  */
 public function collect(Request $request, Response $response, \Exception $exception = null)
 {
     $info = array('calls' => 0, 'hits' => 0);
     foreach ($this->trackers as $tracker) {
         $name = $tracker->getName();
         $calls = $tracker->getCalls();
         $hits = $tracker->getHits();
         $info['calls'] += $calls;
         $info['hits'] += $hits;
         $info['caches'][$name]['options'] = $this->cacheOptions[$name];
         $info['caches'][$name]['queries'] = $tracker->getQueries();
         $info['caches'][$name]['calls'] = $calls;
         $info['caches'][$name]['hits'] = $hits;
     }
     $drivers = DriverList::getAvailableDrivers();
     foreach ($drivers as $name => $class) {
         if (!in_array($name, array('Ephemeral', 'Composite'))) {
             $info['availableDrivers'][] = $name;
         }
     }
     $info['default'] = $this->defaultCache;
     $this->data = $info;
 }
 public function testGetDrivers()
 {
     $availableDrivers = DriverList::getAvailableDrivers();
     $getDrivers = DriverList::getDrivers();
     $this->assertEquals($availableDrivers, $getDrivers, 'getDrivers is an alias for getAvailableDrivers');
 }
 public function testGetAvailableDrivers()
 {
     $collector = $this->getPrimedCollector();
     $drivers = $collector->getDrivers();
     $this->assertInternalType('array', $drivers, 'getDrivers returns an array');
     $systemDrivers = array_keys(DriverList::getAvailableDrivers());
     $this->assertFalse(in_array('Ephemeral', $drivers), 'getDrivers does not include Ephemeral driver');
     $this->assertFalse(in_array('Composite', $drivers), 'getDrivers does not include Composite driver');
     $this->assertTrue(in_array('FileSystem', $drivers), 'getDrivers does always include FileSystem driver');
     //var_dump($drivers, $systemDrivers);exit();
     foreach ($drivers as $driver) {
         $this->assertTrue(in_array($driver, $systemDrivers), 'getDrivers returns only registered drivers- Unregistered: ' . $driver);
     }
 }
 public function testGetDrivers()
 {
     $service = $this->getCacheService();
     $this->assertEquals(DriverList::getAvailableDrivers(), $service->getDrivers(), 'Service available drivers');
 }
 /**
  * Returns the current list of drivers that the system is able to use.
  *
  * @return array
  */
 public function getDrivers()
 {
     return DriverList::getAvailableDrivers();
 }
 /**
  * Registers services on the given container.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  *
  * @param Container $container A container instance.
  * @return void
  */
 public function register(Container $container)
 {
     /**
      * @param Container $container A container instance.
      * @return CacheConfig
      */
     $container['cache/config'] = function (Container $container) {
         $appConfig = $container['config'];
         $cacheConfig = new CacheConfig($appConfig->get('cache'));
         return $cacheConfig;
     };
     $container['cache/available-drivers'] = DriverList::getAvailableDrivers();
     /**
      * @param Container $container A container instance.
      * @return Container The Collection of cache drivers, in a Container.
      */
     $container['cache/drivers'] = function (Container $container) {
         $cacheConfig = $container['cache/config'];
         $drivers = new Container();
         $parentContainer = $container;
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\Apc
          */
         $drivers['apc'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             if (!isset($drivers['Apc'])) {
                 // Apc is not available on system
                 return null;
             }
             return new $drivers['Apc']();
         };
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\Sqlite
          */
         $drivers['db'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             if (!isset($drivers['SQLite'])) {
                 // SQLite is not available on system
                 return null;
             }
             return new $drivers['SQLite']();
         };
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\FileSystem
          */
         $drivers['file'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             return new $drivers['FileSystem']();
         };
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\Memcache
          */
         $drivers['memcache'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             if (!isset($drivers['Memcache'])) {
                 // Memcache is not available on system
                 return null;
             }
             $cacheConfig = $parentContainer['cache/config'];
             $driverOptions = ['servers' => []];
             if (isset($cacheConfig['servers'])) {
                 $servers = [];
                 foreach ($cacheConfig['servers'] as $server) {
                     $servers[] = array_values($server);
                 }
                 $driverOptions['servers'] = $servers;
             } else {
                 // Default Memcache options: locahost:11211
                 $driverOptions['servers'][] = ['127.0.0.1', 11211];
             }
             return new $drivers['Memcache']($driverOptions);
         };
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\Ephemeral
          */
         $drivers['memory'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             return new $drivers['Ephemeral']();
         };
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\BlackHole
          */
         $drivers['noop'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             return new $drivers['BlackHole']();
         };
         /**
          * @param Container $container A container instance.
          * @return \Stash\Driver\Redis
          */
         $drivers['redis'] = function (Container $container) use($parentContainer) {
             $drivers = $parentContainer['cache/available-drivers'];
             if (!isset($drivers['Redis'])) {
                 // Redis is not available on system
                 return null;
             }
             return new $drivers['Redis']();
         };
         return $drivers;
     };
     /**
      * @param Container $container A container instance.
      * @return Container The Collection of DatabaseSourceConfig, in a Container.
      */
     $container['cache/driver'] = function (Container $container) {
         $cacheConfig = $container['cache/config'];
         $types = $cacheConfig->get('types');
         foreach ($types as $type) {
             if (isset($container['cache/drivers'][$type])) {
                 return $container['cache/drivers'][$type];
             }
         }
         // If no working drivers were available, fallback to an Ephemeral (memory) driver.
         return $container['cache/drivers']['memory'];
     };
     /**
      * The cache pool, using Stash.
      *
      * @param Container $container A container instance.
      * @return \Stash\Pool
      */
     $container['cache'] = function (Container $container) {
         $cacheConfig = $container['cache/config'];
         $driver = $container['cache/driver'];
         $pool = new Pool($driver);
         $pool->setLogger($container['logger']);
         // Ensure an alphanumeric namespace (prefix)
         $namespace = preg_replace('/[^A-Za-z0-9 ]/', '', $cacheConfig['prefix']);
         $pool->setNamespace($namespace);
         return $pool;
     };
 }
Example #9
0
 /**
  * Create a default configuration file, used in installation
  *
  * When using Windows NTFS, PHP has a maximum path length of 260 bytes. Each key level in a
  * Stash hierarchical key corresponds to a directory, and is normalized as an md5 hash. Also,
  * Stash uses 2 levels for its own integrity ad locking mechanisms. The full key length used
  * in XoopCore can reach 202 charachters.
  *
  * If the combined path length would excede 260, we will try and switch to SQLite driver if
  * it is available when running on a Windows system.
  *
  * @return void
  */
 public static function createDefaultConfig()
 {
     $configFile = \XoopsBaseConfig::get('var-path') . '/configs/cache.php';
     if (file_exists($configFile)) {
         return;
     }
     $defaults = self::getDefaults();
     if (false !== stripos(PHP_OS, 'WIN')) {
         $pathLen = strlen($defaults['default']['options']['path']);
         if (260 <= $pathLen + 202) {
             // try alternative driver as filesystem has max path length issues on Windows
             if (array_key_exists("SQLite", \Stash\DriverList::getAvailableDrivers())) {
                 $defaults['default']['driver'] = 'SQLite';
                 unset($defaults['default']['options']['dirSplit']);
                 @mkdir($defaults['default']['options']['path']);
             } else {
                 trigger_error("Manual cache configuration required.");
             }
         }
     }
     Yaml::saveWrapped($defaults, $configFile);
 }
 public function setUp()
 {
     $this->drivers = DriverList::getAvailableDrivers();
 }
Example #11
0
 public function testRegisterDriver()
 {
     DriverList::registerDriver('Array', 'Stash\\Driver\\Ephemeral');
     $drivers = DriverList::getAvailableDrivers();
     $this->assertArrayHasKey('Array', $drivers, 'getDrivers returns Array driver');
 }