public function onStatusChange($new_status) { if ($new_status == 'disabled') { $cache = Environment::getCache('modules.GalleryModule'); $cache->clean(array(Cache::ALL => TRUE)); } }
/** * merges config files of each module imported via config.ini[modules] to one file and loads it * considering current environment [dev, production, ...] - separate config file for each * uses Nette/Cache for invalidation when one (or more) of config files changed * * @param string|null filepath * @return Config */ public static function load($baseConfigFile = null) { if ($baseConfigFile === null) { $baseConfigFile = Environment::expand(Environment::getConfigurator()->defaultConfigFile); } $envName = Environment::getName(); Environment::setVariable('tempDir', VAR_DIR . '/cache'); $cache = Environment::getCache('config'); $key = "config[{$envName}]"; if (!isset($cache[$key])) { // najviac casu zabera load, tak az tu, ked ho je treba $appConfig = Environment::loadConfig($baseConfigFile); $configs = array(Config::fromFile($baseConfigFile, $envName)->toArray()); $configPaths = array($baseConfigFile); foreach ($appConfig->modules as $c) { $configPaths[] = $path = MODULES_DIR . "/{$c}Module/config.ini"; if (file_exists($path)) { $configs[] = Config::fromFile($path, $envName)->toArray(); } } $arrayConfig = call_user_func_array('array_merge_recursive', $configs); $cache->save($key, $arrayConfig, array('files' => $configPaths)); } return Environment::loadConfig(new Config($cache[$key])); }
public static function invalidateCache($type = NULL) { if (is_null($type)) { $type = static::$cacheKey; } $cache = Environment::getCache("Seolinks." . $type); unset($cache['data']); }
/** * invalidate cache by type (and section) and/or tags * * @param string|null * @param string|null * @param string * * @return void */ public static function invalidate($type = null, $tags = null, $section = 'data') { if ($type) { $cache = Environment::getCache($type); unset($cache[$section]); } // expire by given tags if ($tags) { $cache = Environment::getCache(); $cache->clean(array('tags' => array($tags))); } }
public function setOwnerId($id) { if (defined('ACL_CACHING') and ACL_CACHING) { $this->cache = Environment::getCache(); $key = static::ID . '-' . $id; if (!isset($this->cache[$key])) { $this->cache->save($key, static::getDependencies($id)); } $this->ownerId = $this->cache[$key]; } else { $this->ownerId = static::getDependencies($id); } }
/** * Renders template to output. * @return void */ public function render() { $cache = Environment::getCache('StringTemplate'); $key = md5($this->content); $content = $cache[$key]; if ($content === NULL) { // not cached if (!$this->getFilters()) { $this->onPrepareFilters($this); } $cache[$key] = $content = $this->compile($this->content); } $this->__set('template', $this); /*Nette\Loaders\*/ LimitedScope::evaluate($content, $this->getParams()); }
public function run() { $this->cache = Environment::getCache('Application'); $modules = Environment::getConfig('modules'); if (!empty($modules)) { foreach ($modules as $module) { $this->loadModule($module); } } $this->setupRouter(); // $this->setupHooks(); // Requires database connection $this->onRequest[] = array($this, 'setupPermission'); // ... // Run the application! parent::run(); $this->cache->release(); }
/** * Renders HTML code for custom panel. * @return void */ function getPanel() { //Debug::timer('presenter-tree'); $cache = Environment::getCache('debug/panels/PresenterTree'); if (isset($cache['tree'])) { $tree = $cache['tree']; } else { Environment::enterCriticalSection('debug/panels/PresenterTree'); $generated = $this->generate(); $tree = $cache->save('tree', $generated['tree'], array('files' => $generated['depends'])); Environment::leaveCriticalSection('debug/panels/PresenterTree'); } ob_start(); $template = new Template(dirname(__FILE__) . '/bar.presentertree.panel.phtml'); $template->tree = $tree; $template->render(); //Debug::fireLog('presenter-tree render time (ms): ' . round(1000 * Debug::timer('presenter-tree', TRUE), 2)); return $cache['output'] = ob_get_clean(); }
/** * Autoloader factory. * @return Acl */ public static function factory() { $expire = 24 * 60 * 60; // 1 den $driver = Environment::getConfig('database')->driver; try { $key = 'acl'; $cache = Environment::getCache('application/acl'); if (isset($cache[$key])) { // serving from cache return $cache[$key]; } else { $acl = new self(); $cache->save($key, $acl, array('expire' => $expire, 'tags' => array('system', 'acl'))); return $acl; } } catch (Exception $e) { $acl = new self(); //$cache->save($key, $acl, array('expire' => $expire)); return $acl; } }
public function startup() { parent::startup(); if (!isset($this->lang)) { $this->lang = $this->getHttpRequest()->detectLanguage($this->langs); if ($this->lang == null) { $this->lang = 'en'; } $this->canonicalize(); } $this->translator = Environment::getService('Mokuji\\Translator\\Admin'); $this->translator->lang = $this->lang; //$this->translator = new Admin_Translator($this->lang); $cache = Environment::getCache('langs'); $langs = $cache->offsetGet('langs'); if ($langs == NULL) { $this->langs = $this->translator->getSupportedLangs(); //$this->model('lang')->getAll(); $cache->save('langs', $this->langs); } else { $this->langs = $langs; } $this->refreshConfig(); }
/** * @return Cache */ protected function getCache() { return Environment::getCache('Nette.RobotLoader'); }
public static function changeStatus($module_name, $new_status) { Admin_ModulesModel::changeStatus($module_name, $new_status); $cache = Environment::getCache('modules.' . $module_name . 'Module'); $cache->save('status', $new_status); }
/** * @return Cache */ protected static function getCache() { return Environment::getCache('Nette.Annotations'); }
/** * Getter for Cache instance * Creates instance if called for the first time * Creates MemcachedStorage if extension memcache exists * Otherwise FileStorage Cache is created * Triggers notice if config variable advertisememcached in perform block is not set to false * @return Cache */ public static function getCache() { if (!self::$cache) { $config = Environment::getConfig('perform'); if (extension_loaded('memcache')) { self::$cache = new Cache(new MemcachedStorage($config->memcache_host, $config->memcache_port, $config->cache_prefix)); $namespace = self::$cache; $namespace['test'] = true; if ($namespace['test'] === true) { return self::$cache; } } self::$cache = Environment::getCache($config->cache_prefix); if ($config->advertisememcached) { trigger_error("FileCache enabled, use Memcached if possible. Turn off this warning by setting advertisememcached to false", E_USER_WARNING); } } return self::$cache; }
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tmpDir), RecursiveIteratorIterator::CHILD_FIRST) as $entry) { // delete all files if ($entry->isDir()) { @rmdir($entry); } else { @unlink($entry); } } Environment::setVariable('tempDir', $tmpDir); $key = ''; $value = array(); for ($i = 0; $i < 32; $i++) { $key .= chr($i); $value[] = chr($i) . chr(255 - $i); } $cache = Environment::getCache(); echo "Is cached?\n"; Debug::dump(isset($cache[$key])); echo "Cache content:\n"; Debug::dump($cache[$key]); echo "Writing cache...\n"; $cache[$key] = $value; $cache->release(); echo "Is cached?\n"; Debug::dump(isset($cache[$key])); echo "Is cache ok?\n"; Debug::dump($cache[$key] === $value); echo "Removing from cache using unset()...\n"; unset($cache[$key]); $cache->release(); echo "Is cached?\n";
function getCache() { if (null === $this->cache) { $this->cache = Environment::getCache('google-translator'); } return $this->cache; }
/** * @return Cache */ protected function getCache() { return Environment::getCache('Mokuji.Loader'); }
/** * @return Cache */ protected static function getCache() { return Environment::getCache('Nette.Template.Curly'); }
/** * Get cache * @return Cache */ public static function getCache() { return Environment::getCache(__CLASS__); }
public function findAllStrings() { $files = array(); foreach (self::$dirs as $dir) { $path = realpath(APP_DIR . "\\{$dir}\\"); $files = array_merge($files, $this->getFiles($path, array('php', 'phtml'))); } // z tohto suoru nechceme sosat $files = array_diff($files, array(__FILE__)); $phrases = array(); $patterns = array("#{_[\"\\']((?:\\\\?+.)*?)[\"\\']#s", "#\\s+(?:\\\$this->translate|_)\\([\\'|\"](.*)[\\'|\"]\\)#Us", "#flashMessage\\([\\'|\"](.*)[\\'|\"][,|\\)|(\\s+\\.\\s+)]#Us"); foreach ($files as $file) { foreach ($patterns as $pattern) { preg_match_all($pattern, file_get_contents($file), $matches); foreach ($matches[1] as $val) { // $phrases[] = $val; $phrases[$val] = substr($file, strlen(WWW_DIR) + 1); } } } $phrasesArray = $phrases; $phrases = array_unique(array_keys($phrases)); if (!isset($this->dictionary)) { $this->buildDictionary(); } $loadedPhrases = array_keys($this->dictionary); $allPhrases = array_unique(array_merge($phrases, $loadedPhrases)); $oldOrForm = array_diff($loadedPhrases, $phrases); $newPhrases = array_diff($phrases, $loadedPhrases); // dump($phrasesArray); // dump($loadedPhrases); // dump($allPhrases); // dump($oldOrForm); // dump($newPhrases); // $oldOrForm // $newPhrases = array_diff_key(1) // dump($newPhrases); // dump(); // insert new phrases in 1 query if (count($newPhrases) > 0) { $files = $this->filterArray($newPhrases, $phrasesArray); // dump($files);die(); $data = array('msg_id' => array_keys($files), 'file' => array_values($files)); dibi::query('INSERT INTO %n %m', self::TABLE, $data); } if (count($oldOrForm) > 0) { foreach ($oldOrForm as $phrase) { dibi::update(self::TABLE, array('oldOrForm' => 1))->where('msg_id = %s', $phrase)->execute(); } } if ($this->debug) { // dump($phrases); echo '<b>'; echo count($allPhrases) . " phrases together<br>"; echo count($files) . " files matched<br>"; echo count($phrases) . " phrases found to be translated<br>"; echo count($newPhrases) . " new phrases found:</b><br>" . join('<br>', $newPhrases); echo '<b>' . count($oldOrForm) . " phrases not found but exist in dictionary => possibly unused or from form:</b><br>" . join('<br>', $oldOrForm); } // aby sa refresol slovnik $cache = Environment::getCache('dictionary'); unset($cache['data']); $this->buildDictionary(); }
public function handleClearCache() { Environment::getCache()->release(); Environment::getCache()->clean(array(Cache::ALL => TRUE)); }
public function handleBrowseFiles($dir, $ext) { $cache = Environment::getCache($this->getName()); $ckey = __FUNCTION__ . $dir . $ext; fd(Environment::getVariables()); if (!isset($cache[$ckey])) { $root = WWW_DIR . '/'; $response = ''; if (file_exists($root . $dir)) { $files = scandir($root . $dir); natcasesort($files); if (count($files) > 2) { $response .= "<ul class=\"jqueryFileTree\" style=\"display: none;\">"; foreach ($files as $file) { if (file_exists($root . $dir . $file) && $file != '.' && $file != '..' && is_dir($root . $dir . $file)) { $response .= "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($dir . $file) . "/\">" . htmlentities($file) . "</a></li>"; } } foreach ($files as $file) { $file_info = pathinfo($file); if (file_exists($root . $dir . $file) && $file != '.' && $file != '..' && !is_dir($root . $dir . $file) && $file_info['extension'] == $ext) { $ext = preg_replace('/^.*\\./', '', $file); $response .= "<li class=\"file ext_{$ext}\"><a href=\"#\" rel=\"" . htmlentities($dir . $file) . "\">" . htmlentities($file) . "</a></li>"; } } $response .= "</ul>"; } } $cache->save($ckey, $response, array('expire' => time() + 60 * 10)); } echo $cache[$ckey]; $this->terminate(); }
/** * Loads global configuration from file and process it. * @param string|Nette\Config\Config file name or Config object * @param bool * @return Nette\Config\Config */ public function loadConfig($file, $useCache) { if ($useCache === NULL) { $useCache = Environment::isLive(); } $cache = $useCache && $this->cacheKey ? Environment::getCache('Nette.Environment') : NULL; $name = Environment::getName(); $cacheKey = Environment::expand($this->cacheKey); if (isset($cache[$cacheKey])) { Environment::swapState($cache[$cacheKey]); $config = Environment::getConfig(); } else { if ($file instanceof Config) { $config = $file; $file = NULL; } else { if ($file === NULL) { $file = $this->defaultConfigFile; } $file = Environment::expand($file); $config = Config::fromFile($file, $name, 0); } // process environment variables if ($config->variable instanceof Config) { foreach ($config->variable as $key => $value) { Environment::setVariable($key, $value); } } if (PATH_SEPARATOR !== ';' && isset($config->set->include_path)) { $config->set->include_path = str_replace(';', PATH_SEPARATOR, $config->set->include_path); } $config->expand(); $config->setReadOnly(); // process services $locator = Environment::getServiceLocator(); if ($config->service instanceof Config) { foreach ($config->service as $key => $value) { $locator->addService($value, strtr($key, '-', '\\')); } } // save cache if ($cache) { $state = Environment::swapState(NULL); $state[0] = $config; // TODO: better! $cache->save($cacheKey, $state, array(Cache::FILES => $file)); } } // check temporary directory - TODO: discuss /* $dir = Environment::getVariable('tempDir'); if ($dir && !(is_dir($dir) && is_writable($dir))) { trigger_error("Temporary directory '$dir' is not writable", E_USER_NOTICE); } */ // process ini settings if ($config->set instanceof Config) { foreach ($config->set as $key => $value) { $key = strtr($key, '-', '.'); if (function_exists('ini_set')) { ini_set($key, $value); } else { switch ($key) { case 'include_path': set_include_path($value); break; case 'iconv.internal_encoding': iconv_set_encoding('internal_encoding', $value); break; case 'mbstring.internal_encoding': mb_internal_encoding($value); break; case 'date.timezone': date_default_timezone_set($value); break; case 'error_reporting': error_reporting($value); break; case 'ignore_user_abort': ignore_user_abort($value); break; case 'max_execution_time': set_time_limit($value); break; default: throw new NotSupportedException('Required function ini_set() is disabled.'); } } } } // define constants if ($config->const instanceof Config) { foreach ($config->const as $key => $value) { define($key, $value); } } // set modes if (isset($config->mode)) { foreach ($config->mode as $mode => $state) { Environment::setMode($mode, $state); } } return $config; }
<h1>Nette\Environment services test</h1> <pre> <?php require_once '../../Nette/loader.php'; /*use Nette\Debug;*/ /*use Nette\Environment;*/ echo "Environment::getHttpResponse\n"; $obj = Environment::getHttpResponse(); Debug::dump($obj->class); echo "Environment::getApplication\n"; $obj = Environment::getApplication(); Debug::dump($obj->class); echo "Environment::getCache(...)\n"; Environment::setVariable('tempDir', __FILE__); $obj = Environment::getCache('my'); Debug::dump($obj->class); /* in PHP 5.3 echo "Environment::getXyz(...)\n"; Environment::setServiceAlias('Nette\Web\IUser', 'xyz'); $obj = Environment::getXyz(); Debug::dump($obj->class); */
/** * set up supported langs from database, also set up language select and all store in cache * */ private function setSupportedLangs() { $supportedLangs = Environment::getCache("SupportedLanguages"); if ($supportedLangs['data'] === null) { $allLangs = LangsModel::getAll(); $langs = array(); $select = array(); foreach ($allLangs as $v) { $langs[$v['id']] = $v['lang']; $select[$v['id']] = $v['name']; } $supportedLangs['data'] = array('langs' => $langs, 'select' => $select); } LangsModel::setLangsSelect($supportedLangs['data']['select']); $this->template->langs = LangsModel::$supportedLangs = $supportedLangs['data']['langs']; }
public function getCache() { return Environment::getCache('Models' . $this->getReflection()->getName()); }