Esempio n. 1
0
 public function testLogToFile()
 {
     $logFile = __DIR__ . '/test.log';
     if (file_exists($logFile)) {
         unlink($logFile);
     }
     $logger = new TipyLogger($logFile);
     $logger->format = '[%level]';
     $logger->info("I am in file");
     $this->assertTrue(file_exists($this->getFilePath($logger)));
     $log = file_get_contents($this->getFilePath($logger));
     $this->assertEqual($log, "[INFO] I am in file" . PHP_EOL);
 }
Esempio n. 2
0
 /**
  * Query database
  *
  * <code>
  * $dao = new TipyDAO();
  * $result = $dao->query('select * from users where first_name=? and last_name=?', ['john', 'doe']);
  * </code>
  *
  * @param string $sql SQL template with ? placeholders
  * @param array $params Array with values to fill placeholders
  * @throws TipyDaoException
  * @return mysqli_result
  */
 public function query($sql, $params = null)
 {
     if ($params) {
         $sql = str_replace('%', '%%', $sql);
         $sql = str_replace('?', '"%s"', $sql);
         $link = $this->dbLink;
         array_walk($params, function (&$string) use($link) {
             if ($string === null) {
                 $string = 'TIPY_SQL_NULL_VALUE';
             }
             $string = $link->real_escape_string($string);
         });
         array_unshift($params, $sql);
         $query = call_user_func_array('sprintf', $params);
         $query = str_replace('"TIPY_SQL_NULL_VALUE"', 'NULL', $query);
     } else {
         $query = $sql;
     }
     $this->logger->debug($query);
     $result = $this->dbLink->query($query);
     self::$queryCount++;
     if (!$result) {
         throw new TipyDaoException($this->dbLink->error);
     } else {
         return $result;
     }
 }
Esempio n. 3
0
 /**
  * Initialize controller and run action
  *
  * Requires **$app->in('controller')** and **$app->in('action')**
  * parameters to be defined.
  *
  * @see TipyController
  * @todo Different exceptions handling in production and development modes
  */
 public function run()
 {
     $this->logger->debug($this->request->method() . ': ' . $this->request->uri());
     try {
         // Get controller and action name
         $controllerName = $this->in->get('controller');
         $actionName = $this->in->get('action');
         // Some basic checking
         if (!$controllerName || preg_match('/^\\W*$/', $controllerName)) {
             throw new TipyException('Controller name is missing');
         }
         if (!$actionName || preg_match('/^\\W*$/', $actionName)) {
             throw new TipyException('Action name is missing');
         }
         $controllerName = TipyInflector::controllerize($controllerName) . 'Controller';
         if (class_exists($controllerName)) {
             $controller = new $controllerName();
         } else {
             throw new TipyException('Unable to find ' . $controllerName . ' class');
         }
         $actionName = TipyInflector::camelCase($actionName);
         if (in_array($actionName, get_class_methods($controllerName))) {
             $this->logger->debug('Executing ' . $controllerName . '::' . $actionName . '();');
             $controller->execute($actionName);
         } else {
             throw new TipyException('Undefined action ' . $controllerName . '::' . $actionName . '()');
         }
     } catch (Exception $exception) {
         throw new TipyException($exception->getMessage(), $exception->getCode(), $exception);
     }
 }