Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9eff960

Browse files
committed
[HttpFoundation] deprecated finding deep items in Request and ParameterBag
1 parent ce3b8fd commit 9eff960

11 files changed

+157
-59
lines changed

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -99,54 +99,20 @@ public function add(array $parameters = array())
9999
* @throws \InvalidArgumentException
100100
*
101101
* @api
102+
*
103+
* @deprecated Finding deep items is deprecated since version 2.7, to be removed in 3.0.
102104
*/
103105
public function get($path, $default = null, $deep = false)
104106
{
105-
if (!$deep || false === $pos = strpos($path, '[')) {
106-
return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
107-
}
108-
109-
$root = substr($path, 0, $pos);
110-
if (!array_key_exists($root, $this->parameters)) {
111-
return $default;
112-
}
113-
114-
$value = $this->parameters[$root];
115-
$currentKey = null;
116-
for ($i = $pos, $c = strlen($path); $i < $c; $i++) {
117-
$char = $path[$i];
118-
119-
if ('[' === $char) {
120-
if (null !== $currentKey) {
121-
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
122-
}
123-
124-
$currentKey = '';
125-
} elseif (']' === $char) {
126-
if (null === $currentKey) {
127-
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
128-
}
129-
130-
if (!is_array($value) || !array_key_exists($currentKey, $value)) {
131-
return $default;
132-
}
133-
134-
$value = $value[$currentKey];
135-
$currentKey = null;
136-
} else {
137-
if (null === $currentKey) {
138-
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
139-
}
140-
141-
$currentKey .= $char;
142-
}
107+
if (true === $deep) {
108+
trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.7 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
143109
}
144110

145-
if (null !== $currentKey) {
146-
throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
111+
if (!$deep || false === $pos = strpos($path, '[')) {
112+
return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
147113
}
148114

149-
return $value;
115+
return ParameterBagUtils::getParameterWithPath($this, $path, $default);
150116
}
151117

152118
/**
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation;
13+
14+
/**
15+
* @internal
16+
*/
17+
final class ParameterBagUtils
18+
{
19+
/**
20+
* Returns a "parameter" value.
21+
*
22+
* Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
23+
*
24+
* @param ParameterBag $parameters The parameter bag
25+
* @param string $path The key
26+
* @param mixed $default The default value if the parameter key does not exist
27+
*
28+
* @return mixed
29+
*
30+
* @throws \InvalidArgumentException when the path is malformed
31+
*/
32+
public static function getParameterWithPath(ParameterBag $parameters, $path, $default = null)
33+
{
34+
if (false === $pos = strpos($path, '[')) {
35+
return $parameters->get($path, $default);
36+
}
37+
38+
$root = substr($path, 0, $pos);
39+
40+
if (null === $value = $parameters->get($root)) {
41+
return $default;
42+
}
43+
44+
return self::parseParameterPath($path, $pos, $value, $default);
45+
}
46+
47+
/**
48+
* Returns a request "parameter" value.
49+
*
50+
* Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
51+
*
52+
* @param Request $request The request
53+
* @param string $path The key
54+
*
55+
* @return mixed
56+
*
57+
* @throws \InvalidArgumentException when the path is malformed
58+
*/
59+
public static function getRequestParameterWithPath(Request $request, $path)
60+
{
61+
if (false === $pos = strpos($path, '[')) {
62+
return $request->get($path);
63+
}
64+
65+
$root = substr($path, 0, $pos);
66+
67+
if (null === $value = $request->get($root)) {
68+
return;
69+
}
70+
71+
return self::parseParameterPath($path, $pos, $value);
72+
}
73+
74+
private static function parseParameterPath($path, $pos, $value, $default = null)
75+
{
76+
$currentKey = null;
77+
for ($i = $pos, $c = strlen($path); $i < $c; $i++) {
78+
$char = $path[$i];
79+
80+
if ('[' === $char) {
81+
if (null !== $currentKey) {
82+
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
83+
}
84+
85+
$currentKey = '';
86+
} elseif (']' === $char) {
87+
if (null === $currentKey) {
88+
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
89+
}
90+
91+
if (!is_array($value) || !array_key_exists($currentKey, $value)) {
92+
return $default;
93+
}
94+
95+
$value = $value[$currentKey];
96+
$currentKey = null;
97+
} else {
98+
if (null === $currentKey) {
99+
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
100+
}
101+
102+
$currentKey .= $char;
103+
}
104+
}
105+
106+
if (null !== $currentKey) {
107+
throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
108+
}
109+
110+
return $value;
111+
}
112+
}

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,15 @@ public static function getHttpMethodParameterOverride()
743743
* @param bool $deep is parameter deep in multidimensional array
744744
*
745745
* @return mixed
746+
*
747+
* @deprecated Finding deep items is deprecated since version 2.7, to be removed in 3.0.
746748
*/
747749
public function get($key, $default = null, $deep = false)
748750
{
751+
if (true === $deep) {
752+
trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.7 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
753+
}
754+
749755
if ($this !== $result = $this->query->get($key, $this, $deep)) {
750756
return $result;
751757
}

src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ public function testGetDoesNotUseDeepByDefault()
8686
}
8787

8888
/**
89+
* @group legacy
8990
* @dataProvider getInvalidPaths
9091
* @expectedException \InvalidArgumentException
9192
*/
92-
public function testGetDeepWithInvalidPaths($path)
93+
public function testLegacyGetDeepWithInvalidPaths($path)
9394
{
95+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
96+
9497
$bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
9598

9699
$bag->get($path, null, true);
@@ -106,8 +109,13 @@ public function getInvalidPaths()
106109
);
107110
}
108111

109-
public function testGetDeep()
112+
/**
113+
* @group legacy
114+
*/
115+
public function testLegacyGetDeep()
110116
{
117+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
118+
111119
$bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
112120

113121
$this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\Authentication;
1313

14+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\HttpKernelInterface;
1617
use Psr\Log\LoggerInterface;
@@ -82,7 +83,7 @@ public function setOptions(array $options)
8283
*/
8384
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
8485
{
85-
if ($failureUrl = $request->get($this->options['failure_path_parameter'], null, true)) {
86+
if ($failureUrl = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['failure_path_parameter'])) {
8687
$this->options['failure_path'] = $failureUrl;
8788
}
8889

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\Authentication;
1313

14+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1415
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\Security\Http\HttpUtils;
@@ -108,7 +109,7 @@ protected function determineTargetUrl(Request $request)
108109
return $this->options['default_target_path'];
109110
}
110111

111-
if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
112+
if ($targetUrl = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['target_path_parameter'])) {
112113
return $targetUrl;
113114
}
114115

src/Symfony/Component/Security/Http/Firewall/LogoutListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
1515
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
16+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -98,7 +99,7 @@ public function handle(GetResponseEvent $event)
9899
}
99100

