Skip to content

Commit 9ee2249

Browse files
authored
Merge pull request #2 from igorsgm/chore/adjustments
A few adjustments
2 parents cd3a19d + 8b66427 commit 9ee2249

File tree

6 files changed

+78
-64
lines changed

6 files changed

+78
-64
lines changed

src/HooksPipeline.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class HooksPipeline extends Pipeline
1616
*/
1717
protected $callback;
1818

19+
/**
20+
* @var Closure
21+
*/
22+
protected $exceptionCallback;
23+
1924
/**
2025
* @var Repository
2126
*/
@@ -49,6 +54,17 @@ public function withCallback(Closure $callback)
4954
return $this;
5055
}
5156

57+
/**
58+
* @param Closure $callback
59+
* @return $this
60+
*/
61+
public function withExceptionCallback(Closure $callback)
62+
{
63+
$this->exceptionCallback = $callback;
64+
65+
return $this;
66+
}
67+
5268
/**
5369
* Get a Closure that represents a slice of the application onion.
5470
*
@@ -103,6 +119,10 @@ protected function carry()
103119
*/
104120
protected function handleException($passable, $e)
105121
{
122+
if ($this->exceptionCallback) {
123+
return call_user_func_array($this->exceptionCallback, [$e]);
124+
}
125+
106126
throw $e;
107127
}
108128
}

src/LaravelGitHooks.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,23 @@ public function __construct(Application $app, HookStorage $storage, array $hooks
3535
}
3636

3737
/**
38-
* {@inheritDoc}
38+
* Get all supported git hooks
3939
*/
40+
public static function getSupportedHooks()
41+
{
42+
return [
43+
'pre-commit',
44+
'prepare-commit-msg',
45+
'commit-msg',
46+
'post-commit',
47+
'pre-push',
48+
'pre-rebase',
49+
'post-rewrite',
50+
'post-checkout',
51+
'post-merge',
52+
];
53+
}
54+
4055
public function run(): void
4156
{
4257
foreach ($this->hooksMap as $hook) {

src/LaravelGitHooksServiceProvider.php

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,20 @@ class LaravelGitHooksServiceProvider extends ServiceProvider
1111
*/
1212
public function boot()
1313
{
14-
/*
15-
* Optional methods to load your package assets
16-
*/
17-
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'laravel-git-hooks');
18-
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-git-hooks');
19-
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
20-
// $this->loadRoutesFrom(__DIR__.'/routes.php');
21-
2214
if ($this->app->runningInConsole()) {
2315
$this->publishes([
2416
__DIR__.'/../config/git-hooks.php' => $this->app->configPath('git-hooks.php'),
25-
], 'config');
26-
27-
// Publishing the views.
28-
/*$this->publishes([
29-
__DIR__.'/../resources/views' => resource_path('views/vendor/laravel-git-hooks'),
30-
], 'views');*/
31-
32-
// Publishing assets.
33-
/*$this->publishes([
34-
__DIR__.'/../resources/assets' => public_path('vendor/laravel-git-hooks'),
35-
], 'assets');*/
36-
37-
// Publishing the translation files.
38-
/*$this->publishes([
39-
__DIR__.'/../resources/lang' => resource_path('lang/vendor/laravel-git-hooks'),
40-
], 'lang');*/
17+
], 'laravel-git-hooks');
4118

4219
// Registering package commands.
43-
// $this->commands([]);
20+
$this->commands([
21+
\Igorsgm\LaravelGitHooks\Console\Commands\RegisterHooks::class,
22+
\Igorsgm\LaravelGitHooks\Console\Commands\CommitMessage::class,
23+
\Igorsgm\LaravelGitHooks\Console\Commands\PreCommit::class,
24+
\Igorsgm\LaravelGitHooks\Console\Commands\PrepareCommitMessage::class,
25+
\Igorsgm\LaravelGitHooks\Console\Commands\PostCommit::class,
26+
\Igorsgm\LaravelGitHooks\Console\Commands\PrePush::class,
27+
]);
4428
}
4529
}
4630

