Beispiel #1
0
 /**
  * @param RelayBuilder $relay
  *
  * @return $this
  */
 public function buildApplication(RelayBuilder $relay)
 {
     $this->registerCoreDependencies();
     $callable[] = $this->container->get('Vu\\Router\\Router');
     $callable[] = new Action($this->container);
     $next = $relay->newInstance($callable);
     return $next($this->container->get(ServerRequestInterface::class), $this->container->get('Psr\\Http\\Message\\ResponseInterface'));
 }
 /**
  * Handle
  *
  * @param Request $request Request
  * @param Response $response Response
  *
  * @return Response Response
  */
 public function handle(HttpMessages_CraftRequest $request, HttpMessages_CraftResponse $response)
 {
     $relay = new RelayBuilder();
     $globalMiddleware = \Craft\craft()->config->get('globalMiddleware', 'httpmessages');
     $routeMiddleware = $request->getRoute()->getMiddleware();
     $dispatcher = $relay->newInstance(array_merge($globalMiddleware, $routeMiddleware));
     return $dispatcher($request, $response);
 }
 /**
  * Run the application.
  */
 public function run()
 {
     // Create Request and Response
     $request = $this->container->get(ServerRequestInterface::class);
     $response = new Response();
     $builder = new RelayBuilder(function ($callable) {
         return is_string($callable) ? $this->container->get($callable) : $callable;
     });
     // Apply middlewares
     $relay = $builder->newInstance($this->getMiddlewares());
     $response = $relay($request, $response);
     (new SapiEmitter())->emit($response);
 }
Beispiel #4
0
 /**
  * Router object
  *
  * @param App $app
  * @return Relay
  */
 public static function create(ServiceContainer $app)
 {
     $resolver = array(static::class, 'resolver');
     $relayBuilder = new RelayBuilder($resolver);
     // Add app container to request
     $app->request = $app->request->withAttribute('app', $app);
     $queue = [];
     // Runtime middleware
     //$queue[] = '\Molengo\Middleware\ControllerMiddleware';
     //
     // User defined middleware
     if (!empty($app->config['queue'])) {
         $queue = array_merge_recursive($queue, $app->config['queue']);
     }
     $app->config['queue'] = $queue;
     return $relayBuilder->newInstance($queue);
 }
Beispiel #5
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $request = $this->decorateRequest($request);
     $response = $this->decorateResponse($response);
     $relay = $this->relayBuilder->newInstance($this->middleware);
     $error = $this->errorHandler;
     $notFound = $this->notFoundHandler;
     try {
         $response = $relay($request, $response);
     } catch (\Exception $ex) {
         $response = $error($request, $response, $ex);
     }
     // todo: detect this better
     $body = (string) $response->getBody();
     if (0 === strlen($body)) {
         $response = $notFound($request, $response);
     }
     return $next ? $next($request, $response) : $response;
 }
Beispiel #6
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $request = $this->decorateRequest($request);
     $response = $this->decorateResponse($response);
     $relay = $this->relayBuilder->newInstance($this->middleware);
     $error = $this->errorHandler;
     $notFound = $this->notFoundHandler;
     try {
         $response = $relay($request, $response);
     } catch (\Exception $ex) {
         $response = $error($request, $response, $ex);
     }
     if (!$response instanceof ResponseInterface) {
         throw new Exception\InvalidResponse();
     }
     if (null === $this->routeMap->getRouteMatch()) {
         $response = $notFound($request, $response);
     }
     return $next ? $next($request, $response) : $response;
 }
Beispiel #7
0
<?php

