Ejemplo n.º 1
0
 /**
  * Run the router
  *
  * @param string $method
  * @param string $url
  */
 public function run($method = null, $url = null)
 {
     // Prepare the session loading.
     Http::getInstance()->session()->init();
     // Begin parsing request.
     if ($method === null) {
         $method = $_SERVER['REQUEST_METHOD'];
     }
     if ($url === null) {
         $url = $_SERVER['REQUEST_URI'];
         // Strip query from url
         if (strstr($url, '?')) {
             $parts = explode('?', $url, 2);
             $url = $parts[0];
         }
     }
     // Compile
     $this->compiled = strtoupper($method) . '~' . $url;
     // Try to match
     foreach ($this->routes as $route) {
         if ($route->match($this->compiled)) {
             $this->executeRoute($route, $method);
             break;
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Get spoofed request class instance
  *
  * @param string $url
  * @param string $method
  * @param array $post
  * @return \Arvici\Heart\Http\Request
  */
 private function spoofedRequest($url, $method = 'get', $post = array())
 {
     TestUtils::clear();
     //$url = '/get/test?param1=1&param2=yes&param3[]=1&param3[]=2&param3[]=3';
     $urlparts = parse_url($url);
     parse_str($urlparts['query'], $get);
     TestUtils::spoofUrl($url, $method, $get, $post);
     return Http::getInstance()->request();
 }
Ejemplo n.º 3
0
 /**
  * Create validation class with input.
  *
  * @param array|null $input Array of input, null or leave undefined for all request parameters/values.
  */
 public function __construct($input = null)
 {
     // Default we validate $_POST and $_GET data together.
     if ($input === null) {
         $input = Http::getInstance()->request()->params();
     }
     $this->input = $input;
     $this->set = array();
 }
Ejemplo n.º 4
0
 /**
  * Get fresh clean response class instance.
  *
  * @return \Arvici\Heart\Http\Response
  */
 private function getResponse()
 {
     TestUtils::clear();
     return Http::getInstance()->response();
 }
Ejemplo n.º 5
0
 /**
  * Render all views in the stack.
  *
  * @param bool $return Return the rendered output?
  *
  * @return mixed|void
  *
  * @throws RendererException
  */
 public function run($return = false)
 {
     $output = "";
     foreach ($this->stack as $view) {
         /** @var View $view */
         $data = $view->getData();
         if (!is_array($data)) {
             $data = array();
         }
         if ($view->getType() === View::PART_BODY_PLACEHOLDER) {
             throw new RendererException("The body placeholder isn't replaced!");
             // @codeCoverageIgnore
         }
         if (!empty($this->globalData)) {
             $data = array_merge($data, $this->globalData);
         }
         $engineClass = $view->getEngine();
         /** @var RendererInterface $engine */
         $engine = $engineClass->newInstance();
         if (!$engine instanceof RendererInterface) {
             // @codeCoverageIgnore
             throw new RendererException("Engine is not instance of the RendererInterface.");
             // @codeCoverageIgnore
         }
         // @codeCoverageIgnore
         // Render it!
         $output .= $engine->render($view, $data);
     }
     // If we need to return. Then just return it.
     if ($return) {
         return $output;
     }
     // Instead, push it into the response object and send.
     Http::getInstance()->response()->body($output)->send(200);
     // @codeCoverageIgnore
     return;
     // @codeCoverageIgnore
 }
Ejemplo n.º 6
0
 /**
  * @covers \Arvici\Heart\Router\Router
  * @covers \Arvici\Heart\Router\Route
  * @covers \Arvici\Heart\Http\Http
  * @covers \Arvici\Heart\Http\Request
  * @covers \Arvici\Heart\Http\Response
  */
 public function testQueryParameters()
 {
     TestUtils::clear();
     $router = Router::getInstance();
     $router->addRoute(new Route('/test/get/controller/query', 'GET', 'App\\Controller\\TestQuery::basicQuery'));
     $this->spoof('/test/get/controller/query?test=yes', 'GET', array('test' => 'yes'));
     try {
         $router->run();
         $this->assertTrue(false);
     } catch (\Exception $e) {
         $this->assertEquals(999, $e->getCode());
     }
     // Test via request object (right here)
     $request = Http::getInstance()->request();
     $this->assertEquals(array('test' => 'yes'), $request->get()->all());
     // More testing on REQUEST is done in a separate case
 }
Ejemplo n.º 7
0
 /**
  * Controller constructor, will prepare the request and response objects.
  */
 public function __construct()
 {
     $this->request = Http::getInstance()->request();
     $this->response = Http::getInstance()->response();
 }
Ejemplo n.º 8
0
 public static function clearHttp()
 {
     Http::clearInstance();
 }