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

Skip to content

Commit 44df302

Browse files
committed
[BrowserKit] Emulate back/forward browser navigation
1 parent 67fba7d commit 44df302

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ abstract class Client
3838
protected $crawler;
3939
protected $insulated = false;
4040
protected $redirect;
41+
protected $redirects = array();
4142
protected $followRedirects = true;
43+
protected $browserNavigation = false;
4244

4345
private $maxRedirects = -1;
4446
private $redirectCount = 0;
@@ -68,6 +70,16 @@ public function followRedirects($followRedirect = true)
6870
$this->followRedirects = (bool) $followRedirect;
6971
}
7072

73+
/**
74+
* Sets whether to emulate back/forward browser navigation (skip redirects).
75+
*
76+
* @param bool $browserNavigation Whether to emulate browser navigation
77+
*/
78+
public function browserNavigation($browserNavigation = true)
79+
{
80+
$this->browserNavigation = (bool) $browserNavigation;
81+
}
82+
7183
/**
7284
* Sets the maximum number of requests that crawler can follow.
7385
*
@@ -308,6 +320,8 @@ public function request($method, $uri, array $parameters = array(), array $files
308320
}
309321

310322
if ($this->followRedirects && $this->redirect) {
323+
$this->redirects[] = spl_object_hash($this->history->current());
324+
311325
return $this->crawler = $this->followRedirect();
312326
}
313327

@@ -410,7 +424,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
410424
*/
411425
public function back()
412426
{
413-
return $this->requestFromRequest($this->history->back(), false);
427+
do {
428+
$request = $this->history->back();
429+
} while ($this->browserNavigation && in_array(spl_object_hash($request), $this->redirects));
430+
431+
return $this->requestFromRequest($request, false);
414432
}
415433

416434
/**
@@ -420,7 +438,11 @@ public function back()
420438
*/
421439
public function forward()
422440
{
423-
return $this->requestFromRequest($this->history->forward(), false);
441+
do {
442+
$request = $this->history->forward();
443+
} while ($this->browserNavigation && in_array(spl_object_hash($request), $this->redirects));
444+
445+
return $this->requestFromRequest($request, false);
424446
}
425447

426448
/**

0 commit comments

Comments
 (0)