From c6fa09041ca26a6560ab2462dced2bb0db09a55a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Escribano?= Date: Sun, 29 Sep 2024 19:37:10 +0200 Subject: [PATCH] fix: fixed an error in modifying variables in the configuration file. --- config/oauth.php | 2 +- src/Commands/OAuthCommand.php | 48 ++++++++++++++++++++++++++++------- tests/Feature/CommandTest.php | 26 +++++++++++++++++++ 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/config/oauth.php b/config/oauth.php index 0dcbeab..012d18d 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -31,7 +31,7 @@ * login_route_name: Client secret key of the OAuth system * redirect_route_name_callback_ok: Route of the project receiving the callback */ - 'user_model_name' => '\App\Models\User'::class, + 'user_model_name' => 'Raiolanetworks\OAuth\Tests\Models\TestUser', 'guard_name' => 'web', 'login_route_name' => 'login', 'redirect_route_name_callback_ok' => 'home', diff --git a/src/Commands/OAuthCommand.php b/src/Commands/OAuthCommand.php index db58113..4ea905d 100644 --- a/src/Commands/OAuthCommand.php +++ b/src/Commands/OAuthCommand.php @@ -4,6 +4,7 @@ namespace Raiolanetworks\OAuth\Commands; +use Exception; use Illuminate\Console\Command; use Illuminate\Support\Str; @@ -26,7 +27,6 @@ public function handle(): int info('The configuration file has been published.'); $this->setConfigVariables(); - $this->call('config:clear'); info('Some variables have been overwritten in the configuration file “oauth.php”.'); $this->setEnvironmentVariables(); @@ -50,39 +50,38 @@ protected function setConfigVariables(): void $modelName = text( label: 'Model name Authenticatable:', placeholder: 'E.g. app/Models/User', - default: 'app/Models/User', + default: 'App\Models\User', validate: fn (string $value) => $this->modelNameValidation($value), ); + $this->setConfigVariable('user_model_name', $modelName); $guardName = text( label: 'Main guard name:', placeholder: 'E.g. web', default: 'web', ); + $this->setConfigVariable('guard_name', $guardName); $loginRoute = text( label: 'Login route name:', placeholder: 'E.g. login', default: 'login', ); + $this->setConfigVariable('login_route_name', $loginRoute); $redirectCallbackOkRoute = text( label: 'Route name when callback is OK:', placeholder: 'E.g. home', default: 'home', ); + $this->setConfigVariable('redirect_route_name_callback_ok', $redirectCallbackOkRoute); $offlineAccessScope = select( label: 'Will you use the refresh token system in your app?', options: ['Yes', 'No'], default: 'Yes', ); - - config()->set('oauth.user_model_name', $modelName); - config()->set('oauth.guard_name', $guardName); - config()->set('oauth.login_route_name', $loginRoute); - config()->set('oauth.redirect_route_name_callback_ok', $redirectCallbackOkRoute); - config()->set('oauth.offline_access', $offlineAccessScope === 'Yes' ? true : false); + $this->setConfigVariable('offline_access', $offlineAccessScope === 'Yes' ? true : false); } protected function setEnvironmentVariables(): void @@ -145,7 +144,7 @@ protected function createEnvironmentVariables(string $key, string|int $value): v protected function modelNameValidation(string $value): ?string { - $path = $value . '.php'; + $path = Str::replace('\\', '/', $value) . '.php'; $class = '\\' . Str::ucfirst(Str::replace('/', '\\', $value)); $authenticatableClass = 'Illuminate\Contracts\Auth\Authenticatable'; @@ -160,4 +159,35 @@ class_implements($class) === false => 'Th default => null, }; } + + protected function setConfigVariable(string $key, mixed $value): void + { + $configPath = 'config/oauth.php'; + + if (! file_exists($configPath)) { + throw new Exception('Unable to find the configuration file...'); + } + + /** @var array $lines */ + $lines = file($configPath); + + foreach ($lines as &$line) { + $trimLine = trim($line); + + if (empty($trimLine) || strpos($trimLine, '//') === 0 || strpos($trimLine, '#') === 0) { + continue; + } + + $pattern = "/(['\"])" . preg_quote($key, '/') . "\\1\s*=>\s*(.+?),/"; + + if (preg_match($pattern, $trimLine)) { + $valorFormateado = var_export($value, true); + $line = preg_replace($pattern, "'$key' => $valorFormateado,", $line); + + break; + } + } + + file_put_contents($configPath, implode('', $lines)); + } } diff --git a/tests/Feature/CommandTest.php b/tests/Feature/CommandTest.php index 9d25156..4e533c3 100644 --- a/tests/Feature/CommandTest.php +++ b/tests/Feature/CommandTest.php @@ -3,6 +3,8 @@ declare(strict_types=1); use Illuminate\Console\Command; +use Illuminate\Support\Facades\File; +use Raiolanetworks\OAuth\Commands\OAuthCommand; use Raiolanetworks\OAuth\Tests\Models\TestUser; it('run the install command', function () { @@ -24,3 +26,27 @@ unlink($tempFilePath); }); + +it('verify that if the configuration file does not exist it returns an exception', function () { + $configPath = 'config/oauth.php'; + $backupPath = 'config/oauth_backup.php'; + + if (file_exists($configPath)) { + rename($configPath, $backupPath); + } + + try { + $command = new OAuthCommand(); + + $reflection = new ReflectionClass($command); + $method = $reflection->getMethod('setConfigVariable'); + $method->setAccessible(true); + + expect(fn () => $method->invokeArgs($command, ['some_key', 'some_value'])) + ->toThrow(Exception::class, 'Unable to find the configuration file...'); + } finally { + if (File::exists($backupPath)) { + File::move($backupPath, $configPath); + } + } +});