/** * Starts the initializing of given locale and namespace. * * This method is intend to start the initializing of given locale and namespace and return the corresponding key. * * @param string $locale Locale to init * @param array $namespaces Namespaces to init * * @author Benjamin Carl <*****@*****.**> * * @return string The key (identifier) for the created translationtable */ public function initLocaleNamespace($locale, array $namespaces) { // get checksum for translation-table + namespaces(s) $checksum = hash('md4', $locale . serialize($namespaces)); // Assume cached content does not exist $translationTable = false; // If we want to initialize the translation table we need at least support for it ... if (true === $this->hasTranslationTableSupport($this)) { // We only run the generating code if either: a) never generated before or b) if caching is disabled if (false === isset(self::$translationTables[$checksum]) || false === $this->getCacheEnabled()) { // If caching enabled => try to get table from cache if (true === $this->getCacheEnabled()) { try { $translationTable = self::$cache->read($checksum); } catch (Doozr_Cache_Service_Exception $e) { // Intentionally left empty } } // If we did not receive a valid result from cache or if cache is disabled => parse file(s) for table(s) if (false === $translationTable) { // Build translationtable $translationTable = $this->buildTranslationtable($locale, $namespaces); } // Store in cache if caching enabled if (true === $this->getCacheEnabled()) { // cache translationtable self::$cache->create($checksum, $translationTable, $this->getCacheLifetime()); } // Put into local translationtable (runtime cache!) self::$translationTables[$checksum] = $translationTable; } } // In all cases return the crc (key/identifier) return $checksum; }
/** * This method is intend to render the current state of the view as html. For this it makes use of the base * template engine, and html5 template files. If you need another output or something like this, you must * overwrite this method. * * @param array $data The data as override for internal stored data * @param string $fingerprint Optional fingerprint used as cache identifier for front- and * backend! Hint: Rendering user specific data an user identifier * MUST be used as salt when generating the fingerprint!!! * Otherwise user specific data can and will be sent to another * user!. So the following rule should be followed: * - generic view/template no user data = fingerprint by * content/path/url * - user specific view/template with user data = use * session-id or user-id! * @param Doozr_I18n_Service_Interface $i18n An instance of a Doozr I18n service * * @author Benjamin Carl <*****@*****.**> * * @return bool TRUE if successful, otherwise FALSE * * @throws Doozr_Cache_Service_Exception * @throws Doozr_Base_View_Exception * @throws Doozr_Exception * @throws PHPTAL_ConfigurationException */ protected function render(array $data = [], $fingerprint = null, Doozr_I18n_Service_Interface $i18n = null) { $this->setFingerprint($this->generateFingerprint($fingerprint, $data)); $html = null; if (false === $this->getConfiguration()->kernel->debugging->enabled) { // We try to receive data for rendering from cache :) this is much faster try { $html = $this->cache->read($this->getFingerprint()); } catch (Doozr_Cache_Service_Exception $e) { $html = null; } } // If data was/could not be retrieved we get it fresh here ... if ($html === null) { // Get name of template file $templateFile = $this->configuration->kernel->view->template->path . $this->translateToTemplateFilename() . '.' . $this->getTemplateExtension(); if (false === $this->getRegistry()->getFilesystem()->exists($templateFile)) { throw new Doozr_Base_View_Exception(sprintf('The template file "%s" is required for rendering but it does not exist.', $templateFile)); } /* @var $template PHPTAL */ $template = Doozr_Loader_Serviceloader::load('template', $templateFile); // Set output runtimeEnvironment ... $template->setOutputMode($this->getOutputMode()); // if I18n passed -> forward to template engine (e.g. PHPTAL) if (null !== $i18n) { $i18n->useDomain($this->translateToTextdomain()); $template->setTranslator($i18n); $template->{'doozr_locale'} = $i18n->getLocale(); } // Assign data from passed in array to template (for use as a template variable) foreach ($data as $key => $value) { $template->{$key} = $value; } // setup template compile output dir $template->setPhpCodeDestination($this->configuration->kernel->view->directories->compiled); // set the encoding of output $template->setEncoding($this->configuration->kernel->localization->encoding); // Output XHTML or HTML5 ... ? $template->setOutputMode($this->configuration->kernel->view->settings->outputmode); // execute = get result $html = $template->execute(); if (true === $this->isDebugging()) { $renderer = $this->getRegistry()->getDebugbar()->getJavascriptRenderer(); $renderer->setBaseUrl('/assets'); $head = $renderer->renderHead(); $body = $renderer->render(); $html = str_replace('</head>', $head . '</head>', $html); $html = str_replace('</body>', $body . '</body>', $html); } // finally store in cache try { $this->cache->create($html, $this->getFingerprint()); } catch (Doozr_Cache_Service_Exception $e) { pred($e); } } return $html; }
/** * Calls the garbage-collector of the cache-container. * * - is forced ($force = true) * - there was a run before and this run is older = gcProbabilityTime * * @param string $scope The scope to look for elements in * @param int $maximumLifetime The maximum lifetime of an element * @param bool $force TRUE to force a garbage collection run, otherwise FALSE (default) to check * * @author Benjamin Carl <*****@*****.**> * * @return int The number of elements collected in gc run */ public function garbageCollection($scope = null, $maximumLifetime = null, $force = false) { // Default result $result = 0; // Get our active scope if no special one passed if ($scope === null) { $scope = $this->getScope(); } // The maximum lifetime for the entry in seconds if ($maximumLifetime === null) { $maximumLifetime = $this->getGcMaximumLifetime(); } // Start the randomizer ... srand(microtime(true)); // Force || Time and probability based if ($force || self::$gcLastRunTimestamp !== null && self::$gcLastRunTimestamp < time() + $this->getGcProbabilityTime() || rand(1, 100) <= $this->gcProbability) { $result = $this->getContainer()->garbageCollection($scope, $maximumLifetime); self::$gcLastRunTimestamp = time(); } return $result; }