diff --git a/src/Phaseolies/Helpers/helpers.php b/src/Phaseolies/Helpers/helpers.php index 0617c08f..0014b577 100644 --- a/src/Phaseolies/Helpers/helpers.php +++ b/src/Phaseolies/Helpers/helpers.php @@ -3,7 +3,7 @@ use Phaseolies\Utilities\Paginator; use Phaseolies\Translation\Translator; use Phaseolies\Support\Contracts\UrlGeneratorInterface; -use Phaseolies\Support\StringService; +use Phaseolies\Support\Contracts\StringServiceInterface; use Phaseolies\Support\Facades\Log; use Phaseolies\Support\Facades\Crypt; use Phaseolies\Support\CookieJar; @@ -433,11 +433,11 @@ function is_auth(): bool if (!function_exists('str')) { /** - * Instance of the StringService class + * Instance of the string service * - * @return StringService + * @return StringServiceInterface */ - function str(): StringService + function str(): StringServiceInterface { return app('str'); } diff --git a/src/Phaseolies/Launchers/FacadeLauncher.php b/src/Phaseolies/Launchers/FacadeLauncher.php index 66394f60..4a562367 100644 --- a/src/Phaseolies/Launchers/FacadeLauncher.php +++ b/src/Phaseolies/Launchers/FacadeLauncher.php @@ -7,6 +7,7 @@ use Phaseolies\Support\Contracts\UrlGeneratorInterface; use Phaseolies\Support\ViteManager; use Phaseolies\Support\StringService; +use Phaseolies\Support\Contracts\StringServiceInterface; use Phaseolies\Support\Storage\StorageFileService; use Phaseolies\Support\Storage\Contracts\StorageInterface; use Phaseolies\Signals\SignalBus; @@ -116,9 +117,11 @@ public function register() return new Sanitizer([], []); }); - // Bind the 'str' service to a singleton instance of the StringService class. + // Bind the string service contract to a singleton implementation and + // retain the existing 'str' service name as an alias. // This handles string related helper jobs - $this->app->singleton('str', StringService::class); + $this->app->singleton(StringServiceInterface::class, StringService::class); + $this->app->alias(StringServiceInterface::class, 'str'); // Bind the 'cookie' service to a singleton instance of the CookieJar class. // This handles cookie related jobs diff --git a/src/Phaseolies/Support/Contracts/StringServiceInterface.php b/src/Phaseolies/Support/Contracts/StringServiceInterface.php new file mode 100644 index 00000000..cf4f4f33 --- /dev/null +++ b/src/Phaseolies/Support/Contracts/StringServiceInterface.php @@ -0,0 +1,297 @@ +assertFalse($this->stringService->isJson('Hello World')); $this->assertFalse($this->stringService->isJson('')); } + + public function testStringServiceConformsToTheContract() + { + $this->assertInstanceOf(StringServiceInterface::class, $this->stringService); + } + + public function testApplicationBindsTheStringContractAndLegacyServiceNameToTheSameSingleton() + { + $app = $this->getMockBuilder(Application::class)->disableOriginalConstructor()->onlyMethods([])->getMock(); + (new ReflectionClass(Container::class))->getConstructor()->invoke($app); + $previous = Container::getInstance(); + Container::setInstance($app); + + try { + $originalAppUrl = getenv('APP_URL'); + putenv('APP_URL=http://localhost'); + (new FacadeLauncher($app))->register(); + + $contract = $app->get(StringServiceInterface::class); + + $this->assertInstanceOf(StringService::class, $contract); + $this->assertSame($contract, $app->get('str')); + $this->assertSame($contract, str()); + } finally { + Container::setInstance($previous); + $originalAppUrl === false ? putenv('APP_URL') : putenv("APP_URL={$originalAppUrl}"); + } + } }