@@ -50,44 +34,21 @@ public function boot()
5034
public function register()
5135
{
5236
// Automatically apply the package configuration
53-
$this->mergeConfigFrom(__DIR__.'/../config/git-hooks.php', 'git-hooks');
54-
55-
if ($this->app->runningInConsole()) {
56-
$this->app->singleton('laravel-git-hooks', function ($app) {
57-
$hooks = [
58-
'pre-commit',
59-
'prepare-commit-msg',
60-
'commit-msg',
61-
'post-commit',
62-
'pre-push',
63-
'pre-rebase',
64-
'post-rewrite',
65-
'post-checkout',
66-
'post-merge',
67-
];
68-
69-
$config = $app['config']->get('git-hooks');
37+
$this->mergeConfigFrom(__DIR__.'/../config/git-hooks.php', 'laravel-git-hooks');
7038

71-
$hooks = array_filter($hooks, function ($hook) use ($config) {
72-
return ! empty($config[$hook]);
73-
});
39+
$this->app->singleton('laravel-git-hooks', function ($app) {
40+
$config = $app['config']->get('git-hooks');
7441

75-
$storage = $app[Contracts\HookStorage::class];
76-
77-
return new LaravelGitHooks($app, $storage, $hooks);
42+
$hooks = array_filter(LaravelGitHooks::getSupportedHooks(), function ($hook) use ($config) {
43+
return ! empty($config[$hook]);
7844
});
7945

80-
$this->app->bind(Contracts\HookStorage::class, HookStorage::class);
81-
$this->app->bind(Contracts\CommitMessageStorage::class, CommitMessageStorage::class);
46+
$storage = $app[Contracts\HookStorage::class];
8247

83-
$this->commands([
84-
\Igorsgm\LaravelGitHooks\Console\Commands\RegisterHooks::class,
85-
\Igorsgm\LaravelGitHooks\Console\Commands\CommitMessage::class,
86-
\Igorsgm\LaravelGitHooks\Console\Commands\PreCommit::class,
87-
\Igorsgm\LaravelGitHooks\Console\Commands\PrepareCommitMessage::class,
88-
\Igorsgm\LaravelGitHooks\Console\Commands\PostCommit::class,
89-
\Igorsgm\LaravelGitHooks\Console\Commands\PrePush::class,
90-
]);
91-
}
48+
return new LaravelGitHooks($app, $storage, $hooks);
49+
});
50+
51+
$this->app->bind(Contracts\HookStorage::class, HookStorage::class);
52+
$this->app->bind(Contracts\CommitMessageStorage::class, CommitMessageStorage::class);
9253
}
9354
}

src/Traits/WithPipeline.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Config\Repository;
99
use Illuminate\Pipeline\Pipeline;
1010
use Illuminate\Support\Collection;
11+
use Throwable;
1112

1213
trait WithPipeline
1314
{
@@ -34,7 +35,8 @@ protected function makePipeline(): Pipeline
3435

3536
return $pipeline
3637
->through($this->getRegisteredHooks())
37-
->withCallback($this->showInfoAboutHook());
38+
->withCallback($this->showInfoAboutHook())
39+
->withExceptionCallback($this->showHookErrorAndExit());
3840
}
3941

4042
/**
@@ -49,6 +51,22 @@ protected function showInfoAboutHook(): Closure
4951
};
5052
}
5153

54+
/**
55+
* Show Exception message and exit
56+
*
57+
* @return Closure
58+
*/
59+
protected function showHookErrorAndExit(): Closure
60+
{
61+
return function (Throwable $e) {
62+
$message = $e->getMessage() ? ' - '.$e->getMessage() : '';
63+
$message = sprintf('%s failed%s.', $this->getHook(), $message);
64+
65+
$this->getOutput()->writeln(' <bg=red;fg=white> ERROR </> '.$message.PHP_EOL);
66+
exit(1);
67+
};
68+
}
69+
5270
/**
5371
* {@inheritDoc}
5472
*/

tests/LaravelGitHooksServiceProviderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public function test_config_file_should_be_published()
1919

2020
$provider->boot();
2121

22-
$configPath = key(LaravelGitHooksServiceProvider::$publishGroups['config']);
22+
$configPath = key(LaravelGitHooksServiceProvider::$publishGroups['laravel-git-hooks']);
2323

2424
$this->assertFileExists($configPath);
2525
$this->assertEquals(
2626
'config/git-hooks.php',
27-
LaravelGitHooksServiceProvider::$publishGroups['config'][$configPath]
27+
LaravelGitHooksServiceProvider::$publishGroups['laravel-git-hooks'][$configPath]
2828
);
2929
}
3030
}

tests/LaravelGitHooksTest.php

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

33
namespace Igorsgm\LaravelGitHooks\Tests;
44

5-
use Igorsgm\LaravelGitHooks\LaravelGitHooks;
65
use Igorsgm\LaravelGitHooks\Contracts\HookStorage;
6+
use Igorsgm\LaravelGitHooks\LaravelGitHooks;
77
use Mockery;
88

99
class LaravelGitHooksTest extends TestCase

0 commit comments

Comments
 (0)