require_once '../vendor/autoload.php';
use Relay\RelayBuilder;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
$request = ServerRequestFactory::fromGlobals();
$response = new Response();
$queue = array();
$relayBuilder = new RelayBuilder();
$relay = $relayBuilder->newInstance($queue);
$response = $relay($request, $response);
exit;
Beispiel #8
0
 /**
  * Match a given Request Url against stored routes
  * @param string $requestUrl
  * @param string $requestMethod
  * @return array|boolean Array with route information on success, false on failure (no match).
  */
 public function dispatch($requestUrl = null, $requestMethod = null)
 {
     $params = array();
     $match = false;
     // set Request Url if it isn't passed as parameter
     if ($requestUrl === null) {
         $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
     }
     // strip base path from request url
     $requestUrl = substr($requestUrl, strlen($this->basePath));
     // Strip query string (?a=b) from Request Url
     if (($strpos = strpos($requestUrl, '?')) !== false) {
         $requestUrl = substr($requestUrl, 0, $strpos);
     }
     // Strip anchors from the url
     // if (($strpos = strpos($requestUrl, '#')) !== false) {
     // 	$requestUrl = substr($requestUrl, 0, $strpos);
     // }
     // set Request Method if it isn't passed as a parameter
     if ($requestMethod === null) {
         $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
     }
     foreach ($this->routes as $handler) {
         list($method, $_route, $target, $name) = $handler;
         $methods = explode('|', $method);
         $method_match = false;
         // Check if request method matches. If not, abandon early. (CHEAP)
         foreach ($methods as $method) {
             if (strcasecmp($requestMethod, $method) === 0) {
                 $method_match = true;
                 break;
             }
         }
         // Method did not match, continue to next route.
         if (!$method_match) {
             continue;
         }
         // Check for a wildcard (matches all)
         if ($_route === '*') {
             $match = true;
         } elseif (isset($_route[0]) && $_route[0] === '@') {
             $pattern = '`' . substr($_route, 1) . '`u';
             $match = preg_match($pattern, $requestUrl, $params);
         } else {
             $route = null;
             $regex = false;
             $j = 0;
             $n = isset($_route[0]) ? $_route[0] : null;
             $i = 0;
             // Find the longest non-regex substring and match it against the URI
             while (true) {
                 if (!isset($_route[$i])) {
                     break;
                 } elseif (false === $regex) {
                     $c = $n;
                     $regex = $c === '[' || $c === '(' || $c === '.';
                     if (false === $regex && false !== isset($_route[$i + 1])) {
                         $n = $_route[$i + 1];
                         $regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
                     }
                     if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) {
                         continue 2;
                     }
                     $j++;
                 }
                 $route .= $_route[$i++];
             }
             $regex = $this->compileRoute($route);
             $match = preg_match($regex, $requestUrl, $params);
         }
         if ($match == true || $match > 0) {
             if ($params) {
                 foreach ($params as $key => $value) {
                     if (is_numeric($key)) {
                         unset($params[$key]);
                     }
                 }
             }
             $mm = array('target' => $target, 'params' => $params, 'name' => $name);
             if ($match) {
                 //we are going to change this to use all be in the router function
                 //break up the target at the @ symbol. first part is function second is the file
                 $target = explode('@', $mm['target']);
                 $method = $target[0];
                 $class = "\\App\\Controllers\\" . $target[1];
                 //include the source file
                 include $this->controllersDir . str_replace('\\', '/', $target[1]) . '.php';
                 //instantiate the class
                 $instance = new $class();
                 //this is where we will place our middleware. first check to see if the there is an alias in
                 //the middleware array and then execute it against our controller.
                 //get the middleware array from the instance
                 $instanceMiddleware = $instance->getMiddleware();
                 $middleware = $this->getMiddleWareArray();
                 //count the instance middleware array
                 $middlewareCnt = count($instanceMiddleware);
                 $middlewareInstances = [];
                 //build an acceptible middleware list to send to relay
                 $i = 0;
                 foreach ($instanceMiddleware as $val) {
                     //then loop through the middleware and see if we have a match
                     foreach ($middleware as $key => $value) {
                         if ($val == $key) {
                             //check if its an array if it is we have to add them to the container individually.
                             $middlewareInstances[$i][] = $instanceMiddleware[$i];
                             if (is_array($value)) {
                                 //first index is the middleware class and the second index is the plugin
                                 for ($j = 0; $j < count($value); $j++) {
                                     $middlewareInstances[$i][] = $value[$j];
                                     $i++;
                                 }
                             } else {
                                 //just a middleware class. This class will extend from another and doesnt need the plugin portion
                                 $middlewareInstances[$i][] = $value;
                                 $i++;
                             }
                         }
                     }
                 }
                 //get the request array
                 if (defined('PHPUNIT_BETASYNTAX_TESTSUITE') == true) {
                     $requestMethod = $requestMethod;
                 } else {
                     $requestMethod = $_SERVER['REQUEST_METHOD'];
                 }
                 //instantiate our request and response for Relay
                 $request = new Request($requestMethod, $requestUrl);
                 $response = new Response();
                 $queue = [];
                 //build the middleware queue
                 for ($i = 0; $i < count($middlewareInstances); $i++) {
                     $queue[] = $middlewareInstances[$i][1];
                 }
                 //setup the automatic resolver
                 $resolver = function ($class) {
                     return new $class();
                 };
                 //build the Relay builder class
                 $relayBuilder = new RelayBuilder($resolver);
                 $relay = $relayBuilder->newInstance($queue);
                 //run the middleware against our controller
                 $response = $relay($request, $response);
                 //finally if all our middleware passed on the response we can then run our intended controller action
                 if (!defined('PHPUNIT_BETASYNTAX_TESTSUITE') == true) {
                     $instance->{$method}($mm['params']);
                 }
                 $this->routeFound = true;
             }
         }
     }
     if (!$this->routeFound) {
         if (defined('PHPUNIT_BETASYNTAX_TESTSUITE') == true) {
             return true;
         } else {
             view('Errors/404.haml');
         }
     } else {
         return false;
     }
 }
Beispiel #9
0
 /**
  * Run the application.
  */
 public function run()
 {
     $this->boot();
     // If we haven't defined any routes
     // then don't do anything
     if (!$this->routes) {
         return;
     }
     $request = $this->container->get(ServerRequestInterface::class);
     $response = $this->container->get(ResponseInterface::class);
     $emitter = $this->container->get(SapiEmitter::class);
     // Build Relay factory
     $builder = new RelayBuilder(function ($callable) {
         return is_string($callable) ? $this->container->get($callable) : $callable;
     });
     // Process middlewares
     $middlewares = (array) $this->configuration->getMiddlewares();
     $relay = $builder->newInstance($middlewares);
     $response = $relay($request, $response);
     $emitter->emit($response);
 }
Beispiel #10
0
 public function applyMiddleware()
 {
     $relayBuilder = new RelayBuilder();
     $relay = $relayBuilder->newInstance($this->queues);
     $this->response = $relay($this->request, $this->response);
 }
Beispiel #11
0
 /**
  *
  */
 public function run()
 {
     $route = $this->getRoute();
     if ($route !== null) {
         $queue = array_merge($this->_middleware->getArrayCopy(), $route->getMiddleware());
         $queue[] = $route->getRequestHandler();
         $relayBuilder = new RelayBuilder();
         $relay = $relayBuilder->newInstance($queue);
         (new SapiEmitter())->emit($relay($this->_request, $this->_response));
         return true;
     }
     (new SapiEmitter())->emit($this->_response->withStatus(404));
 }