From 74fb2a691bd9b688e1fe027260c2bf2737fce697 Mon Sep 17 00:00:00 2001 From: Vincent Dieltiens Date: Sat, 9 Nov 2013 01:02:01 +0100 Subject: [PATCH 1/4] [Router] Update the Url QueryString of the request object with the parameters return by the UrlMatcher to let the developer access thoses parameters --- src/Ratchet/Http/Router.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index 8e6611e..4596af1 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -3,6 +3,7 @@ namespace Ratchet\Http; use Ratchet\ConnectionInterface; use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\Response; +use Guzzle\Http\Url; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; @@ -45,6 +46,16 @@ class Router implements HttpServerInterface { if (!($route['_controller'] instanceof HttpServerInterface)) { throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); } + + $parameters = array(); + foreach($route as $key => $value) { + if (!in_array($key, array('_controller', '_route'))) { + $parameters[$key] = $value; + } + } + $url = Url::factory($request->getPath()); + $url->setQuery($parameters); + $request->setUrl($url); $conn->controller = $route['_controller']; $conn->controller->onOpen($conn, $request); From 27d3939d309e94bbb2b392c591ac747ac0b3cda6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurynas=20Ver=C5=BEukauskas?= Date: Tue, 18 Mar 2014 22:18:50 +0200 Subject: [PATCH 2/4] Test added --- tests/unit/Http/RouterTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/unit/Http/RouterTest.php b/tests/unit/Http/RouterTest.php index 8656dd3..56471ce 100644 --- a/tests/unit/Http/RouterTest.php +++ b/tests/unit/Http/RouterTest.php @@ -1,7 +1,9 @@ _router->onError($this->_conn, $e); } + + public function testRouterGeneratesRouteParameters() + { + /** @var $controller WsServerInterface */ + $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock(); + /** @var $matcher UrlMatcherInterface */ + $this->_matcher->expects($this->any())->method('match')->will( + $this->returnValue(array('_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux')) + ); + $conn = $this->getMock('Ratchet\Mock\Connection'); + + $request = $this->getMock('Guzzle\Http\Message\Request', array('getPath'), ['GET', 'ws://random.url'], '', false); + $request->expects($this->any())->method('getPath')->will($this->returnValue('ws://doesnt.matter/')); + + $request->setHeaderFactory($this->getMock('Guzzle\Http\Message\Header\HeaderFactoryInterface')); + $request->setUrl('ws://doesnt.matter/'); + + $router = new Router($this->_matcher); + + $router->onOpen($conn, $request); + + $this->assertEquals(array('foo' => 'bar', 'baz' => 'qux'), $request->getQuery()->getAll()); + } } From 343ecdfa0fb8187f95ad4cd6fb3eff06e4b07ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurynas=20Ver=C5=BEukauskas?= Date: Tue, 18 Mar 2014 22:23:36 +0200 Subject: [PATCH 3/4] Filter all keys starting with underscore --- src/Ratchet/Http/Router.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index 4596af1..817f6a8 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -46,10 +46,10 @@ class Router implements HttpServerInterface { if (!($route['_controller'] instanceof HttpServerInterface)) { throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); } - + $parameters = array(); foreach($route as $key => $value) { - if (!in_array($key, array('_controller', '_route'))) { + if ((is_string($key)) && ('_' !== substr($key, 0, 1))) { $parameters[$key] = $value; } } @@ -100,4 +100,4 @@ class Router implements HttpServerInterface { $conn->send((string)$response); $conn->close(); } -} \ No newline at end of file +} From b7b0a6d2f4b84cd636cae91ae98cad87b15c1d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurynas=20Ver=C5=BEukauskas?= Date: Wed, 19 Mar 2014 18:28:28 +0200 Subject: [PATCH 4/4] PHP5.3 fix --- tests/unit/Http/RouterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Http/RouterTest.php b/tests/unit/Http/RouterTest.php index 56471ce..412e774 100644 --- a/tests/unit/Http/RouterTest.php +++ b/tests/unit/Http/RouterTest.php @@ -98,7 +98,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase { ); $conn = $this->getMock('Ratchet\Mock\Connection'); - $request = $this->getMock('Guzzle\Http\Message\Request', array('getPath'), ['GET', 'ws://random.url'], '', false); + $request = $this->getMock('Guzzle\Http\Message\Request', array('getPath'), array('GET', 'ws://random.url'), '', false); $request->expects($this->any())->method('getPath')->will($this->returnValue('ws://doesnt.matter/')); $request->setHeaderFactory($this->getMock('Guzzle\Http\Message\Header\HeaderFactoryInterface'));