예제 #1
0
 public function testDumpArray()
 {
     $response = new Response();
     $this->assertEmpty($response->body());
     $response->dump(array('sure', 1, 10, 17, 'ok' => 'no'));
     $this->assertNotEmpty($response->body());
     $this->assertNotEquals('<pre></pre>', $response->body());
 }
예제 #2
0
파일: Action.php 프로젝트: nlevee/php-libs
 /**
  * Fonction par default de gestion/retranscription d'erreur
  * @param Exception $error
  * @param \Router\Response $response
  * @return string
  */
 public function error(\Exception $error, Router\Response $response)
 {
     $response->type("text/html", "utf-8");
     $response->status($error->getCode() ?: 500);
     return '<html><head></head><body><pre>' . (string) $error . '</pre></body></html>';
 }
예제 #3
0
 /**
  * On lance le dispatch de la requete avec les routes inséré
  * @param Response $response
  * @return bool
  */
 public function perform(\Router\Response $response)
 {
     // ajout de la route global si existe
     $aApplyRoute = $this->routes['ANY'];
     if (isset($this->routes[$this->method])) {
         $aApplyRoute = array_merge($this->routes[$this->method], $aApplyRoute);
     }
     if (!empty($aApplyRoute)) {
         require_once __DIR__ . "/Route.php";
         foreach ($aApplyRoute as $route) {
             // récuperation des params de la route et creation de l'objet pour test
             list($pattern_uri, $callback, $filters) = $route;
             $oRoute = new \Router\Route($pattern_uri, array_merge($this->filters, $filters));
             if (false === ($params = $oRoute->match($this->request->getRequestUri()))) {
                 continue;
             }
             // insert des params dans le request
             foreach ($params as $key => $value) {
                 $this->request->setParam($key, $value);
             }
             // appel du callback si c'est possible
             if (is_callable($callback) === true) {
                 ob_start();
                 call_user_func_array($callback, array($this->request, $response));
                 $response->appendBody(ob_get_contents());
                 ob_end_clean();
             }
             return true;
         }
         unset($oRoute);
     }
     // renvoi false si aucune regle n'est passé
     return false;
 }
예제 #4
0
 /**
  * @param Cookie $cookie
  *
  * @return mixed
  */
 public function addCookie(Cookie $cookie)
 {
     $cookie = new ResponseCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiration(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
     $this->response->cookies()->set($cookie->getName(), $cookie);
 }
예제 #5
0
 /**
  * Handles an HTTP error exception through our HTTP error callbacks
  *
  * @param HttpExceptionInterface $http_exception The exception that occurred
  * @param RouteCollection $matched The collection of routes that were matched in dispatch
  * @param array $methods_matched The HTTP methods that were matched in dispatch
  * @return void
  */
 protected function httpError(HttpExceptionInterface $http_exception, RouteCollection $matched, $methods_matched)
 {
     if (!$this->response->isLocked()) {
         $this->response->code($http_exception->getCode());
     }
     if (count($this->httpErrorCallbacks) > 0) {
         foreach (array_reverse($this->httpErrorCallbacks) as $callback) {
             if ($callback instanceof Route) {
                 $this->handleRouteCallback($callback, $matched, $methods_matched);
             } elseif (is_callable($callback)) {
                 if (is_string($callback)) {
                     $callback($http_exception->getCode(), $this, $matched, $methods_matched, $http_exception);
                 } else {
                     call_user_func($callback, $http_exception->getCode(), $this, $matched, $methods_matched, $http_exception);
                 }
             }
         }
     }
     // Lock our response, since we probably don't want
     // anything else messing with our error code/body
     $this->response->lock();
 }
예제 #6
0
파일: App.php 프로젝트: nlevee/php-libs
 /**
  * Cette fonction est lancé par le routeur et permet de lancer l'action qui convient
  * selon les paramètre recu dans $request
  * @param \Router\Request $request
  * @param \Router\Response $response
  * @return mixed
  */
 public function dispatch(Router\Request $request, Router\Response $response)
 {
     $sControllerName = $request->getParam('controller', $this->default['controller']);
     $sActionName = $request->getParam('action', $this->default['action']);
     // appel de l'action requise
     $output = $this->launch($sControllerName, $sActionName, array($request, $response));
     // ajout du contenu dans le body de la response
     $response->appendBody($output);
     return $output;
 }