Skip to content

Commit

Permalink
refactor(src): optimize LoginCaptchaServiceProvider
Browse files Browse the repository at this point in the history
- Removed unnecessary code
- Moved view publishing logic to a separate method
- Added condition to check if running in console before publishing views
  • Loading branch information
guanguans committed Oct 17, 2023
1 parent 3f83e47 commit 621f30b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/LoginCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ public function init(): void
parent::init();

$this->setupConfig()
->publishView()
->loadMigrations()
->extendValidator()
->bootingCaptcha();

$this->publishes([
$this->getViewPath() => resource_path(sprintf('views/vendor/%s', $this->getName())),
]);
}

public function provides(): array
Expand Down Expand Up @@ -84,6 +81,17 @@ protected function setupConfig(): self
return $this;
}

protected function publishView(): self
{
if ($this->app->runningInConsole()) {
$this->publishes([
$this->getViewPath() => resource_path(sprintf('views/vendor/%s', $this->getName())),
]);
}

return $this;
}

/**
* 初始化配置.
*/
Expand Down
6 changes: 1 addition & 5 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ function login_captcha_check(string $value): bool
*/
function login_captcha_url(?string $routeName = null): string
{
if (null === $routeName) {
return admin_route('captcha.generate', ['random' => Str::random()]);
}

return admin_route($routeName, ['random' => Str::random()]);
return admin_route($routeName ?? 'captcha.generate', ['random' => Str::random()]);
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/LoginCaptchaServiceProviderrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/dcat-login-captcha.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\DcatLoginCaptcha\Tests;

use Guanguans\DcatLoginCaptcha\LoginCaptchaServiceProvider;
use Guanguans\DcatLoginCaptcha\Setting;
use Illuminate\Support\Facades\Validator;

it('can return provides', function (): void {
expect(new LoginCaptchaServiceProvider(app()))
->provides()->toBeArray();
})->group(__DIR__, __FILE__);

it('can return setting form', function (): void {
expect(new LoginCaptchaServiceProvider(app()))
->settingForm()->toBeInstanceOf(Setting::class);
})->group(__DIR__, __FILE__);

it('can check dcat_login_captcha rule', function (): void {
expect(Validator::make(['captcha' => 'foo'], ['captcha' => 'dcat_login_captcha']))
->fails()->toBeTrue();
})->group(__DIR__, __FILE__);

0 comments on commit 621f30b

Please sign in to comment.