100101
if (null !== $this->csrfTokenManager) {
101-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
102+
$csrfToken = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['csrf_parameter']);
102103

103104
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
104105
throw new LogoutException('Invalid CSRF token.');

src/Symfony/Component/Security/Http/Firewall/SimpleFormAuthenticationListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1515
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
1616
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
17+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
1920
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
@@ -101,19 +102,19 @@ protected function requiresAuthentication(Request $request)
101102
protected function attemptAuthentication(Request $request)
102103
{
103104
if (null !== $this->csrfTokenManager) {
104-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
105+
$csrfToken = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['csrf_parameter']);
105106

106107
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
107108
throw new InvalidCsrfTokenException('Invalid CSRF token.');
108109
}
109110
}
110111

111112
if ($this->options['post_only']) {
112-
$username = trim($request->request->get($this->options['username_parameter'], null, true));
113-
$password = $request->request->get($this->options['password_parameter'], null, true);
113+
$username = trim(ParameterBagUtils::getParameterWithPath($request->request, $this->options['username_parameter']));
114+
$password = ParameterBagUtils::getParameterWithPath($request->request, $this->options['password_parameter']);
114115
} else {
115-
$username = trim($request->get($this->options['username_parameter'], null, true));
116-
$password = $request->get($this->options['password_parameter'], null, true);
116+
$username = trim(ParameterBagUtils::getRequestParameterWithPath($request, $this->options['username_parameter']));
117+
$password = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['password_parameter']);
117118
}
118119

