Skip to content
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

Replace Core Services #3943

Merged
merged 3 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 40 additions & 0 deletions tests/_support/Config/Services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Support\Config;

use CodeIgniter\HTTP\URI;
use Config\Services as BaseServices;
use RuntimeException;

/**
* Services Class
*
* Provides a replacement uri Service
* to demonstrate overriding core services.
*/
class Services extends BaseServices
{
/**
* The URI class provides a way to model and manipulate URIs.
*
* @param string $uri
* @param boolean $getShared
*
* @return URI
*/
public static function uri(string $uri = null, bool $getShared = true)
{
// Intercept our test case
if ($uri === 'testCanReplaceFrameworkServices')
{
throw new RuntimeException('Service originated from ' . static::class);
}

if ($getShared)
{
return static::getSharedInstance('uri', $uri);
}

return new URI($uri);
}
}
9 changes: 8 additions & 1 deletion 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,5 +359,4 @@ public function testServiceInstance()
$this->assertInstanceOf(\Config\Services::class, new \Config\Services());
rename(COMPOSER_PATH . '.backup', COMPOSER_PATH);
}

}
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.