Ejemplo n.º 1
0
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before a test is executed.
  */
 protected function setUp()
 {
     parent::setUp();
     \ntentan\caching\Cache::reset();
     $this->users = \ntentan\models\Model::load('users');
     $this->roles = \ntentan\models\Model::load('roles');
     $this->departments = \ntentan\models\Model::load('departments');
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
 /**
  * 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);
     }
 }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 6
0
 public function setUp()
 {
     Cache::reInstantiate();
     Ntentan::$cacheMethod = 'memcache';
 }
Ejemplo n.º 7
0
 public function cached()
 {
     return Cache::exists($this->getCacheKey());
 }
Ejemplo n.º 8
0
 /**
  * @depends testAdditions
  */
 public function testRemovals()
 {
     Cache::remove('existent');
     $this->assertEquals(false, Cache::get('existent'));
     $this->assertEquals(false, Cache::exists('existent'));
 }
Ejemplo n.º 9
0
 /**
  * Removes an item from the cache.
  * 
  * @param string $key
  */
 public static function remove($key)
 {
     Cache::instance()->removeImplementation($key);
 }
Ejemplo n.º 10
0
 public function testMethodNotFound()
 {
     $this->setExpectedException('ntentan\\exceptions\\MethodNotFoundException');
     Cache::someMethodBi();
 }
Ejemplo n.º 11
0
 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;
     }
 }
Ejemplo n.º 12
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);
     }
 }