119120
$request->getSession()->set(Security::LAST_USERNAME, $username);

src/Symfony/Component/Security/Http/Firewall/UsernamePasswordFormAuthenticationListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
2929
use Symfony\Component\Security\Core\Security;
3030
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31+
use Symfony\Component\Security\Http\Util\ParameterBagUtils;
3132

3233
/**
3334
* UsernamePasswordFormAuthenticationListener is the default implementation of
@@ -76,19 +77,19 @@ protected function requiresAuthentication(Request $request)
7677
protected function attemptAuthentication(Request $request)
7778
{
7879
if (null !== $this->csrfTokenManager) {
79-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
80+
$csrfToken = ParameterBagUtils::getDeepParameter($request, $this->options['csrf_parameter']);
8081

8182
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
8283
throw new InvalidCsrfTokenException('Invalid CSRF token.');
8384
}
8485
}
8586

8687
if ($this->options['post_only']) {
87-
$username = trim($request->request->get($this->options['username_parameter'], null, true));
88-
$password = $request->request->get($this->options['password_parameter'], null, true);
88+
$username = trim(ParameterBagUtils::getParameterWithPath($request, $this->options['username_parameter']));
89+
$password = ParameterBagUtils::getParameterWithPath($request, $this->options['password_parameter']);
8990
} else {
90-
$username = trim($request->get($this->options['username_parameter'], null, true));
91-
$password = $request->get($this->options['password_parameter'], null, true);
91+
$username = trim(ParameterBagUtils::getDeepParameter($request, $this->options['username_parameter']));
92+
$password = ParameterBagUtils::getDeepParameter($request, $this->options['password_parameter']);
9293
}
9394

9495
$request->getSession()->set(Security::LAST_USERNAME, $username);

src/Symfony/Component/Security/Http/RememberMe/AbstractRememberMeServices.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\RememberMe;
1313

14+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1415
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1516
use Symfony\Component\Security\Core\User\UserInterface;
1617
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
@@ -301,7 +302,7 @@ protected function isRememberMeRequested(Request $request)
301302
return true;
302303
}
303304

304-
$parameter = $request->get($this->options['remember_me_parameter'], null, true);
305+
$parameter = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['remember_me_parameter']);
305306

306307
if (null === $parameter && null !== $this->logger) {
307308
$this->logger->debug('Did not send remember-me cookie.', array('parameter' => $this->options['remember_me_parameter']));

src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testFailurePathCanBeOverwritten()
145145
public function testFailurePathCanBeOverwrittenWithRequest()
146146
{
147147
$this->request->expects($this->once())
148-
->method('get')->with('_failure_path', null, true)
148+
->method('get')->with('_failure_path', null, false)
149149
->will($this->returnValue('/auth/login'));
150150

151151
$this->httpUtils->expects($this->once())
@@ -160,7 +160,7 @@ public function testFailurePathParameterCanBeOverwritten()
160160
$options = array('failure_path_parameter' => '_my_failure_path');
161161

162162
$this->request->expects($this->once())
163-
->method('get')->with('_my_failure_path', null, true)
163+
->method('get')->with('_my_failure_path', null, false)
164164
->will($this->returnValue('/auth/login'));
165165

166166
$this->httpUtils->expects($this->once())

0 commit comments

Comments
 (0)