Skip to content

Commit

Permalink
Enable namespace service precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Jan 15, 2021
1 parent b950fc3 commit 3643732
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Config;

use CodeIgniter\Config\Services as CoreServices;
use CodeIgniter\Config\BaseService;

/**
* Services Configuration file.
Expand All @@ -17,7 +17,7 @@
* method format you should use for your service methods. For more examples,
* see the core Services file at system/Config/Services.php.
*/
class Services extends CoreServices
class Services extends BaseService
{
// public static function example($getShared = true)
// {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ parameters:
- '#Call to an undefined method CodeIgniter\\Database\\BaseConnection::supportsForeignKeys\(\)#'
- '#Call to an undefined method CodeIgniter\\Database\\ConnectionInterface::(tableExists|protectIdentifiers|setAliasedTables|escapeIdentifiers|affectedRows|addTableAlias|getIndexData)\(\)#'
- '#Call to an undefined method CodeIgniter\\Router\\RouteCollectionInterface::(getDefaultNamespace|isFiltered|getFilterForRoute|getRoutesOptions)\(\)#'
- '#Call to an undefined static method Config\\Services::[a-z]+\(\)#'
- '#Cannot access property [\$a-z_]+ on ((bool\|)?object\|resource)#'
- '#Cannot call method [a-zA-Z_]+\(\) on ((bool\|)?object\|resource)#'
- '#Method CodeIgniter\\Database\\ConnectionInterface::query\(\) invoked with 3 parameters, 1-2 required#'
Expand Down
2 changes: 1 addition & 1 deletion system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ function old(string $key, $default = null, $escape = 'html')
*/
function redirect(string $uri = null): RedirectResponse
{
$response = Services::redirectResponse(null, true);
$response = Services::redirectresponse(null, true);

if (! empty($uri))
{
Expand Down
2 changes: 1 addition & 1 deletion system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static function __callStatic(string $name, array $arguments)
public static function serviceExists(string $name): ?string
{
static::buildServicesCache();
$services = array_merge([Services::class], self::$serviceNames);
$services = array_merge(self::$serviceNames, [Services::class]);
$name = strtolower($name);

foreach ($services as $service)
Expand Down
4 changes: 2 additions & 2 deletions system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,11 @@ public static function response(App $config = null, bool $getShared = true)
*
* @return RedirectResponse
*/
public static function redirectResponse(App $config = null, bool $getShared = true)
public static function redirectresponse(App $config = null, bool $getShared = true)
{
if ($getShared)
{
return static::getSharedInstance('redirectResponse', $config);
return static::getSharedInstance('redirectresponse', $config);
}

$config = $config ?? config('App');
Expand Down
9 changes: 5 additions & 4 deletions tests/_support/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Tests\Support\Config;

use Config\Services as BaseServices;
use Tatter\Https\ServerRequest;
use RuntimeException;

/**
* Services Class
*
* Defines our version of the HTTP services to override
* the framework defaults.
* Provides a replacement uri Service
* to demonstrate overriding core services.
*/
class Services extends BaseServices
{
Expand All @@ -23,9 +23,10 @@ class Services extends BaseServices
*/
public static function uri(string $uri = null, bool $getShared = true)
{
// Intercept our test case
if ($uri === 'testCanReplaceFrameworkServices')
{
$_SESSION['testCanReplaceFrameworkServices'] = true;
throw new RuntimeException('Service originated from ' . static::class);
}

if ($getShared)
Expand Down
18 changes: 8 additions & 10 deletions tests/system/Config/ServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public function tearDown(): void
Services::reset();
}

public function testCanReplaceFrameworkServices()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('Service originated from Tests\Support\Config\Services');

Services::uri('testCanReplaceFrameworkServices');
}

public function testNewAutoloader()
{
$actual = Services::autoloader();
Expand Down Expand Up @@ -351,14 +359,4 @@ public function testServiceInstance()
$this->assertInstanceOf(\Config\Services::class, new \Config\Services());
rename(COMPOSER_PATH . '.backup', COMPOSER_PATH);
}

public function testCanReplaceFrameworkServices()
{
$this->assertArrayNotHasKey('testCanReplaceFrameworkServices', $_SESSION);

Services::uri('testCanReplaceFrameworkServices');

$this->assertArrayHasKey('testCanReplaceFrameworkServices', $_SESSION);
unset($_SESSION['testCanReplaceFrameworkServices']);
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.0.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Bugs Fixed:

- Fixed a bug in ``Entity`` class where declaring class parameters was preventing data propagation to the ``attributes`` array.
- 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.
- ``Config\Services`` (in **app/Config/Services.php**) now extends ``CodeIgniter\Config\BaseService`` to allow proper discovery of third-party services.

Deprecations:

Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/installation/upgrade_405.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ updated requirements. These methods are as follows:

To facilitate use of this interface these methods have been moved from the framework's ``Response`` into a ``ResponseTrait``
which you may use, and ``DownloadResponse`` now extends ``Response`` directly to ensure maximum compatibility.

**Config\Services**

Service discovery has been updated to allow third-party services (when enabled via Modules) to take precedence over core services. Update
**app/Config/Services.php** so the class extends ``CodeIgniter\Config\BaseService`` to allow proper discovery of third-party services.

0 comments on commit 3643732

Please sign in to comment.