/** * @depends testAdditions */ public function testExpiry() { Cache::add('should_expire', 'something', 5); $this->assertEquals(true, Cache::exists('should_expire')); sleep(6); $this->assertEquals(false, Cache::exists('should_expire')); $this->assertEquals(false, Cache::get('should_expire')); }
public function testWrongPath() { $this->setExpectedException('PHPUnit_Framework_Error', 'The file cache directory *i_dont_think_this_dir_would_exist* was not found or is not writable!'); Ntentan::$debug = false; // prevent the error message from showing Cache::setCachePath('i_dont_think_this_dir_would_exist'); Cache::add('test', 'should fail'); Ntentan::$debug = true; }
public function describe() { if (!Cache::exists("model_" . $this->route)) { $description = $this->dataStore->describe(); if (is_array($this->mustBeUnique)) { foreach ($description["fields"] as $i => $field) { $uniqueField = false; foreach ($this->mustBeUnique as $unique) { if (is_array($unique)) { if (isset($unique['field'])) { if ($field["name"] == $unique["field"]) { $uniqueField = true; $uniqueMessage = $unique["message"]; } } else { throw new exceptions\DescriptionException("A mustBeUnique constraint specified as an array must always contain a field property"); } } else { if ($field["name"] == $unique) { $uniqueField = true; $uniqueMessage = null; } } } if ($uniqueField) { $description["fields"][$i]["unique"] = true; if ($uniqueMessage != null) { $description["fields"][$i]["unique_violation_message"] = $uniqueMessage; } } } } if (is_array($this->belongsTo)) { foreach ($this->belongsTo as $belongsTo) { $belongsToModel = is_array($belongsTo) ? $belongsTo[0] : $belongsTo; $description["belongs_to"][] = $belongsToModel; $alias = null; if (is_array($belongsTo)) { $fieldName = $belongsTo["as"]; $alias = $belongsTo["as"]; } else { $alias = strtolower(Ntentan::singular($this->getBelongsTo($belongsTo))); $fieldName = $alias . "_id"; } foreach ($description["fields"] as $i => $field) { if ($field["name"] == $fieldName) { $description["fields"][$i]["model"] = Ntentan::plural($belongsToModel); $description["fields"][$i]["foreign_key"] = true; $description["fields"][$i]["field_name"] = $fieldName; if ($alias != '') { $description["fields"][$i]["alias"] = $alias; } } } } } else { if ($this->belongsTo != null) { $description["belongs_to"][] = $this->belongsTo; $fieldName = strtolower(Ntentan::singular($this->belongsTo)) . "_id"; foreach ($description["fields"] as $i => $field) { if ($field["name"] == $fieldName) { $description["fields"][$i]["model"] = $this->belongsTo; $description["fields"][$i]["foreign_key"] = true; } } } } Cache::add("model_" . $this->route, $description); } return Cache::get("model_" . $this->route); }
/** * The routing engines entry. This method analyses the URL and implements * the routing engine. */ public static function route() { // Implement the routing engine Ntentan::$requestedRoute = $_GET["q"]; if (Ntentan::$route == '') { Ntentan::$route = Ntentan::$requestedRoute; } unset($_GET["q"]); unset($_REQUEST["q"]); if (Ntentan::$route == "") { Ntentan::$route = Ntentan::$defaultRoute; } else { foreach (Ntentan::$routes as $route) { if (preg_match($route["pattern"], Ntentan::$route, $matches) == 1) { $parts = array(); if (isset($route["route"])) { $newRoute = $route["route"]; foreach ($matches as $key => $value) { $newRoute = str_replace("::{$key}", $value, $newRoute); $parts["::{$key}"] = $value; } Ntentan::$route = $newRoute; } if (is_array($route["globals"])) { foreach ($route["globals"] as $key => $value) { $GLOBALS["ROUTE_{$key}"] = str_replace(array_keys($parts), $parts, $value); } } break; } } } if (Ntentan::$route == "") { Ntentan::$route = isset($route['default']) ? $route['default'] : Ntentan::$postRoutingDefaultRoute; } controllers\Controller::load(Ntentan::$route); // Store all camelisations into the cache; if (count(Ntentan::$camelisations) > $camelisations) { Cache::add('nt_camelisations', Ntentan::$camelisations); } }
public function runMethod($params, $method = null) { $path = $method === null ? $this->method : $method; if (method_exists($this, $path)) { $controllerClass = new ReflectionClass($this->getName()); $method = $controllerClass->GetMethod($path); if ($this->view->template == null) { $this->view->template = str_replace("/", "_", $this->route) . '_' . $this->getRawMethod() . '.tpl.php'; } $method->invokeArgs($this, $params); $this->preRender(); $return = $this->view->out($this->getData()); $return = $this->postRender($return); } else { foreach ($this->componentInstances as $component) { //@todo Look at how to prevent this from running several times if ($component->hasMethod($path)) { $component->variables = $this->variables; $component->runMethod($params, $path); break; } } } if ($this->view->cacheTimeout !== false && Ntentan::$debug !== true) { Cache::add('view_' . Ntentan::getRouteKey(), $return, $this->view->cacheTimeout); } echo $return; }
public function __toString() { $cacheKey = $this->getCacheKey(); if (Cache::exists($cacheKey) && Ntentan::$debug === false) { $output = Cache::get($cacheKey); } else { $this->execute(); $this->preRender(); TemplateEngine::appendPath($this->filePath); if ($this->template == "") { $this->template = ($this->plugin == '' ? '' : "{$this->plugin}_") . "{$this->name}_widget.tpl.php"; } try { $output = TemplateEngine::render($this->template, $this->data); } catch (Exception $e) { die('Template not Found!'); } $this->postRender(); Cache::add($cacheKey, $output, $this->cacheLifetime); } return $output; }
public function doesTableExist($table, $schema) { $key = "schema_table_{$schema}_{$table}"; if (Cache::exists($key)) { return Cache::get($key); } else { $exists = $this->_doesTableExist($table, $schema); Cache::add($key, $exists); return $exists; } }
public static function render($template, $templateData, $view = null) { $cacheKey = "template_{$template}_" . TemplateEngine::getContext(); $path = TemplateEngine::getPath(); if (Cache::exists($cacheKey) && Ntentan::$debug === false) { $templateFile = Cache::get($cacheKey); } else { $extension = explode('.', $template); $breakDown = explode('_', array_shift($extension)); $extension = implode(".", $extension); for ($i = 0; $i < count($breakDown); $i++) { $testTemplate = implode("_", array_slice($breakDown, $i, count($breakDown) - $i)) . ".{$extension}"; foreach (TemplateEngine::getPath() as $path) { $newTemplateFile = "{$path}/{$testTemplate}"; if (file_exists($newTemplateFile)) { Cache::add($cacheKey, $newTemplateFile); $templateFile = $newTemplateFile; break; } } if ($templateFile != '') { break; } } } if ($templateFile == null) { $pathString = "[" . implode('; ', TemplateEngine::getPath()) . "]"; Ntentan::error("Could not find a suitable template file for the current request <b><code>{$template}</code></b>. Template path <b>{$pathString}</b>"); die; } else { return TemplateEngine::getEngineInstance($templateFile)->generate($templateData, $view); } }