This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpKernel/RequestStack.php

87 lines
1.7 KiB
PHP
Raw Normal View History

2013-04-18 09:33:03 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
/**
* Request stack that controls the lifecycle of requests.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class RequestStack
{
/**
* @var Request[]
*/
private $requests = array();
public function push(Request $request)
{
$this->requests[] = $request;
}
/**
2013-08-31 21:10:38 +01:00
* Pops the current request from the stack.
2013-04-18 09:33:03 +01:00
*
* This operation lets the current request go out of scope.
*
* @return Request
*/
public function pop()
{
2013-08-31 21:10:38 +01:00
if (!$this->requests) {
throw new \LogicException('Unable to pop a Request as the stack is already empty.');
}
2013-04-18 09:33:03 +01:00
return array_pop($this->requests);
}
/**
* @return Request|null
*/
public function getCurrentRequest()
{
return end($this->requests) ?: null;
}
/**
* @return Request|null
*/
public function getMasterRequest()
{
if (!$this->requests) {
return null;
}
return $this->requests[0];
}
/**
2013-08-31 21:10:38 +01:00
* Returns the parent request of the current.
2013-04-18 09:33:03 +01:00
*
2013-08-31 21:10:38 +01:00
* If current Request is the master request, it returns null.
2013-04-18 09:33:03 +01:00
*
2013-08-31 21:10:38 +01:00
* @return Request|null
2013-04-18 09:33:03 +01:00
*/
public function getParentRequest()
{
$pos = count($this->requests) - 2;
if (!isset($this->requests[$pos])) {
return null;
}
return $this->requests[$pos];
}
}