Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
namespace Ratchet\Http;
3
use Ratchet\ConnectionInterface;
4
use Psr\Http\Message\RequestInterface;
5
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
6
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
7
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
8
use GuzzleHttp\Psr7 as gPsr;
9
 
10
class Router implements HttpServerInterface {
11
    use CloseResponseTrait;
12
 
13
    /**
14
     * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface
15
     */
16
    protected $_matcher;
17
 
18
    private $_noopController;
19
 
20
    public function __construct(UrlMatcherInterface $matcher) {
21
        $this->_matcher = $matcher;
22
        $this->_noopController = new NoOpHttpServerController;
23
    }
24
 
25
    /**
26
     * {@inheritdoc}
27
     * @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface
28
     */
29
    public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
30
        if (null === $request) {
31
            throw new \UnexpectedValueException('$request can not be null');
32
        }
33
 
34
        $conn->controller = $this->_noopController;
35
 
36
        $uri = $request->getUri();
37
 
38
        $context = $this->_matcher->getContext();
39
        $context->setMethod($request->getMethod());
40
        $context->setHost($uri->getHost());
41
 
42
        try {
43
            $route = $this->_matcher->match($uri->getPath());
44
        } catch (MethodNotAllowedException $nae) {
45
            return $this->close($conn, 405, array('Allow' => $nae->getAllowedMethods()));
46
        } catch (ResourceNotFoundException $nfe) {
47
            return $this->close($conn, 404);
48
        }
49
 
50
        if (is_string($route['_controller']) && class_exists($route['_controller'])) {
51
            $route['_controller'] = new $route['_controller'];
52
        }
53
 
54
        if (!($route['_controller'] instanceof HttpServerInterface)) {
55
            throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface');
56
        }
57
 
58
        $parameters = [];
59
        foreach($route as $key => $value) {
60
            if ((is_string($key)) && ('_' !== substr($key, 0, 1))) {
61
                $parameters[$key] = $value;
62
            }
63
        }
64
        $parameters = array_merge($parameters, gPsr\parse_query($uri->getQuery() ?: ''));
65
 
66
        $request = $request->withUri($uri->withQuery(gPsr\build_query($parameters)));
67
 
68
        $conn->controller = $route['_controller'];
69
        $conn->controller->onOpen($conn, $request);
70
    }
71
 
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function onMessage(ConnectionInterface $from, $msg) {
76
        $from->controller->onMessage($from, $msg);
77
    }
78
 
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function onClose(ConnectionInterface $conn) {
83
        if (isset($conn->controller)) {
84
            $conn->controller->onClose($conn);
85
        }
86
    }
87
 
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function onError(ConnectionInterface $conn, \Exception $e) {
92
        if (isset($conn->controller)) {
93
            $conn->controller->onError($conn, $e);
94
        }
95
    }
96
}