Skip to content

Commit

Permalink
Merge pull request #11613 from creative-commoners/pulls/6.0/remove-ct…
Browse files Browse the repository at this point in the history
…rl-curr

API Do not raise user_error from Controller::curr()
  • Loading branch information
GuySartorelli authored Feb 16, 2025
2 parents 2c8fbec + 32598bb commit 8bb82b3
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 58 deletions.
24 changes: 2 additions & 22 deletions src/Control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,30 +527,10 @@ public function render($params = null): DBHTMLText

/**
* Returns the current controller.
*
* @return Controller
*/
public static function curr()
{
if (Controller::$controller_stack) {
return Controller::$controller_stack[0];
}
// This user_error() will be removed in the next major version of Silverstripe CMS
user_error("No current controller available", E_USER_WARNING);
return null;
}

/**
* Tests whether we have a currently active controller or not. True if there is at least 1
* controller in the stack.
*
* @return bool
* @deprecated 5.4.0 Will be removed without equivalent functionality to replace it
*/
public static function has_curr()
public static function curr(): ?Controller
{
Deprecation::noticeWithNoReplacment('5.4.0');
return Controller::$controller_stack ? true : false;
return Controller::$controller_stack[0] ?? null;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Control/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ public static function validateSameSite(string $sameSite): void
*/
private static function getRequest(): ?HTTPRequest
{
$request = null;
if (Controller::has_curr()) {
$request = Controller::curr()->getRequest();
}
$request = Controller::curr()?->getRequest();
// NullHTTPRequest always has a scheme of http - set to null so we can fallback on default_base_url
return ($request instanceof NullHTTPRequest) ? null : $request;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dev/SapphireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ protected function tearDown(): void
// Note: Ideally a clean Controller should be created for each test.
// Now all tests executed in a batch share the same controller.
if (class_exists(Controller::class)) {
$controller = Controller::has_curr() ? Controller::curr() : null;
$controller = Controller::curr();
if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) {
$response->setStatusCode(200);
$response->removeHeader('Location');
Expand Down
4 changes: 2 additions & 2 deletions src/Dev/TestSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public function __destruct()
{
// Shift off anything else that's on the stack. This can happen if something throws
// an exception that causes a premature TestSession::__destruct() call
while (Controller::has_curr() && Controller::curr() !== $this->controller) {
while (Controller::curr() && Controller::curr() !== $this->controller) {
Controller::curr()->popCurrent();
}

if (Controller::has_curr()) {
if (Controller::curr()) {
$this->controller->popCurrent();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected function getRequest()
return $controller->getRequest();
}
// Fall back to current controller
if (Controller::has_curr() && !(Controller::curr()->getRequest() instanceof NullHTTPRequest)) {
if (Controller::curr() && !(Controller::curr()->getRequest() instanceof NullHTTPRequest)) {
return Controller::curr()->getRequest();
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private function initState(): void
private function addStateFromRequest(): void
{
$request = $this->getRequest();
if (($request instanceof NullHTTPRequest) && Controller::has_curr()) {
if (($request instanceof NullHTTPRequest) && Controller::curr()) {
$request = Controller::curr()->getRequest();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Logging/ErrorOutputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function write(LogRecord $record): void
return;
}

if (Controller::has_curr()) {
if (Controller::curr()) {
$response = Controller::curr()->getResponse();
} else {
$response = new HTTPResponse();
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/Hierarchy/Hierarchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@ public function allowedChildren(): array
*/
public function showingCMSTree()
{
if (!Controller::has_curr() || !class_exists(LeftAndMain::class)) {
$controller = Controller::curr();
if (!$controller || !class_exists(LeftAndMain::class)) {
return false;
}
$controller = Controller::curr();
return $controller instanceof LeftAndMain
&& in_array($controller->getAction(), ["treeview", "listview", "getsubtree"]);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public static function permissionFailure($controller = null, $messageSet = null)
];
};

if (!$controller && Controller::has_curr()) {
if (!$controller && Controller::curr()) {
$controller = Controller::curr();
}

Expand Down Expand Up @@ -526,8 +526,9 @@ public function getRequest()
return $request;
}

if (Controller::has_curr() && Controller::curr() !== $this) {
return Controller::curr()->getRequest();
$controller = Controller::curr();
if ($controller && $controller !== $this) {
return $controller->getRequest();
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Security/SecurityToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function getSession()
$injector = Injector::inst();
if ($injector->has(HTTPRequest::class)) {
return $injector->get(HTTPRequest::class)->getSession();
} elseif (Controller::has_curr()) {
} elseif (Controller::curr()) {
return Controller::curr()->getRequest()->getSession();
}
throw new Exception('No HTTPRequest object or controller available yet!');
Expand Down
5 changes: 3 additions & 2 deletions tests/php/Control/ControllerTest/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/php/Control/DirectorTest/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/php/Control/RequestHandlingTest/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public function __construct()
{
$this->failover = new ControllerFailover();
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/php/Forms/EmailFieldTest/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/php/Forms/FormFactoryTest/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class TestController extends Controller
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class ControllerWithSpecialSubmittedValueFields extends Controller implements Te
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/php/Forms/FormTest/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class TestController extends Controller implements TestOnly
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
$controller = Controller::curr();
if ($controller) {
$this->setRequest($controller->getRequest());
}
}

Expand Down

0 comments on commit 8bb82b3

Please sign in to comment.