Skip to content

Commit a9fa3d0

Browse files
authored
Merge pull request #9248 from neznaika0/fix-call-as-service
perf: Improve call as `service()`
2 parents 3346b6f + ea38947 commit a9fa3d0

File tree

99 files changed

+336
-420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+336
-420
lines changed

admin/starter/tests/session/ExampleSessionTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use CodeIgniter\Test\CIUnitTestCase;
4-
use Config\Services;
54

65
/**
76
* @internal
@@ -10,7 +9,7 @@ final class ExampleSessionTest extends CIUnitTestCase
109
{
1110
public function testSessionSimple(): void
1211
{
13-
$session = Services::session();
12+
$session = service('session');
1413

1514
$session->set('logged_in', 123);
1615
$this->assertSame(123, $session->get('logged_in'));

admin/starter/tests/unit/HealthTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use CodeIgniter\Test\CIUnitTestCase;
44
use Config\App;
5-
use Config\Services;
65
use Tests\Support\Libraries\ConfigReader;
76

87
/**
@@ -17,7 +16,7 @@ public function testIsDefinedAppPath(): void
1716

1817
public function testBaseUrlHasBeenSet(): void
1918
{
20-
$validation = Services::validation();
19+
$validation = service('validation');
2120

2221
$env = false;
2322

app/Config/Events.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
*/
4545
if (CI_DEBUG && ! is_cli()) {
4646
Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
47-
Services::toolbar()->respond();
47+
service('toolbar')->respond();
4848
// Hot Reload route - for framework use on the hot reloader.
4949
if (ENVIRONMENT === 'development') {
50-
Services::routes()->get('__hot-reload', static function (): void {
50+
service('routes')->get('__hot-reload', static function (): void {
5151
(new HotReloader())->run();
5252
});
5353
}

app/Config/Format.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ class Format extends BaseConfig
7272
*/
7373
public function getFormatter(string $mime)
7474
{
75-
return Services::format()->getFormatter($mime);
75+
return service('format')->getFormatter($mime);
7676
}
7777
}

app/Controllers/BaseController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public function initController(RequestInterface $request, ResponseInterface $res
5353

5454
// Preload any models, libraries, etc, here.
5555

56-
// E.g.: $this->session = \Config\Services::session();
56+
// E.g.: $this->session = service('session');
5757
}
5858
}

app/Views/errors/html/error_exception.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
use CodeIgniter\HTTP\Header;
3-
use Config\Services;
43
use CodeIgniter\CodeIgniter;
54

65
$errorId = uniqid('error', true);
@@ -225,7 +224,7 @@
225224

226225
<!-- Request -->
227226
<div class="content" id="request">
228-
<?php $request = Services::request(); ?>
227+
<?php $request = service('request'); ?>
229228

230229
<table>
231230
<tbody>
@@ -343,7 +342,7 @@
343342

344343
<!-- Response -->
345344
<?php
346-
$response = Services::response();
345+
$response = service('response');
347346
$response->setStatusCode(http_response_code());
348347
?>
349348
<div class="content" id="response">

phpstan-baseline.php

-18
Original file line numberDiff line numberDiff line change
@@ -18361,12 +18361,6 @@
1836118361
'count' => 1,
1836218362
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
1836318363
];
18364-
$ignoreErrors[] = [
18365-
// identifier: missingType.iterableValue
18366-
'message' => '#^Method class@anonymous/tests/system/View/ParserTest\\.php\\:340\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#',
18367-
'count' => 1,
18368-
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
18369-
];
1837018364
$ignoreErrors[] = [
1837118365
// identifier: argument.type
1837218366
'message' => '#^Parameter \\#2 \\$context of method CodeIgniter\\\\View\\\\Parser\\:\\:setData\\(\\) expects \'attr\'\\|\'css\'\\|\'html\'\\|\'js\'\\|\'raw\'\\|\'url\'\\|null, \'unknown\' given\\.$#',
@@ -18379,18 +18373,6 @@
1837918373
'count' => 1,
1838018374
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
1838118375
];
18382-
$ignoreErrors[] = [
18383-
// identifier: missingType.property
18384-
'message' => '#^Property class@anonymous/tests/system/View/ParserTest\\.php\\:340\\:\\:\\$bar has no type specified\\.$#',
18385-
'count' => 1,
18386-
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
18387-
];
18388-
$ignoreErrors[] = [
18389-
// identifier: missingType.property
18390-
'message' => '#^Property class@anonymous/tests/system/View/ParserTest\\.php\\:340\\:\\:\\$foo has no type specified\\.$#',
18391-
'count' => 1,
18392-
'path' => __DIR__ . '/tests/system/View/ParserTest.php',
18393-
];
1839418376
$ignoreErrors[] = [
1839518377
// identifier: method.notFound
1839618378
'message' => '#^Call to an undefined method CodeIgniter\\\\View\\\\Table\\:\\:compileTemplate\\(\\)\\.$#',

system/Autoloader/Autoloader.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Config\Autoload;
2020
use Config\Kint as KintConfig;
2121
use Config\Modules;
22-
use Config\Services;
2322
use InvalidArgumentException;
2423
use Kint;
2524
use Kint\Renderer\CliRenderer;
@@ -537,7 +536,7 @@ private function configureKint(): void
537536
Kint::$plugins = $config->plugins;
538537
}
539538

