Ampache PHP Cross Reference Groupware Applications

Source: /modules/Ratchet/Http/Router.php - 92 lines - 2841 bytes - Summary - Text - Print

   1  <?php
   2  namespace Ratchet\Http;
   3  use Ratchet\ConnectionInterface;
   4  use Guzzle\Http\Message\RequestInterface;
   5  use Guzzle\Http\Message\Response;
   6  use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
   7  use Symfony\Component\Routing\Exception\MethodNotAllowedException;
   8  use Symfony\Component\Routing\Exception\ResourceNotFoundException;
   9  
  10  class Router implements HttpServerInterface {
  11      /**
  12       * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface
  13       */
  14      protected $_matcher;
  15  
  16      public function __construct(UrlMatcherInterface $matcher) {
  17          $this->_matcher = $matcher;
  18      }
  19  
  20      /**
  21       * {@inheritdoc}
  22       * @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface
  23       */
  24      public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
  25          if (null === $request) {
  26              throw new \UnexpectedValueException('$request can not be null');
  27          }
  28  
  29          $context = $this->_matcher->getContext();
  30          $context->setMethod($request->getMethod());
  31          $context->setHost($request->getHost());
  32  
  33          try {
  34              $route = $this->_matcher->match($request->getPath());
  35          } catch (MethodNotAllowedException $nae) {
  36              return $this->close($conn, 403);
  37          } catch (ResourceNotFoundException $nfe) {
  38              return $this->close($conn, 404);
  39          }
  40  
  41          if (is_string($route['_controller']) && class_exists($route['_controller'])) {
  42              $route['_controller'] = new $route['_controller'];
  43          }
  44  
  45          if (!($route['_controller'] instanceof HttpServerInterface)) {
  46              throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface');
  47          }
  48  
  49          $conn->controller = $route['_controller'];
  50          $conn->controller->onOpen($conn, $request);
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      function onMessage(ConnectionInterface $from, $msg) {
  57          $from->controller->onMessage($from, $msg);
  58      }
  59  
  60      /**
  61       * {@inheritdoc}
  62       */
  63      function onClose(ConnectionInterface $conn) {
  64          if (isset($conn->controller)) {
  65              $conn->controller->onClose($conn);
  66          }
  67      }
  68  
  69      /**
  70       * {@inheritdoc}
  71       */
  72      function onError(ConnectionInterface $conn, \Exception $e) {
  73          if (isset($conn->controller)) {
  74              $conn->controller->onError($conn, $e);
  75          }
  76      }
  77  
  78      /**
  79       * Close a connection with an HTTP response
  80       * @param \Ratchet\ConnectionInterface $conn
  81       * @param int                          $code HTTP status code
  82       * @return null
  83       */
  84      protected function close(ConnectionInterface $conn, $code = 400) {
  85          $response = new Response($code, array(
  86              'X-Powered-By' => \Ratchet\VERSION
  87          ));
  88  
  89          $conn->send((string)$response);
  90          $conn->close();
  91      }
  92  }

title

Description

title

Description

title

Description

title

title

Body