Skip to content

Commit 89a41df

Browse files
authored
Make install work and read the config where it is registered (#13)
Two defects stopped the package working at all. The shipped config named the hook command Simtabi\GitCommitChecker\Commands\PreCommitHookCommand, a namespace the package never had, so install aborted with "Class [...] not found". And both commands read the bare git-commit-checker key while the provider registers laranail.git-commit-checker. On an unpublished install, install iterated no hooks and the hook always reported itself disabled. The config now names the real class. Both commands read through one helper that prefers laranail.git-commit-checker and still honours a config published to the old bare path, as a deprecated fallback. The publish tag now writes config/laranail/git-commit-checker.php. Tests cover the class existing, the hook running when enabled and skipping when disabled at the registered key, and the legacy override. The class and enabled tests failed before the fix. The README gains its Quick start.
1 parent b555f29 commit 89a41df

8 files changed

Lines changed: 121 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
_Nothing yet._
10+
### Fixed
11+
12+
- **`install` works.** The shipped config named the hook command
13+
`Simtabi\GitCommitChecker\Commands\PreCommitHookCommand`, a namespace this package never had,
14+
so `install` aborted with *"Class [...] not found"*. It now names
15+
`Simtabi\Laranail\GitCommitChecker\Commands\PreCommitHookCommand`.
16+
- **The config is read where the provider registers it.** Both commands read the bare
17+
`git-commit-checker` key while the defaults live at `laranail.git-commit-checker`, so an
18+
unpublished install iterated no hooks and the hook always reported itself disabled.
19+
20+
### Deprecated
21+
22+
- **A config published to `config/git-commit-checker.php`** is still honoured. Republish with
23+
`--tag=laranail::git-commit-checker-config`, which now writes
24+
`config/laranail/git-commit-checker.php`, and delete the old file.
25+
26+
### Added
27+
28+
- A `Quick start` section in the README.
1129

1230
## v0.1.0
1331

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ Requires PHP `^8.4.1 || ^8.5` and Laravel `^13.0`.
1616
composer require laranail/git-commit-checker
1717
```
1818

19+
## Quick start
20+
21+
```bash
22+
# Write .git/hooks/pre-commit and, optionally, a pint.json preset
23+
php artisan git-commit-checker:install
24+
25+
# Every commit now runs Pint in --test mode over the changed PHP files;
26+
# run the same check by hand without committing
27+
php artisan git-commit-checker:pre-commit-hook
28+
```
29+
30+
How the hook is wired is in [Architecture](docs/architecture.md); everything else is in the [documentation index](#documentation).
31+
1932
## <a name="documentation"></a>Documentation
2033

2134
Full documentation is at

‎config/config.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use Simtabi\GitCommitChecker\Commands\PreCommitHookCommand;
5+
use Simtabi\Laranail\GitCommitChecker\Commands\PreCommitHookCommand;
66

77
return [
88
'enabled' => env('GIT_COMMIT_CHECKER_ENABLED', true),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Simtabi\Laranail\GitCommitChecker\Commands\Concerns;
6+
7+
/**
8+
* One place that knows where this package's configuration lives.
9+
*
10+
* The provider registers it at `laranail.git-commit-checker`. Until 2026-10 the commands read the
11+
* bare `git-commit-checker` key instead, so an unpublished install saw null everywhere: `install`
12+
* iterated over nothing and the hook reported itself disabled. A config published before then landed
13+
* at the bare `config/git-commit-checker.php` and is still honoured (deprecated), so an application
14+
* that already overrode it keeps its override.
15+
*/
16+
trait ReadsPackageConfig
17+
{
18+
protected function packageConfig(string $key, mixed $default = null): mixed
19+
{
20+
$config = $this->laravel['config'];
21+
22+
$root = $config->has('git-commit-checker.hooks') ? 'git-commit-checker' : 'laranail.git-commit-checker';
23+
24+
return $config->get("{$root}.{$key}", $default);
25+
}
26+
}

‎src/Commands/InstallCommand.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
use Illuminate\Console\ConfirmableTrait;
1010
use Symfony\Component\Console\Input\InputOption;
1111
use Symfony\Component\Console\Attribute\AsCommand;
12+
use Simtabi\Laranail\GitCommitChecker\Commands\Concerns\ReadsPackageConfig;
1213

1314
#[AsCommand('git-commit-checker:install', 'Install "pre_commit" hook into your git.')]
1415
class InstallCommand extends Command
1516
{
1617
use ConfirmableTrait;
18+
use ReadsPackageConfig;
1719

1820
public function handle(): int
1921
{
@@ -29,7 +31,7 @@ public function handle(): int
2931
return self::FAILURE;
3032
}
3133

32-
foreach ($this->laravel['config']->get('git-commit-checker.hooks') as $hook => $command) {
34+
foreach ($this->packageConfig('hooks', []) as $hook => $command) {
3335
$this->install($hook, $command)
3436
? $this->components->info("Hook [$hook] is installed successfully.")
3537
: $this->components->error("Unable to install hook [$hook].");
@@ -88,7 +90,7 @@ protected function generateHookScript(string $signature): string
8890

8991
protected function generatePintConfiguration(string $path): void
9092
{
91-
$presets = $this->laravel['config']->get('git-commit-checker.pint.presets', []);
93+
$presets = $this->packageConfig('pint.presets', []);
9294

9395
if (empty($presets)) {
9496
$this->components->error('Do not found a list of supported presets');
@@ -104,7 +106,7 @@ protected function generatePintConfiguration(string $path): void
104106
json_encode(
105107
$preset !== 'recommended'
106108
? ['preset' => $preset]
107-
: $this->laravel['config']->get('git-commit-checker.pint.recommended_preset'),
109+
: $this->packageConfig('pint.recommended_preset'),
108110
JSON_PRETTY_PRINT,
109111
) . PHP_EOL,
110112
)) {

‎src/Commands/PreCommitHookCommand.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
use Illuminate\Console\Command;
1212
use Symfony\Component\Process\Process;
1313
use Symfony\Component\Console\Attribute\AsCommand;
14+
use Simtabi\Laranail\GitCommitChecker\Commands\Concerns\ReadsPackageConfig;
1415

1516
#[AsCommand('git-commit-checker:pre-commit-hook', 'Git hook before commit')]
1617
class PreCommitHookCommand extends Command
1718
{
19+
use ReadsPackageConfig;
20+
1821
public function handle(): int
1922
{
20-
if (! $this->laravel['config']->get('git-commit-checker.enabled')) {
23+
if (! $this->packageConfig('enabled')) {
2124
$this->components->info('git-commit-hook is disabled. Skipped.');
2225

2326
return self::SUCCESS;

‎src/Providers/GitCommitCheckerServiceProvider.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function boot()
2121

2222
if ($this->app->runningInConsole()) {
2323
$this->publishes([
24-
__DIR__ . '/../../config/config.php' => config_path('git-commit-checker.php'),
24+
__DIR__ . '/../../config/config.php' => config_path('laranail/git-commit-checker.php'),
2525
], 'laranail::git-commit-checker-config');
2626

2727
$this->publishes([

‎tests/Feature/ConfigTest.php‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Config;
7+
use Simtabi\Laranail\GitCommitChecker\Commands\PreCommitHookCommand;
8+
9+
/**
10+
* The shipped config named `Simtabi\GitCommitChecker\Commands\PreCommitHookCommand`, a namespace this
11+
* package never had, so `install` aborted with "Class [...] not found" on every hook.
12+
*/
13+
it('names hook commands that exist', function (): void {
14+
$hooks = Config::get('laranail.git-commit-checker.hooks');
15+
16+
expect($hooks)->toBeArray()->not->toBeEmpty();
17+
18+
foreach ($hooks as $hook => $class) {
19+
expect(class_exists($class))->toBeTrue("hook [$hook] names a class that does not exist: $class")
20+
->and(is_subclass_of($class, Command::class))->toBeTrue();
21+
}
22+
23+
expect($hooks['pre-commit'])->toBe(PreCommitHookCommand::class);
24+
});
25+
26+
/**
27+
* Registered at `laranail.git-commit-checker`, read at the bare `git-commit-checker` key: unpublished,
28+
* `enabled` was null, so the hook reported "disabled" and checked nothing, ever.
29+
*/
30+
it('runs the hook when enabled at the registered key', function (): void {
31+
Config::set('laranail.git-commit-checker.enabled', true);
32+
33+
$this->artisan('git-commit-checker:pre-commit-hook')
34+
->doesntExpectOutputToContain('is disabled');
35+
});
36+
37+
it('skips the hook when disabled at the registered key', function (): void {
38+
Config::set('laranail.git-commit-checker.enabled', false);
39+
40+
$this->artisan('git-commit-checker:pre-commit-hook')
41+
->expectsOutputToContain('is disabled')
42+
->assertSuccessful();
43+
});
44+
45+
/** An application that published the old bare config/git-commit-checker.php keeps its override. */
46+
it('still honours a config published to the old bare path', function (): void {
47+
Config::set('git-commit-checker', array_replace(Config::get('laranail.git-commit-checker'), ['enabled' => false]));
48+
Config::set('laranail.git-commit-checker.enabled', true);
49+
50+
$this->artisan('git-commit-checker:pre-commit-hook')
51+
->expectsOutputToContain('is disabled');
52+
});

0 commit comments

Comments
 (0)