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 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 main entry point of the Ntentan application. This method ensures that * ntentan is properly setup for service. It takes the configuration * data as a parameter. The details of the configuration parameter are * extracted from the config file. * * @param array $ntentan The configuration data for ntentan * @param array $app The configuration data for the application */ public static function setup($ntentan, $app = false) { // setup autoloader spl_autoload_register("ntentan\\Ntentan::autoload"); $configFile = Ntentan::$configPath . 'app.ini'; if ($app === false && !file_exists($configFile)) { throw new exceptions\ApiIniFileNotFoundException("Config file *app.ini* not found"); } else { $app = $app === false ? parse_ini_file($configFile, true) : $app; } // hook in the custom exception handler set_exception_handler(array("\\ntentan\\Ntentan", "exceptionHandler")); // setup paths Ntentan::$home = $ntentan['home']; Ntentan::$namespace = $ntentan['namespace']; Ntentan::$modulesPath = isset($ntentan['modules_path']) ? $ntentan['modules_path'] : $ntentan['namespace']; Ntentan::$pluginsHome = $ntentan['plugins'] == '' ? Ntentan::$pluginsHome : $ntentan['plugins']; Ntentan::$appHome = $app['home'] == '' ? '.' : $app['home']; Ntentan::$appName = $ntentan['app']; Ntentan::$prefix = $app['prefix']; Ntentan::$context = $app['context']; Ntentan::$cacheMethod = $app[Ntentan::$context]['caching'] == '' ? Ntentan::$cacheMethod : $app[Ntentan::$context]['caching']; Ntentan::$debug = $app[Ntentan::$context]['debug'] == 'true' || $app[Ntentan::$context]['debug'] == 1 ? true : false; unset($app['home']); unset($app['plugins']); unset($app['prefix']); unset($app['context']); Ntentan::$config = $app; // setup include paths Ntentan::addIncludePath(array('lib/controllers/', 'lib/models/', 'lib/models/datastores/', 'lib/models/exceptions/', 'lib/views/', 'lib/views/template_engines/', 'lib/views/widgets/', 'lib/exceptions/', 'lib/caching/', 'lib/sessions', '/', "./", Ntentan::$namespace, Ntentan::$viewsPath, Ntentan::$pluginsHome), Ntentan::$home); // load cached items if (Cache::exists('nt_camelisations')) { Ntentan::$camelisations = Cache::get('nt_camelisations'); } else { Ntentan::$camelisations = array(); } $camelisations = count(Ntentan::$camelisations); if (!defined('STDOUT')) { sessions\Manager::start(); } }
/** * A utility method to load a controller. This method loads the controller * and fetches the contents of the controller into the Controller::$contents * variable if the get_contents parameter is set to true on call. If a * controller doesn't exist in the module path, a ModelController is loaded * to help manipulate the contents of the model. If no model exists in that * location, it is asumed to be a package and a package controller is * loaded. * * @param $path The path for the model to be loaded. * @param $returnInstanceOnly Fources the method to return only the instance of the controller object. * @return Controller */ public static function load($route, $returnInstanceOnly = false) { $controllerRoute = ''; $routeArray = explode('/', $route); // Loop through the filtered path and extract the controller class for ($i = 0; $i < count($routeArray); $i++) { $p = $routeArray[$i]; $pCamelized = Ntentan::camelize($p); $filePath = Ntentan::$modulesPath . "/modules/{$controllerRoute}/{$p}/"; if (file_exists($filePath . "{$pCamelized}Controller.php")) { $controllerName = $pCamelized . "Controller"; $controllerRoute .= "/{$p}"; $modelRoute .= "{$p}"; if ($controllerRoute[0] == "/") { $controllerRoute = substr($controllerRoute, 1); } if ($controllerName == "") { Ntentan::error("Path not found! [{$route}]"); } else { Ntentan::addIncludePath(Ntentan::$modulesPath . "/{$controllerRoute}/"); //$controllerName.php"; $controllerNamespace = "\\" . str_replace("/", "\\", Ntentan::$namespace . "/modules/{$controllerRoute}/"); $controllerName = $controllerNamespace . $controllerName; if (class_exists($controllerName)) { $controller = new $controllerName(); foreach ($controller->components as $component) { $controller->addComponent($component); } $controller->setRoute($controllerRoute); $controller->setName($controllerName); $controller->modelRoute = $modelRoute; $controller->filePath = $filePath; $controller->init(); if ($returnInstanceOnly) { return $controller; } // Trap for the cache if (Cache::exists("view_" . Ntentan::getRouteKey()) && Ntentan::$debug === false) { echo Cache::get('view_' . Ntentan::$route); return; } if ($controller->method == '') { $controller->method = $routeArray[$i + 1] != '' ? Ntentan::camelize($routeArray[$i + 1], ".", "", true) : $controller->defaultMethodName; $controller->rawMethod = $routeArray[$i + 1] != '' ? $routeArray[$i + 1] : $controller->defaultMethodName; } if (!$controller->hasMethod()) { $modelRoute .= "."; continue; } } else { Ntentan::error("Controller class *{$controllerName}* not found."); } $controller->runMethod(array_slice($routeArray, $i + 2)); return; } } else { $controllerRoute .= "/{$p}"; $modelRoute .= "{$p}."; } } if (is_object($controller)) { $message = "Controller method *{$routeArray[$i - 1]}()* not found for the *{$controllerName}* controller."; } else { $message = "Controller not found for route *{$route}*"; } Ntentan::error($message); }
/** * @depends testAdditions */ public function testRemovals() { Cache::remove('existent'); $this->assertEquals(false, Cache::get('existent')); $this->assertEquals(false, Cache::exists('existent')); }
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); } }