-
Notifications
You must be signed in to change notification settings - Fork 1.9k
perf: add Services::get() #8607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4f185b0
to
9ac1dda
Compare
system/Config/BaseService.php
Outdated
* | ||
* @return mixed Entry. | ||
*/ | ||
public static function get(string $key): mixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applicable review until override()
.
mixed
should be limited to object
, as the static array prop $instances
accepts object
as value. I don't think we need to accept anything to be set. Accepting mixed
may lead to future misuses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to ?object
.
system/Config/BaseService.php
Outdated
* Sets an entry. | ||
* | ||
* @param string $key Identifier of the entry. | ||
* @param mixed $value Normally an object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment before.
system/Config/BaseService.php
Outdated
public static function set(string $key, mixed $value): void | ||
{ | ||
if (isset(static::$instances[$key])) { | ||
throw new InvalidArgumentException('The Entry for "' . $key . '" is already set.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new InvalidArgumentException('The Entry for "' . $key . '" is already set.'); | |
throw new InvalidArgumentException('The entry for "' . $key . '" is already set.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
system/Config/BaseService.php
Outdated
* Overrides an existing entry. | ||
* | ||
* @param string $key Identifier of the entry. | ||
* @param mixed $value Normally an object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above
The more I think about this the more confused I am about the performance issue we're seeing. All methods are static and should be called before |
The current Services is not that simple. CodeIgniter4/app/Config/Services.php Line 20 in fd28a3f
and BaseService has only $ grep function system/Config/BaseService.php
public static function get(string $key): ?object
public static function set(string $key, object $value): void
public static function override(string $key, object $value): void
protected static function getSharedInstance(string $key, ...$params)
public static function autoloader(bool $getShared = true)
public static function locator(bool $getShared = true)
public static function __callStatic(string $name, array $arguments)
public static function serviceExists(string $name): ?string
public static function reset(bool $initAutoloader = true)
public static function resetSingle(string $name)
public static function injectMock(string $name, $mock)
protected static function buildServicesCache(): void |
Yeah, I guess the only ones that would hit directly would be in |
How do I work with the magic of services? The analyzer swears at the returned type. It looks sad. // some code
$this->request = service('request'); Errors: 3) tests/system/Honeypot/HoneypotTest.php:37
---------- begin diff ----------
@@ @@
/**
* @var CLIRequest|IncomingRequest
*/
- private $request;
+ private ?object $request = null;
private Response $response;
----------- end diff -----------
Applied rules:
* TypedPropertyFromAssignsRector |
Description
Services::get()
method to get the shared instance fast.service()
usesServices::get()
Checklist: