123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- class sfConfigCache
- {
- protected
- $configuration = null,
- $handlers = array(),
- $userHandlers = array();
-
- public function __construct(sfApplicationConfiguration $configuration)
- {
- $this->configuration = $configuration;
- }
-
- protected function callHandler($handler, $configs, $cache)
- {
- if (count($this->handlers) == 0)
- {
-
- $this->loadConfigHandlers();
- }
- if (count($this->userHandlers) != 0)
- {
-
- $this->mergeUserConfigHandlers();
- }
-
- $handlerKey = null;
- $handler = str_replace(DIRECTORY_SEPARATOR, '/', $handler);
-
- $basename = basename($handler);
- if (isset($this->handlers[$handler]))
- {
-
- $handlerKey = $handler;
- }
- else if (isset($this->handlers[$basename]))
- {
-
- $handlerKey = $basename;
- }
- else
- {
-
- foreach (array_keys($this->handlers) as $key)
- {
-
- $pattern = strtr($key, array('.' => '\.', '*' => '.*?'));
-
- if (preg_match('#'.$pattern.'$#', $handler))
- {
- $handlerKey = $key;
- break;
- }
- }
- }
- if (!$handlerKey)
- {
-
- throw new sfConfigurationException(sprintf('Configuration file "%s" does not have a registered handler.', implode(', ', $configs)));
- }
-
- $data = $this->getHandler($handlerKey)->execute($configs);
- $this->writeCacheFile($handler, $cache, $data);
- }
-
- protected function getHandler($name)
- {
- if (is_array($this->handlers[$name]))
- {
- $class = $this->handlers[$name][0];
- $this->handlers[$name] = new $class($this->handlers[$name][1]);
- }
- return $this->handlers[$name];
- }
-
- public function checkConfig($configPath, $optional = false)
- {
- if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
- {
- $timer = sfTimerManager::getTimer('Configuration');
- }
-
- $cache = $this->getCacheName($configPath);
- if (!sfConfig::get('sf_debug') && !sfConfig::get('sf_test') && is_readable($cache))
- {
- return $cache;
- }
- if (!sfToolkit::isPathAbsolute($configPath))
- {
- $files = $this->configuration->getConfigPaths($configPath);
- }
- else
- {
- $files = is_readable($configPath) ? array($configPath) : array();
- }
- if (!isset($files[0]))
- {
- if ($optional)
- {
- return null;
- }
-
- throw new sfConfigurationException(sprintf('Configuration "%s" does not exist or is unreadable.', $configPath));
- }
-
- $mtime = 0;
- foreach ($files as $file)
- {
- if (filemtime($file) > $mtime)
- {
- $mtime = filemtime($file);
- }
- }
- if (!is_readable($cache) || $mtime > filemtime($cache))
- {
-
- $this->callHandler($configPath, $files, $cache);
- }
- if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
- {
- $timer->addTime();
- }
- return $cache;
- }
-
- public function clear()
- {
- sfToolkit::clearDirectory(sfConfig::get('sf_config_cache_dir'));
- }
-
- public function getCacheName($config)
- {
- if (strlen($config) > 3 && ctype_alpha($config[0]) && $config[1] == ':' && ($config[2] == '\\' || $config[2] == '/'))
- {
-
- $config = substr($config, 3);
- }
-
- $config = str_replace(array('\\', '/', ' '), '_', $config);
- $config .= '.php';
- return sfConfig::get('sf_config_cache_dir').'/'.$config;
- }
-
- public function import($config, $once = true, $optional = false)
- {
- $cache = $this->checkConfig($config, $optional);
- if ($optional && !$cache)
- {
- return;
- }
-
- if ($once)
- {
- include_once($cache);
- }
- else
- {
- include($cache);
- }
- }
-
- protected function loadConfigHandlers()
- {
-
- $this->handlers['config_handlers.yml'] = new sfRootConfigHandler();
-
- require $this->checkConfig('config/config_handlers.yml');
-
-
- if (!is_readable($sf_app_modules_dir = sfConfig::get('sf_app_modules_dir')))
- {
- return;
- }
-
- $ignore = array('.', '..', 'CVS', '.svn');
-
- $fp = opendir($sf_app_modules_dir);
-
- while (($directory = readdir($fp)) !== false)
- {
- if (in_array($directory, $ignore))
- {
- continue;
- }
- $configPath = $sf_app_modules_dir.'/'.$directory.'/config/config_handlers.yml';
- if (is_readable($configPath))
- {
-
- $params = array('module_level' => true, 'module_name' => $directory);
- $this->handlers['config_handlers.yml']->initialize($params);
-
-
- $configPath = 'modules/'.$directory.'/config/config_handlers.yml';
- require $this->checkConfig($configPath);
- }
- }
-
- closedir($fp);
- }
-
- protected function writeCacheFile($config, $cache, $data)
- {
- $current_umask = umask(0000);
- if (!is_dir(dirname($cache)))
- {
- if (false === @mkdir(dirname($cache), 0777, true))
- {
- throw new sfCacheException(sprintf('Failed to make cache directory "%s" while generating cache for configuration file "%s".', dirname($cache), $config));
- }
- }
- $tmpFile = $cache.'.'.getmypid();
- if (!$fp = @fopen($tmpFile, 'wb'))
- {
- throw new sfCacheException(sprintf('Failed to write cache file "%s" generated from configuration file "%s".', $tmpFile, $config));
- }
- @fwrite($fp, $data);
- @fclose($fp);
- chmod($tmpFile, 0666);
- @unlink($cache);
- rename($tmpFile, $cache);
- umask($current_umask);
- }
-
- public function registerConfigHandler($handler, $class, $params = array())
- {
- $this->userHandlers[$handler] = new $class($params);
- }
-
- protected function mergeUserConfigHandlers()
- {
-
- $this->handlers = array_merge($this->handlers, $this->userHandlers);
- $this->userHandlers = array();
- }
- }
|