Merge branch 'refs/heads/route_params'

This commit is contained in:
Chris Boden 2014-03-30 12:41:17 -04:00
commit 006a9573ce
2 changed files with 37 additions and 1 deletions

View File

@ -3,6 +3,7 @@ namespace Ratchet\Http;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response; use Guzzle\Http\Message\Response;
use Guzzle\Http\Url;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Exception\ResourceNotFoundException;
@ -46,6 +47,16 @@ class Router implements HttpServerInterface {
throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface');
} }
$parameters = array();
foreach($route as $key => $value) {
if ((is_string($key)) && ('_' !== substr($key, 0, 1))) {
$parameters[$key] = $value;
}
}
$url = Url::factory($request->getPath());
$url->setQuery($parameters);
$request->setUrl($url);
$conn->controller = $route['_controller']; $conn->controller = $route['_controller'];
$conn->controller->onOpen($conn, $request); $conn->controller->onOpen($conn, $request);
} }
@ -89,4 +100,4 @@ class Router implements HttpServerInterface {
$conn->send((string)$response); $conn->send((string)$response);
$conn->close(); $conn->close();
} }
} }

View File

@ -1,7 +1,9 @@
<?php <?php
namespace Ratchet\Http; namespace Ratchet\Http;
use Ratchet\Http\Router; use Ratchet\Http\Router;
use Ratchet\WebSocket\WsServerInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
/** /**
* @covers Ratchet\Http\Router * @covers Ratchet\Http\Router
@ -85,4 +87,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase {
$this->_router->onError($this->_conn, $e); $this->_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'), 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'));
$request->setUrl('ws://doesnt.matter/');
$router = new Router($this->_matcher);
$router->onOpen($conn, $request);
$this->assertEquals(array('foo' => 'bar', 'baz' => 'qux'), $request->getQuery()->getAll());
}
} }