Skip to content

Commit 52ebb0d

Browse files
authored
Merge pull request #3943 from MGatner/service-override
2 parents 19c2ef3 + 642e59f commit 52ebb0d

File tree

9 files changed

+61
-7
lines changed

9 files changed

+61
-7
lines changed

app/Config/Services.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Config;
44

5-
use CodeIgniter\Config\Services as CoreServices;
5+
use CodeIgniter\Config\BaseService;
66

77
/**
88
* Services Configuration file.
@@ -17,7 +17,7 @@
1717
* method format you should use for your service methods. For more examples,
1818
* see the core Services file at system/Config/Services.php.
1919
*/
20-
class Services extends CoreServices
20+
class Services extends BaseService
2121
{
2222
// public static function example($getShared = true)
2323
// {

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ parameters:
3737
- '#Call to an undefined method CodeIgniter\\Database\\BaseConnection::supportsForeignKeys\(\)#'
3838
- '#Call to an undefined method CodeIgniter\\Database\\ConnectionInterface::(tableExists|protectIdentifiers|setAliasedTables|escapeIdentifiers|affectedRows|addTableAlias|getIndexData)\(\)#'
3939
- '#Call to an undefined method CodeIgniter\\Router\\RouteCollectionInterface::(getDefaultNamespace|isFiltered|getFilterForRoute|getRoutesOptions)\(\)#'
40+
- '#Call to an undefined static method Config\\Services::[a-z]+\(\)#'
4041
- '#Cannot access property [\$a-z_]+ on ((bool\|)?object\|resource)#'
4142
- '#Cannot call method [a-zA-Z_]+\(\) on ((bool\|)?object\|resource)#'
4243
- '#Method CodeIgniter\\Database\\ConnectionInterface::query\(\) invoked with 3 parameters, 1-2 required#'

system/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ function old(string $key, $default = null, $escape = 'html')
911911
*/
912912
function redirect(string $route = null): RedirectResponse
913913
{
914-
$response = Services::redirectResponse(null, true);
914+
$response = Services::redirectresponse(null, true);
915915

916916
if (! empty($route))
917917
{

system/Config/BaseService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static function __callStatic(string $name, array $arguments)
182182
public static function serviceExists(string $name): ?string
183183
{
184184
static::buildServicesCache();
185-
$services = array_merge([Services::class], self::$serviceNames);
185+
$services = array_merge(self::$serviceNames, [Services::class]);
186186
$name = strtolower($name);
187187

188188
foreach ($services as $service)

system/Config/Services.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,11 @@ public static function response(App $config = null, bool $getShared = true)
600600
*
601601
* @return RedirectResponse
602602
*/
603-
public static function redirectResponse(App $config = null, bool $getShared = true)
603+
public static function redirectresponse(App $config = null, bool $getShared = true)
604604
{
605605
if ($getShared)
606606
{
607-
return static::getSharedInstance('redirectResponse', $config);
607+
return static::getSharedInstance('redirectresponse', $config);
608608
}
609609

610610
$config = $config ?? config('App');

tests/_support/Config/Services.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Tests\Support\Config;
4+
5+
use CodeIgniter\HTTP\URI;
6+
use Config\Services as BaseServices;
7+
use RuntimeException;
8+
9+
/**
10+
* Services Class
11+
*
12+
* Provides a replacement uri Service
13+
* to demonstrate overriding core services.
14+
*/
15+
class Services extends BaseServices
16+
{
17+
/**
18+
* The URI class provides a way to model and manipulate URIs.
19+
*
20+
* @param string $uri
21+
* @param boolean $getShared
22+
*
23+
* @return URI
24+
*/
25+
public static function uri(string $uri = null, bool $getShared = true)
26+
{
27+
// Intercept our test case
28+
if ($uri === 'testCanReplaceFrameworkServices')
29+
{
30+
throw new RuntimeException('Service originated from ' . static::class);
31+
}
32+
33+
if ($getShared)
34+
{
35+
return static::getSharedInstance('uri', $uri);
36+
}
37+
38+
return new URI($uri);
39+
}
40+
}

tests/system/Config/ServicesTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function tearDown(): void
2525
Services::reset();
2626
}
2727

28+
public function testCanReplaceFrameworkServices()
29+
{
30+
$this->expectException('RuntimeException');
31+
$this->expectExceptionMessage('Service originated from Tests\Support\Config\Services');
32+
33+
Services::uri('testCanReplaceFrameworkServices');
34+
}
35+
2836
public function testNewAutoloader()
2937
{
3038
$actual = Services::autoloader();
@@ -351,5 +359,4 @@ public function testServiceInstance()
351359
$this->assertInstanceOf(\Config\Services::class, new \Config\Services());
352360
rename(COMPOSER_PATH . '.backup', COMPOSER_PATH);
353361
}
354-
355362
}

user_guide_src/source/changelogs/v4.0.5.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Bugs Fixed:
2424

2525
- Fixed a bug in ``Entity`` class where declaring class parameters was preventing data propagation to the ``attributes`` array.
2626
- Handling for the environment variable ``encryption.key`` has changed. Previously, explicit function calls, like ``getenv('encryption.key')`` or ``env('encryption.key')`` where the value has the special prefix ``hex2bin:`` returns an automatically converted binary string. This is now changed to just return the character string with the prefix. This change was due to incompatibility with handling binary strings in environment variables on Windows platforms. However, accessing ``$key`` using ``Encryption`` class config remains unchanged and still returns a binary string.
27+
- ``Config\Services`` (in **app/Config/Services.php**) now extends ``CodeIgniter\Config\BaseService`` to allow proper discovery of third-party services.
2728

2829
Deprecations:
2930

user_guide_src/source/installation/upgrade_405.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ updated requirements. These methods are as follows:
5757

5858
To facilitate use of this interface these methods have been moved from the framework's ``Response`` into a ``ResponseTrait``
5959
which you may use, and ``DownloadResponse`` now extends ``Response`` directly to ensure maximum compatibility.
60+
61+
**Config\Services**
62+
63+
Service discovery has been updated to allow third-party services (when enabled via Modules) to take precedence over core services. Update
64+
**app/Config/Services.php** so the class extends ``CodeIgniter\Config\BaseService`` to allow proper discovery of third-party services.

0 commit comments

Comments
 (0)