3

Silverstripe Version: 4.2

I have a custom AssetAdapter that makes some changes to the filesystem based on the current request. I am using injector to get the request:

$request = Injector::inst()->get(HTTPRequest::class);

For the most part this works fine, but in a couple of isolated instances, I get the error:

ERROR [Emergency]: Uncaught ArgumentCountError: Too few arguments to function SilverStripe\Control\HTTPRequest::__construct(), 0 passed and at least 2 expected
IN GET /ecms-client/public/markseen
Line 157 in /project/path/vendor/silverstripe/framework/src/Control/HTTPRequest.php

This appears to be an issue/conflict with GraphQL and Assets (GraphQL doesn't always seem to have the current request available). I was wondering if there was a way to check if the current HTTPRequest is available/setup before trying to get it via Injector?

scrowler
  • 24,273
  • 9
  • 60
  • 92
PsychoMo
  • 699
  • 5
  • 17

1 Answers1

2

Yeah, Injector::inst()->get() will create a new instance if one doesn't exist yet. Since HTTPRequest requires two arguments during construction, you will get errors.

You can check if one exists using ->has():

if (Injector::inst()->has(HTTPRequest::class)) {
    $request = Injector::inst()->get(HTTPRequest::class);
    // do something
}
scrowler
  • 24,273
  • 9
  • 60
  • 92