/** * Test for specific cache functionality in requests */ public function testCacheForRequests() { $session = Security::getInstance(); $session->setSessionKey('__CACHE__', ['cache' => 1, 'http' => 'localhost/', 'slug' => 'test']); $hash = Cache::getInstance()->getRequestCacheHash(); $this->assertNotNull($hash, 'Invalid cache hash'); $this->assertEquals($hash, sha1('localhost/ test'), 'Different hash returned by cache'); $this->assertTrue(false !== Cache::needCache(), 'Test url expired or error checking cache'); }
/** * Create translation file if not exists * * @param string $absoluteTranslationFileName * * @return array */ public static function generateTranslationsFile($absoluteTranslationFileName) { $translations = array(); if (file_exists($absoluteTranslationFileName)) { @(include $absoluteTranslationFileName); } else { Cache::getInstance()->storeData($absoluteTranslationFileName, "<?php \$translations = array();\n", Cache::TEXT, TRUE); } return $translations; }
/** * Servicio que devuelve el output * @param string $output * @param string $contentType * @param array $cookies * @return string HTML */ public function output($output = '', $contentType = 'text/html', array $cookies = array()) { Logger::log('Start output response'); ob_start(); $this->setReponseHeaders($contentType, $cookies); header('Content-length: ' . strlen($output)); $cache = Cache::needCache(); if (false !== $cache && $this->status_code === Template::STATUS_OK && $this->debug === false) { Logger::log('Saving output response into cache'); $cacheName = $this->cache->getRequestCacheHash(); $this->cache->storeData("json" . DIRECTORY_SEPARATOR . $cacheName, $output); $this->cache->storeData("json" . DIRECTORY_SEPARATOR . $cacheName . ".headers", headers_list(), Cache::JSON); } echo $output; ob_flush(); ob_end_clean(); Logger::log('End output response'); $this->closeRender(); }
/** * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no * * @param string $route * @param array|null $action * @param types\Controller $class * @param array $params */ protected function executeCachedRoute($route, $action, $class, $params = NULL) { Logger::log('Executing route ' . $route, LOG_INFO); Security::getInstance()->setSessionKey("__CACHE__", $action); $cache = Cache::needCache(); $execute = TRUE; if (FALSE !== $cache && Config::getInstance()->getDebugMode() === FALSE) { $cacheDataName = $this->cache->getRequestCacheHash(); $cachedData = $this->cache->readFromCache("templates" . DIRECTORY_SEPARATOR . $cacheDataName, $cache, function () { }); if (NULL !== $cachedData) { $headers = $this->cache->readFromCache("templates" . DIRECTORY_SEPARATOR . $cacheDataName . ".headers", $cache, function () { }, Cache::JSON); Template::getInstance()->renderCache($cachedData, $headers); $execute = FALSE; } } if ($execute) { call_user_func_array(array($class, $action['method']), $params); } }
/** * Método que inyecta automáticamente las dependencias en la clase */ public function init() { if (!$this->isLoaded()) { $cacheFilename = "reflections" . DIRECTORY_SEPARATOR . sha1(get_class($this)) . ".json"; /** @var \PSFS\base\Cache $cacheService */ $cacheService = Cache::getInstance(); /** @var \PSFS\base\config\Config $configService */ $configService = Config::getInstance(); $properties = $cacheService->getDataFromFile($cacheFilename, Cache::JSON); if (true === $configService->getDebugMode() || null === $properties) { $properties = InjectorHelper::getClassProperties(get_class($this)); $cacheService->storeData($cacheFilename, $properties, Cache::JSON); } /** @var \ReflectionProperty $property */ if (!empty($properties) && is_array($properties)) { foreach ($properties as $property => $class) { $this->load($property, true, $class); } } $this->setLoaded(); } else { Logger::log(get_class($this) . ' already loaded', LOG_INFO); } }
/** * Method that reloads config file */ public function loadConfigData() { $this->config = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "config.json", Cache::JSON, TRUE) ?: []; $this->debug = array_key_exists('debug', $this->config) ? (bool) $this->config['debug'] : FALSE; }
/** * Método que guarda en fichero los datos pasados * @param $path * @param $data * @param int $transform * @param boolean $absolutePath */ public function storeData($path, $data, $transform = Cache::TEXT, $absolutePath = false) { $data = Cache::transformData($data, $transform); $this->saveTextToFile($data, $path, $absolutePath); }
/** * Método que inicializa el motor de plantillas */ private function setup() { $this->debug = Config::getInstance()->getDebugMode() ?: FALSE; $this->cache = Cache::getInstance(); $loader = new \Twig_Loader_Filesystem(Config::getInstance()->getTemplatePath()); $this->tpl = new \Twig_Environment($loader, array('cache' => Config::getInstance()->getCachePath() . DIRECTORY_SEPARATOR . 'twig', 'debug' => (bool) $this->debug, 'auto_reload' => TRUE)); }