Skip to content

Commit

Permalink
fix: fixed an error in modifying variables in the configuration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
victore13 committed Sep 29, 2024
1 parent 22ea52b commit c6fa090
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
48 changes: 39 additions & 9 deletions src/Commands/OAuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Raiolanetworks\OAuth\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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';

Expand All @@ -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<string> $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));
}
}
26 changes: 26 additions & 0 deletions tests/Feature/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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);
}
}
});

0 comments on commit c6fa090

Please sign in to comment.