Example #1
0
 protected function addImplementation($key, $object, $ttl)
 {
     if (file_exists(self::getCacheFile()) && is_writable(self::getCacheFile())) {
         $expires = $ttl == 0 ? $ttl : $ttl + time();
         $object = array('expires' => $expires, 'object' => $object);
         $key = $this->hashKey($key);
         file_put_contents(self::getCacheFile("{$key}"), serialize($object));
     } else {
         // Throwing exceptions here sometimes breaks the template engine
         Ntentan::error("The file cache directory *" . self::getCacheFile() . "* was not found or is not writable!", 'Caching error!');
         trigger_error("The file cache directory *" . self::getCacheFile() . "* was not found or is not writable!");
     }
 }
Example #2
0
 public static function initialize($data)
 {
     $type = gettype($data);
     switch ($type) {
         case 'string':
             return new Variable($data);
         case 'array':
             return new Variable($data, array_keys($data));
         case 'object':
         case 'boolean':
         case 'integer':
         case 'double':
         case 'NULL':
             return $data;
         default:
             \ntentan\Ntentan::error("Cannot handle the {$type} type in templates");
     }
 }
Example #3
0
 /**
  * Default call back for displaying exceptions.
  * @param Exception $exception
  */
 public static function exceptionHandler($exception)
 {
     $class = new \ReflectionObject($exception);
     $logged = utils\Logger::log($exception->getMessage() . "\n" . $exception->getTraceAsString(), "logs/application.log");
     echo Ntentan::error("Exception <code><b>{$class->getName()}</b></code> thrown in " . "<code><b>{$exception->getFile()}</b></code> on line " . "<code><b>{$exception->getLine()}</b></code>. " . $exception->getMessage() . ($logged === false ? "\n\n<p>Failed to log this exception. Please check and ensure " . "that the file [logs/application.log] exists and is " . "writable.</p>" : ""), "Exception <code>" . $class->getName() . "</code> thrown", null, true, $exception->getTrace());
 }
Example #4
0
 /**
  * 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);
 }
Example #5
0
 public function __call($widget, $arguments)
 {
     $widgetInstance = $this->loadWidget($widget);
     if ($widgetInstance === false) {
         Ntentan::error("Widget *{$widget}* not found");
     } else {
         $method = new \ReflectionMethod($widgetInstance, 'init');
         $method->invokeArgs($widgetInstance, $arguments);
         return $widgetInstance;
     }
 }
Example #6
0
 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);
     }
 }