540-
$csp = Services::csp();
539+
$csp = service('csp');
541540
if ($csp->enabled()) {
542541
RichRenderer::$js_nonce = $csp->getScriptNonce();
543542
RichRenderer::$css_nonce = $csp->getStyleNonce();

system/BaseModel.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use CodeIgniter\Pager\Pager;
2727
use CodeIgniter\Validation\ValidationInterface;
2828
use Config\Feature;
29-
use Config\Services;
3029
use InvalidArgumentException;
3130
use ReflectionClass;
3231
use ReflectionException;
@@ -1609,7 +1608,7 @@ public function getValidationRules(array $options = []): array
16091608
protected function ensureValidation(): void
16101609
{
16111610
if ($this->validation === null) {
1612-
$this->validation = Services::validation(null, false);
1611+
$this->validation = service('validation', null, false);
16131612
}
16141613
}
16151614

system/Boot.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ protected static function loadAutoloader(): void
246246

247247
protected static function autoloadHelpers(): void
248248
{
249-
Services::autoloader()->loadHelpers();
249+
service('autoloader')->loadHelpers();
250250
}
251251

252252
protected static function setExceptionHandler(): void
253253
{
254-
Services::exceptions()->initialize();
254+
service('exceptions')->initialize();
255255
}
256256

257257
protected static function checkMissingExtensions(): void
@@ -290,7 +290,7 @@ protected static function checkMissingExtensions(): void
290290

291291
protected static function initializeKint(): void
292292
{
293-
Services::autoloader()->initializeKint(CI_DEBUG);
293+
service('autoloader')->initializeKint(CI_DEBUG);
294294
}
295295

296296
protected static function loadConfigCache(): FactoriesCache
@@ -308,7 +308,7 @@ protected static function loadConfigCache(): FactoriesCache
308308
*/
309309
protected static function initializeCodeIgniter(): CodeIgniter
310310
{
311-
$app = Config\Services::codeigniter();
311+
$app = service('codeigniter');
312312
$app->initialize();
313313
$context = is_cli() ? 'php-cli' : 'web';
314314
$app->setContext($context);

system/CLI/CLI.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace CodeIgniter\CLI;
1515

1616
use CodeIgniter\CLI\Exceptions\CLIException;
17-
use Config\Services;
1817
use InvalidArgumentException;
1918
use Throwable;
2019

@@ -416,7 +415,7 @@ protected static function validate(string $field, string $value, $rules): bool
416415
{
417416
$label = $field;
418417
$field = 'temp';
419-
$validation = Services::validation(null, false);
418+
$validation = service('validation', null, false);
420419
$validation->setRules([
421420
$field => [
422421
'label' => $label,

system/Commands/Utilities/Routes/FilterCollector.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\Commands\Utilities\Routes;
1515

16-
use CodeIgniter\Config\Services;
1716
use CodeIgniter\Filters\Filters;
1817
use CodeIgniter\HTTP\Method;
1918
use CodeIgniter\HTTP\Request;
@@ -68,7 +67,7 @@ public function get(string $method, string $uri): array
6867
];
6968
}
7069

71-
$request = Services::incomingrequest(null, false);
70+
$request = service('incomingrequest', null, false);
7271
$request->setMethod($method);
7372

7473
$router = $this->createRouter($request);
@@ -86,7 +85,7 @@ public function get(string $method, string $uri): array
8685
*/
8786
public function getRequiredFilters(): array
8887
{
89-
$request = Services::incomingrequest(null, false);
88+
$request = service('incomingrequest', null, false);
9089
$request->setMethod(Method::GET);
9190

9291
$router = $this->createRouter($request);

system/Config/Services.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public static function session(?SessionConfig $config = null, bool $getShared =
705705
// See https://www.php.net/manual/en/function.session-cache-limiter.php.
706706
// The headers are not managed by CI's Response class.
707707
// So, we remove CI's default Cache-Control header.
708-
AppServices::response()->removeHeader('Cache-Control');
708+
AppServices::get('response')->removeHeader('Cache-Control');
709709

710710
$session->start();
711711
}

system/Database/MigrationRunner.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use CodeIgniter\I18n\Time;
2020
use Config\Database;
2121
use Config\Migrations as MigrationsConfig;
22-
use Config\Services;
2322
use RuntimeException;
2423
use stdClass;
2524

@@ -390,7 +389,7 @@ public function force(string $path, string $namespace, ?string $group = null)
390389
*/
391390
public function findMigrations(): array
392391
{
393-
$namespaces = $this->namespace ? [$this->namespace] : array_keys(Services::autoloader()->getNamespace());
392+
$namespaces = $this->namespace ? [$this->namespace] : array_keys(service('autoloader')->getNamespace());
394393
$migrations = [];
395394

396395
foreach ($namespaces as $namespace) {
@@ -415,7 +414,7 @@ public function findMigrations(): array
415414
public function findNamespaceMigrations(string $namespace): array
416415
{
417416
$migrations = [];
418-
$locator = Services::locator(true);
417+
$locator = service('locator', true);
419418

420419
if (! empty($this->path)) {
421420
helper('filesystem');
@@ -455,7 +454,7 @@ protected function migrationFromFile(string $path, string $namespace)
455454
return false;
456455
}
457456

458-
$locator = Services::locator(true);
457+
$locator = service('locator', true);
459458

460459
$migration = new stdClass();
461460

system/Debug/Exceptions.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use CodeIgniter\HTTP\ResponseInterface;
2323
use Config\Exceptions as ExceptionsConfig;
2424
use Config\Paths;
25-
use Config\Services;
2625
use ErrorException;
2726
use Psr\Log\LogLevel;
2827
use Throwable;
@@ -126,7 +125,7 @@ public function exceptionHandler(Throwable $exception)
126125

127126
[$statusCode, $exitCode] = $this->determineCodes($exception);
128127

129-
$this->request = Services::request();
128+
$this->request = service('request');
130129

131130
if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
132131
$uri = $this->request->getPath() === '' ? '/' : $this->request->getPath();
@@ -155,7 +154,7 @@ public function exceptionHandler(Throwable $exception)
155154
}
156155
}
157156

158-
$this->response = Services::response();
157+
$this->response = service('response');
159158

160159
if (method_exists($this->config, 'handler')) {
161160
// Use new ExceptionHandler

system/Debug/Toolbar.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use CodeIgniter\HTTP\RequestInterface;
2626
use CodeIgniter\HTTP\ResponseInterface;
2727
use CodeIgniter\I18n\Time;
28-
use Config\Services;
2928
use Config\Toolbar as ToolbarConfig;
3029
use Kint\Kint;
3130

@@ -386,7 +385,7 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
386385
return;
387386
}
388387

389-
$toolbar = Services::toolbar(config(ToolbarConfig::class));
388+
$toolbar = service('toolbar', config(ToolbarConfig::class));
390389
$stats = $app->getPerformanceStats();
391390
$data = $toolbar->run(
392391
$stats['startTime'],
@@ -529,7 +528,7 @@ protected function format(string $data, string $format = 'html'): string
529528
case 'html':
530529
$data['styles'] = [];
531530
extract($data);
532-
$parser = Services::parser($this->config->viewsPath, null, false);
531+
$parser = service('parser', $this->config->viewsPath, null, false);
533532
ob_start();
534533
include $this->config->viewsPath . 'toolbar.tpl.php';
535534
$output = ob_get_clean();

system/Debug/Toolbar/Collectors/Logs.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace CodeIgniter\Debug\Toolbar\Collectors;
1515

16-
use Config\Services;
17-
1816
/**
1917
* Loags collector
2018
*/
@@ -92,6 +90,6 @@ protected function collectLogs()
9290
return $this->data;
9391
}
9492

95-
return $this->data = Services::logger(true)->logCache ?? [];
93+
return $this->data = service('logger', true)->logCache ?? [];
9694
}
9795
}

system/Debug/Toolbar/Collectors/Routes.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace CodeIgniter\Debug\Toolbar\Collectors;
1515

1616
use CodeIgniter\Router\DefinedRouteCollector;
17-
use Config\Services;
1817
use ReflectionException;
1918
use ReflectionFunction;
2019
use ReflectionMethod;
@@ -74,8 +73,8 @@ class Routes extends BaseCollector
7473
*/
7574
public function display(): array
7675
{
77-
$rawRoutes = Services::routes(true);
78-
$router = Services::router(null, null, true);
76+
$rawRoutes = service('routes', true);
77+
$router = service('router', null, null, true);
7978

8079
// Get our parameters
8180
// Closure routes
@@ -153,7 +152,7 @@ public function display(): array
153152
*/
154153
public function getBadgeValue(): int
155154
{
156-
$rawRoutes = Services::routes(true);
155+
$rawRoutes = service('routes', true);
157156

158157
return count($rawRoutes->getRoutes());
159158
}

system/Debug/Toolbar/Collectors/Timers.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace CodeIgniter\Debug\Toolbar\Collectors;
1515

16-
use Config\Services;
17-
1816
/**
1917
* Timers collector
2018
*/
@@ -52,7 +50,7 @@ protected function formatTimelineData(): array
5250
{
5351
$data = [];
5452

55-
$benchmark = Services::timer(true);
53+
$benchmark = service('timer', true);
5654
$rows = $benchmark->getTimers(6);
5755

5856
foreach ($rows as $name => $info) {

0 commit comments

Comments
 (0)