Skip to content

Commit

Permalink
Deprecate the array form of Router::setRequestInfo()
Browse files Browse the repository at this point in the history
This was missed in 3.5 and is a form that should not be used and we can
stop using now.
  • Loading branch information
markstory committed Nov 16, 2017
1 parent dee7331 commit 23f1f08
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
14 changes: 12 additions & 2 deletions src/Routing/Router.php
Expand Up @@ -395,21 +395,31 @@ public static function parseRequest(ServerRequestInterface $request)
*
* @param \Cake\Http\ServerRequest|array $request Parameters and path information or a Cake\Http\ServerRequest object.
* @return void
* @deprecatd 3.6.0 Support for arrays will be removed in 4.0.0
*/
public static function setRequestInfo($request)
{
if ($request instanceof ServerRequest) {
static::pushRequest($request);
} else {
deprecationWarning(
'Passing an array into Router::setRequestInfo() is deprecated. ' .
'Pass an instance of ServerRequest instead.'
);

$requestData = $request;
$requestData += [[], []];
$requestData[0] += [
'controller' => false,
'action' => false,
'plugin' => null
];
$request = new ServerRequest();
$request->addParams($requestData[0])->addPaths($requestData[1]);
$request = new ServerRequest([
'params' => $requestData[0],
'url' => isset($requestData[1]['here']) ? $requestData[1]['here'] : '/',
'base' => isset($requestData[1]['base']) ? $requestData[1]['base'] : '',
'webroot' => isset($requestData[1]['webroot']) ? $requestData[1]['webroot'] : '/',
]);
static::pushRequest($request);
}
}
Expand Down
45 changes: 27 additions & 18 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -1291,7 +1291,7 @@ public function testUrlGenerationWithUrlFilter()
$calledCount = 0;
Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
$calledCount++;
$url['lang'] = $request->lang;
$url['lang'] = $request->getParam('lang');

return $url;
});
Expand Down Expand Up @@ -2849,27 +2849,36 @@ public function testReverseToArrayRequestQuery()
/**
* test that setRequestInfo can accept arrays and turn that into a Request object.
*
* @group deprecated
* @return void
*/
public function testSetRequestInfoLegacy()
{
Router::setRequestInfo([
[
'plugin' => null, 'controller' => 'images', 'action' => 'index',
'url' => ['url' => 'protected/images/index']
],
[
'base' => '',
'here' => '/protected/images/index',
'webroot' => '/',
]
]);
$result = Router::getRequest();
$this->assertEquals('images', $result->controller);
$this->assertEquals('index', $result->action);
$this->assertEquals('', $result->base);
$this->assertEquals('/protected/images/index', $result->here);
$this->assertEquals('/', $result->webroot);
$this->deprecated(function () {
Router::setRequestInfo([
[
'plugin' => null, 'controller' => 'images', 'action' => 'index',
'url' => ['url' => 'protected/images/index']
],
[
'base' => '',
'here' => '/protected/images/index',
'webroot' => '/',
]
]);
$result = Router::getRequest();
$this->assertEquals('images', $result->getParam('controller'));
$this->assertEquals('index', $result->getParam('action'));

$this->assertEquals('', $result->getAttribute('base'));
$this->assertEquals('', $result->base);

$this->assertEquals('/protected/images/index', $result->getRequestTarget());
$this->assertEquals('/protected/images/index', $result->here);

$this->assertEquals('/', $result->getAttribute('webroot'));
$this->assertEquals('/', $result->webroot);
});
}

/**
Expand Down

0 comments on commit 23f1f08

Please sign in to comment.