Skip to content

Commit aa3cb42

Browse files
committed
- Removed the dependency of Repository in all commands.
- Changed the way that hook classes are instantiated, now they receive an array with their configuration as parameter instead of receiving directly the repository object and getting its own configuration from it. - Replaced Illuminate\Config\Repository with Config facade in Test files - Removed config dependency from commands constructors
1 parent c4ee3ff commit aa3cb42

14 files changed

+63
-167
lines changed

src/Console/Commands/CommitMessage.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Igorsgm\GitHooks\Contracts\HookCommand;
77
use Igorsgm\GitHooks\Traits\WithCommitMessage;
88
use Illuminate\Console\Command;
9-
use Illuminate\Contracts\Config\Repository;
109

1110
class CommitMessage extends Command implements HookCommand
1211
{
@@ -25,14 +24,11 @@ class CommitMessage extends Command implements HookCommand
2524
protected $description = 'Run hook commit-msg';
2625

2726
/**
28-
* @param Repository $config
2927
* @param CommitMessageStorage $messageStorage
3028
*/
31-
public function __construct(Repository $config, CommitMessageStorage $messageStorage)
29+
public function __construct(CommitMessageStorage $messageStorage)
3230
{
3331
parent::__construct();
34-
35-
$this->config = $config;
3632
$this->messageStorage = $messageStorage;
3733
}
3834

src/Console/Commands/PostCommit.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Igorsgm\GitHooks\Git\Log;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
11-
use Illuminate\Contracts\Config\Repository;
1211

1312
class PostCommit extends Command implements HookCommand
1413
{
@@ -28,21 +27,6 @@ class PostCommit extends Command implements HookCommand
2827
*/
2928
protected $description = 'Run hook post-commit';
3029

31-
/**
32-
* @var Repository
33-
*/
34-
protected $config;
35-
36-
/**
37-
* @param Repository $config
38-
*/
39-
public function __construct(Repository $config)
40-
{
41-
parent::__construct();
42-
43-
$this->config = $config;
44-
}
45-
4630
/**
4731
* {@inheritDoc}
4832
*/

src/Console/Commands/PreCommit.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Igorsgm\GitHooks\Git\GitHelper;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
11-
use Illuminate\Contracts\Config\Repository;
1211

1312
class PreCommit extends Command implements HookCommand
1413
{
@@ -28,21 +27,6 @@ class PreCommit extends Command implements HookCommand
2827
*/
2928
protected $description = 'Run hook pre-commit';
3029

31-
/**
32-
* @var Repository
33-
*/
34-
protected $config;
35-
36-
/**
37-
* @param Repository $config
38-
*/
39-
public function __construct(Repository $config)
40-
{
41-
parent::__construct();
42-
43-
$this->config = $config;
44-
}
45-
4630
/**
4731
* {@inheritDoc}
4832
*/

src/Console/Commands/PrePush.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Igorsgm\GitHooks\Git\Log;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
11-
use Illuminate\Contracts\Config\Repository;
1211

1312
class PrePush extends Command implements HookCommand
1413
{
@@ -28,21 +27,6 @@ class PrePush extends Command implements HookCommand
2827
*/
2928
protected $description = 'Run hook pre-push';
3029

31-
/**
32-
* @var Repository
33-
*/
34-
protected $config;
35-
36-
/**
37-
* @param Repository $config
38-
*/
39-
public function __construct(Repository $config)
40-
{
41-
parent::__construct();
42-
43-
$this->config = $config;
44-
}
45-
4630
/**
4731
* {@inheritDoc}
4832
*/

src/Console/Commands/PrepareCommitMessage.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Igorsgm\GitHooks\Contracts\HookCommand;
77
use Igorsgm\GitHooks\Traits\WithCommitMessage;
88
use Illuminate\Console\Command;
9-
use Illuminate\Contracts\Config\Repository;
109

1110
class PrepareCommitMessage extends Command implements HookCommand
1211
{
@@ -27,14 +26,11 @@ class PrepareCommitMessage extends Command implements HookCommand
2726
protected $description = 'Run hook prepare-commit-msg';
2827

2928
/**
30-
* @param Repository $config
3129
* @param CommitMessageStorage $messageStorage
3230
*/
33-
public function __construct(Repository $config, CommitMessageStorage $messageStorage)
31+
public function __construct(CommitMessageStorage $messageStorage)
3432
{
3533
parent::__construct();
36-
37-
$this->config = $config;
3834
$this->messageStorage = $messageStorage;
3935
}
4036

src/Contracts/HookCommand.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@
22

33
namespace Igorsgm\GitHooks\Contracts;
44

5-
use Illuminate\Contracts\Config\Repository;
6-
75
interface HookCommand
86
{
9-
/**
10-
* Get config repository
11-
*
12-
* @return Repository
13-
*/
14-
public function getConfig(): Repository;
15-
167
/**
178
* Get hook name
189
*

src/HooksPipeline.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use Exception;
7-
use Illuminate\Contracts\Config\Repository;
87
use Illuminate\Contracts\Container\Container;
98
use Illuminate\Pipeline\Pipeline;
109
use Throwable;
@@ -21,25 +20,18 @@ class HooksPipeline extends Pipeline
2120
*/
2221
protected $exceptionCallback;
2322

24-
/**
25-
* @var Repository
26-
*/
27-
protected $config;
28-
2923
/**
3024
* @var string
3125
*/
3226
protected $hook;
3327

3428
/**
3529
* @param \Illuminate\Contracts\Container\Container|null $container
36-
* @param Repository $config
3730
* @param string $hook
3831
*/
39-
public function __construct(Container $container, Repository $config, string $hook)
32+
public function __construct(Container $container, string $hook)
4033
{
4134
parent::__construct($container);
42-
$this->config = $config;
4335
$this->hook = $hook;
4436
}
4537

@@ -81,12 +73,12 @@ protected function carry()
8173
// the appropriate method and arguments, returning the results back out.
8274
return $pipe($passable, $stack);
8375
} elseif (! is_object($pipe)) {
84-
$config = (array) $this->config->get('git-hooks.'.$this->hook.'.'.$pipe);
76+
$hookParameters = (array) config('git-hooks.'.$this->hook.'.'.$pipe);
8577

8678
// If the pipe is a string we will parse the string and resolve the class out
8779
// of the dependency injection container. We can then build a callable and
8880
// execute the pipe function giving in the parameters that are required.
89-
$pipe = $this->getContainer()->make($pipe, ['config' => $config]);
81+
$pipe = $this->getContainer()->make($pipe, ['parameters' => $hookParameters]);
9082

9183
if ($this->callback) {
9284
call_user_func_array($this->callback, [$pipe]);

src/Traits/WithPipeline.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@
55
use Closure;
66
use Igorsgm\GitHooks\Contracts\Hook;
77
use Igorsgm\GitHooks\HooksPipeline;
8-
use Illuminate\Contracts\Config\Repository;
98
use Illuminate\Pipeline\Pipeline;
10-
use Illuminate\Support\Collection;
119
use Throwable;
1210

1311
trait WithPipeline
1412
{
15-
/**
16-
* {@inheritDoc}
17-
*/
18-
public function getConfig(): Repository
19-
{
20-
return $this->config;
21-
}
22-
2313
/**
2414
* Make pipeline instance
2515
*
@@ -29,7 +19,6 @@ protected function makePipeline(): Pipeline
2919
{
3020
$pipeline = new HooksPipeline(
3121
$this->getLaravel(),
32-
$this->getConfig(),
3322
$this->getHook()
3423
);
3524

@@ -72,7 +61,7 @@ protected function showHookErrorAndExit(): Closure
7261
*/
7362
public function getRegisteredHooks(): array
7463
{
75-
$hooks = new Collection((array) $this->config->get('git-hooks.'.$this->getHook()));
64+
$hooks = collect((array) config('git-hooks.'.$this->getHook()));
7665

7766
return $hooks->map(function ($hook, $i) {
7867
if (is_int($i)) {

tests/Console/Commands/CommitMessageTest.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,26 @@
88
use Igorsgm\GitHooks\Contracts\MessageHook;
99
use Igorsgm\GitHooks\Git\GitHelper;
1010
use Igorsgm\GitHooks\Tests\TestCase;
11-
use Illuminate\Config\Repository;
1211
use Illuminate\Console\OutputStyle;
12+
use Illuminate\Support\Facades\Config;
1313
use Mockery;
1414

1515
class CommitMessageTest extends TestCase
1616
{
1717
public function test_get_command_name()
1818
{
19-
$config = $this->makeConfig();
2019
$commitMessageStorage = $this->makeCommitMessageStorage();
2120

22-
$command = new CommitMessage($config, $commitMessageStorage);
21+
$command = new CommitMessage($commitMessageStorage);
2322

2423
$this->assertEquals('git-hooks:commit-msg', $command->getName());
2524
}
2625

2726
public function test_requires_file_argument()
2827
{
29-
$config = $this->makeConfig();
3028
$commitMessageStorage = $this->makeCommitMessageStorage();
3129

32-
$command = new CommitMessage($config, $commitMessageStorage);
30+
$command = new CommitMessage($commitMessageStorage);
3331

3432
$this->assertTrue($command->getDefinition()->hasArgument('file'));
3533
}
@@ -45,12 +43,10 @@ public function test_a_message_should_be_send_through_the_hook_pipes()
4543
return new $class;
4644
});
4745

48-
$config = new Repository([
49-
'git-hooks' => [
50-
'commit-msg' => [
51-
CommitMessageTestHook1::class,
52-
CommitMessageTestHook2::class,
53-
],
46+
Config::set('git-hooks', [
47+
'commit-msg' => [
48+
CommitMessageTestHook1::class,
49+
CommitMessageTestHook2::class,
5450
],
5551
]);
5652

@@ -64,7 +60,7 @@ public function test_a_message_should_be_send_through_the_hook_pipes()
6460
->expects('update')
6561
->with('tmp/COMMIT_MESSAGE', 'Test commit hook1 hook2');
6662

67-
$command = new CommitMessage($config, $commitMessageStorage);
63+
$command = new CommitMessage($commitMessageStorage);
6864

6965
$command->setLaravel($app);
7066

@@ -103,13 +99,11 @@ public function test_pass_hook_config_into_hook_object()
10399
return $path;
104100
});
105101

106-
$config = new Repository([
107-
'git-hooks' => [
108-
'commit-msg' => [
109-
CommitMessageTestHook4::class => [
110-
'param1' => 'hello',
111-
'param2' => 'world',
112-
],
102+
Config::set('git-hooks', [
103+
'commit-msg' => [
104+
CommitMessageTestHook4::class => [
105+
'param1' => 'hello',
106+
'param2' => 'world',
113107
],
114108
],
115109
]);
@@ -124,7 +118,7 @@ public function test_pass_hook_config_into_hook_object()
124118
->expects('update')
125119
->with('tmp/COMMIT_MESSAGE', 'Test commit hello world');
126120

127-
$command = new CommitMessage($config, $commitMessageStorage);
121+
$command = new CommitMessage($commitMessageStorage);
128122

129123
$command->setLaravel($app);
130124

@@ -218,19 +212,19 @@ class CommitMessageTestHook4 implements MessageHook
218212
/**
219213
* @var array
220214
*/
221-
protected $config;
215+
protected $parameters;
222216

223-
public function __construct(array $config)
217+
public function __construct(array $parameters)
224218
{
225-
$this->config = $config;
219+
$this->parameters = $parameters;
226220
}
227221

228222
/**
229223
* {@inheritDoc}
230224
*/
231225
public function handle(\Igorsgm\GitHooks\Git\CommitMessage $message, Closure $next)
232226
{
233-
$message->setMessage($message->getMessage().' '.$this->config['param1'].' '.$this->config['param2']);
227+
$message->setMessage($message->getMessage().' '.$this->parameters['param1'].' '.$this->parameters['param2']);
234228

235229
return $next($message);
236230
}

tests/Console/Commands/PostCommitTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
use Igorsgm\GitHooks\Git\GitHelper;
99
use Igorsgm\GitHooks\Git\Log;
1010
use Igorsgm\GitHooks\Tests\TestCase;
11-
use Illuminate\Config\Repository;
11+
use Illuminate\Support\Facades\Config;
1212
use Mockery;
1313

1414
class PostCommitTest extends TestCase
1515
{
1616
public function test_get_command_name()
1717
{
18-
$config = $this->makeConfig();
19-
$command = new PostCommit($config);
18+
$command = new PostCommit();
2019

2120
$this->assertEquals('git-hooks:post-commit', $command->getName());
2221
}
@@ -39,17 +38,15 @@ public function test_a_message_should_be_send_through_the_hook_pipes()
3938
return $closure($log);
4039
});
4140

42-
$config = new Repository([
43-
'git-hooks' => [
44-
'post-commit' => [
45-
$hook1,
46-
$hook2,
47-
],
41+
Config::set('git-hooks', [
42+
'post-commit' => [
43+
$hook1,
44+
$hook2,
4845
],
4946
]);
5047

5148
$app = $this->makeApplication();
52-
$command = new PostCommit($config);
49+
$command = new PostCommit();
5350
$command->setLaravel($app);
5451

5552
$gitHelper = Mockery::mock('alias:'.GitHelper::class);

0 commit comments

Comments
 (0)