/** * Determine system TMP directory and detect if we have read access * @return string * @throws \Micro\Cache\Exception if unable to determine directory */ public function getTmpDir() { $tmpdir = array(); foreach (array($_ENV, $_SERVER) as $tab) { foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) { if (isset($tab[$key]) && is_string($tab[$key])) { if ($key == 'windir' or $key == 'SystemRoot') { $dir = realpath($tab[$key] . '\\temp'); } else { $dir = realpath($tab[$key]); } if ($this->_isGoodTmpDir($dir)) { return $dir; } } } } $upload = ini_get('upload_tmp_dir'); if ($upload) { $dir = realpath($upload); if ($this->_isGoodTmpDir($dir)) { return $dir; } } if (function_exists('sys_get_temp_dir')) { $dir = sys_get_temp_dir(); if ($this->_isGoodTmpDir($dir)) { return $dir; } } // Attemp to detect by creating a temporary file $tempFile = tempnam(md5(uniqid(rand(), TRUE)), ''); if ($tempFile) { $dir = realpath(dirname($tempFile)); unlink($tempFile); if ($this->_isGoodTmpDir($dir)) { return $dir; } } if ($this->_isGoodTmpDir('/tmp')) { return '/tmp'; } if ($this->_isGoodTmpDir('\\temp')) { return '\\temp'; } Cache::throwException('Could not determine temp directory, please specify a cache_dir manually'); }
/** * @return \Micro\Application\Application */ public function registerDefaultServices() { if (!isset($this['request'])) { $this['request'] = function () { return new Http\Request(); }; } if (!isset($this['response'])) { $this['response'] = function () { return new Http\Response\HtmlResponse(); }; } if (!isset($this['event'])) { $this['event'] = function () { return new Event\Manager(); }; } if (!isset($this['exception.handler'])) { $this['exception.handler'] = function ($app) { return $app; }; } if (!isset($this['exception.handler.fallback'])) { $this['exception.handler.fallback'] = function ($app) { return $app; }; } if (!isset($this['acl'])) { $this['acl'] = function ($app) { if ($app->get('config')->get('acl.enabled', 1)) { return new Acl(); } return \null; }; } if (!isset($this['caches'])) { $this['caches'] = function ($app) { $adapters = $app['config']->get('cache.adapters', []); $caches = []; foreach ($adapters as $adapter => $config) { $caches[$adapter] = Cache::factory($config['frontend']['adapter'], $config['backend']['adapter'], $config['frontend']['options'], $config['backend']['options']); } return $caches; }; } if (!isset($this['cache'])) { $this['cache'] = function ($app) { $adapters = $app->get('caches'); $default = (string) $app['config']->get('cache.default'); return isset($adapters[$default]) ? $adapters[$default] : \null; }; } /** * Create router with routes */ if (!isset($this['router'])) { $this['router'] = function ($app) { return new Router($app['request']); }; } /** * Create default db adapter */ if (!isset($this['db'])) { $this['db'] = function ($app) { $default = $app['config']->get('db.default'); $adapters = $app['config']->get('db.adapters', []); if (!isset($adapters[$default])) { return \null; } $db = Database::factory($adapters[$default]['adapter'], $adapters[$default]); TableAbstract::setDefaultAdapter($db); TableAbstract::setDefaultMetadataCache($app['cache']); return $db; }; } /** * Create default translator */ if (!isset($this['translator'])) { $this['translator'] = function ($app) { return new Translator(); }; } /** * Register session config */ $sessionConfig = $this['config']->get('session', []); if (!empty($sessionConfig)) { Session::register($sessionConfig); } CoreLog::register(); CoreException::register(); return $this; }
/** * Validate a tags array (security, reliable filenames, reserved prefixes...) * * Throw an exception if a problem is found * * @param array $tags Array of tags * @throws \Micro\Cache\Exception * @return void */ protected static function _validateTagsArray($tags) { if (!is_array($tags)) { Cache::throwException('Invalid tags array : must be an array'); } foreach ($tags as $tag) { self::_validateIdOrTag($tag); } reset($tags); }
/** * Make a control key with the string containing datas * * @param string $data Data * @param string $controlType Type of control 'md5', 'crc32' or 'strlen' * @throws \Micro\Cache\Exception * @return string Control key */ protected function _hash($data, $controlType) { switch ($controlType) { case 'md5': return md5($data); case 'crc32': return crc32($data); case 'strlen': return strlen($data); case 'adler32': return hash('adler32', $data); default: Cache::throwException("Incorrect hash function : {$controlType}"); } }
public function registerCacheBinder() { $config = $this->container->get('config'); $this->container->set('caches', function () use($config) { $adapters = $config->get('cache.adapters', []); $caches = []; foreach ($adapters as $adapter => $adapterConfig) { $caches[$adapter] = Cache::factory($adapterConfig['frontend']['adapter'], $adapterConfig['backend']['adapter'], $adapterConfig['frontend']['options'], $adapterConfig['backend']['options']); } return $caches; }, \false); $this->container->set('cache', function ($container) use($config) { $adapters = $container->get('caches'); $default = (string) $config->get('cache.default'); return isset($adapters[$default]) ? $adapters[$default] : \null; }, \false); }