From 758203b6d3f6afd88be9438f4d1c3014a3f0ab0f Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 20 Mar 2015 14:59:11 +0000 Subject: [PATCH] Encapsulate cache pool instantiation in CacheFactory --- src/mibew/configs/default_config.yml | 13 ++ src/mibew/index.php | 17 +- .../libs/classes/Mibew/Cache/CacheFactory.php | 152 ++++++++++++++++++ src/mibew/libs/common/configurations.php | 10 +- 4 files changed, 182 insertions(+), 10 deletions(-) create mode 100644 src/mibew/libs/classes/Mibew/Cache/CacheFactory.php diff --git a/src/mibew/configs/default_config.yml b/src/mibew/configs/default_config.yml index 0a8aacf2..08f9b932 100644 --- a/src/mibew/configs/default_config.yml +++ b/src/mibew/configs/default_config.yml @@ -39,6 +39,19 @@ mailer: # "tls". encryption: false +# Cache subsystem +cache: + # This value determines where the cached data will be stored. Possible + # values are "none" and "file_system". + # + # If "none" is used the caching will be disabled. This option should be used + # only if none of the other options works. + # + # If "file_system" is used the cached data will be stored in files within + # the "cache/" directory. Notice, that the file system storage does not work + # on NFS systems bucause exclusive file locks are not supported where. + storage: file_system + # Locales ## Native name will be used in this locale home_locale: en diff --git a/src/mibew/index.php b/src/mibew/index.php index bb74b061..355a76a9 100644 --- a/src/mibew/index.php +++ b/src/mibew/index.php @@ -22,6 +22,7 @@ require_once(dirname(__FILE__) . '/libs/init.php'); use Mibew\Application; use Mibew\Authentication\AuthenticationManager; +use Mibew\Cache\CacheFactory; use Mibew\Mail\MailerFactory; use Mibew\Routing\Router; use Mibew\Routing\Loader\CacheLoader; @@ -31,14 +32,17 @@ use Symfony\Component\Config\Loader\LoaderResolver; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Loader\YamlFileLoader; -// Use custom cache -$cache_driver = new \Stash\Driver\FileSystem(); -$cache_driver->setOptions(array('path' => MIBEW_FS_ROOT . '/cache/stash')); -$cache = new \Stash\Pool($cache_driver); +$configs = load_system_configs(); + +// Prepare the cache +$cache_factory = new CacheFactory($configs['cache']); +// For now directory for cache files cannot be changed via the configs file. +// TODO: Evaluate possibility of using custom cache directory. +$cache_factory->setOption('path', MIBEW_FS_ROOT . '/cache/stash'); // The main route loader which loads nothig but works as a cache proxy for other // loaders. -$route_loader = new CacheLoader($cache); +$route_loader = new CacheLoader($cache_factory->getCache()); // Real loaders are attached via the resolver. $loader_resolver = new LoaderResolver(array( $route_loader, @@ -49,10 +53,9 @@ $loader_resolver = new LoaderResolver(array( $router = new Router($route_loader, 'configs/routing.yml'); $application = new Application($router, new AuthenticationManager()); -$application->setCache($cache); +$application->setCache($cache_factory->getCache()); // Use custom config-dependent mailer factory -$configs = load_system_configs(); $application->setMailerFactory(new MailerFactory($configs['mailer'])); // Process request diff --git a/src/mibew/libs/classes/Mibew/Cache/CacheFactory.php b/src/mibew/libs/classes/Mibew/Cache/CacheFactory.php new file mode 100644 index 00000000..cb465ba3 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Cache/CacheFactory.php @@ -0,0 +1,152 @@ +setOptions($options); + } + + /** + * Gets factory's option. + * + * @param string $name Name of the option to retrieve. + * @throws \InvalidArgumentException If the option is unknown. + */ + public function getOption($name) + { + if (!isset($this->options[$name])) { + throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $name)); + } + + return $this->options[$name]; + } + + /** + * Sets factory's option. + * + * @param string $name Name of the option to set. + * @param string $value New value. + * @throws \InvalidArgumentException If the option is unknown. + */ + public function setOption($name, $value) + { + if (!isset($this->options[$name])) { + throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $name)); + } + + $this->options[$name] = $value; + + // Cached instance of cache is not valid any more. New one should be + // created. + $this->cache = null; + } + + /** + * Sets factory's options. + * + * @param array $options Associative array of options. + * @throws \InvalidArgumentException If specified array has unknow options. + */ + public function setOptions($options) + { + $defaults = array( + 'storage' => 'file_system', + 'path' => '/tmp', + ); + + // Make sure all passed options are known + $unknown_options = array_diff(array_keys($options), array_keys($defaults)); + if (count($unknown_options) != 0) { + throw new \InvalidArgumentException(sprintf( + 'These options are unknown: %s', + implode(', ', $unknown_options) + )); + } + + if (empty($this->options)) { + // The options are set for the first time. + $this->options = $options + $defaults; + } else { + // Update only specified options. + $this->options = $options + $this->options; + } + + // Cached instance of cache is not valid any more. New one should be + // created. + $this->cache = null; + } + + /** + * Builds cache pool instance. + * + * @return PoolInterface An instance of cache pool. + * @throws \RuntimeException If at least one of factory's options is + * invalid. + */ + public function getCache() + { + if (is_null($this->cache)) { + $storage = $this->getOption('storage'); + if ($storage === 'none') { + $driver = new EphemeralDriver(); + } elseif ($storage === 'file_system') { + $driver = new FileSystemDriver(); + $driver->setOptions(array('path' => $this->getOption('path'))); + } else { + throw new \RuntimeException(sprintf( + 'Wrong value of "storage" option: "%s"', + $storage + )); + } + + $this->cache = new CachePool($driver); + } + + return $this->cache; + } +} diff --git a/src/mibew/libs/common/configurations.php b/src/mibew/libs/common/configurations.php index e812d671..94b50a71 100644 --- a/src/mibew/libs/common/configurations.php +++ b/src/mibew/libs/common/configurations.php @@ -33,9 +33,13 @@ function load_system_configs() if (is_null($configs)) { $parser = new YamlParser(); $configs = $parser->parse(file_get_contents(MIBEW_FS_ROOT . '/configs/config.yml')); - // Mailer configs are not necessary and can be omited but the section - // must exist anyway. - $configs += array('mailer' => array()); + $configs += array( + // Mailer configs are not necessary and can be omited but the + // section must exist anyway. + 'mailer' => array(), + // Cache section must extst too. + 'cache' => array(), + ); } return $configs;