|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of SebastianFeldmann\Git. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Runner; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
|
15
|
|
|
use CaptainHook\App\Console\IO; |
|
16
|
|
|
use CaptainHook\App\Hook\Condition as ConditionInterface; |
|
17
|
|
|
use CaptainHook\App\Hook\Condition\Cli; |
|
18
|
|
|
use CaptainHook\App\Hook\Constrained; |
|
19
|
|
|
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor; |
|
20
|
|
|
use SebastianFeldmann\Git\Repository; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Condition |
|
25
|
|
|
* |
|
26
|
|
|
* Executes an action condition by creating a condition object from a condition configuration. |
|
27
|
|
|
* |
|
28
|
|
|
* @package CaptainHook |
|
29
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
30
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
31
|
|
|
* @since Class available since Release 4.2.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
class Condition |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var \CaptainHook\App\Console\IO |
|
37
|
|
|
*/ |
|
38
|
|
|
private $io; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \SebastianFeldmann\Git\Repository |
|
42
|
|
|
*/ |
|
43
|
|
|
private $repository; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Currently executed hook |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
12 |
|
*/ |
|
50
|
|
|
private $hook; |
|
51
|
12 |
|
|
|
52
|
12 |
|
/** |
|
53
|
12 |
|
* Condition constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
56
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
57
|
|
|
* @param string $hook |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(IO $io, Repository $repository, string $hook) |
|
60
|
|
|
{ |
|
61
|
5 |
|
$this->io = $io; |
|
62
|
|
|
$this->repository = $repository; |
|
63
|
5 |
|
$this->hook = $hook; |
|
64
|
4 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Creates the configured condition and evaluates it |
|
68
|
|
|
* |
|
69
|
|
|
* @param \CaptainHook\App\Config\Condition $config |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function doesConditionApply(Config\Condition $config): bool |
|
73
|
|
|
{ |
|
74
|
|
|
$condition = $this->createCondition($config); |
|
75
|
|
|
// check for any given restrictions |
|
76
|
|
|
if (!$this->isApplicable($condition)) { |
|
77
|
5 |
|
$this->io->write('Condition skipped due to hook constraint', true, IO::VERBOSE); |
|
78
|
|
|
return true; |
|
79
|
5 |
|
} |
|
80
|
3 |
|
return $condition->isTrue($this->io, $this->repository); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
/** |
|
84
|
2 |
|
* Return the configured condition |
|
85
|
1 |
|
* |
|
86
|
|
|
* In case of a cli condition it returns an special condition class that deals with |
|
87
|
1 |
|
* the binary execution with implementing the same interface. |
|
88
|
|
|
* |
|
89
|
|
|
* @param \CaptainHook\App\Config\Condition $config |
|
90
|
|
|
* @return \CaptainHook\App\Hook\Condition |
|
91
|
|
|
* @throws \RuntimeException |
|
92
|
|
|
*/ |
|
93
|
|
|
private function createCondition(Config\Condition $config): ConditionInterface |
|
94
|
|
|
{ |
|
95
|
|
|
if (Util::getExecType($config->getExec()) === 'cli') { |
|
96
|
|
|
return new Cli(new Processor(), $config->getExec()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$class = $config->getExec(); |
|
100
|
|
|
if (!class_exists($class)) { |
|
101
|
|
|
throw new RuntimeException('could not find condition class: ' . $class); |
|
102
|
|
|
} |
|
103
|
|
|
return new $class(...$config->getArgs()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Make sure the condition can be used during this hook |
|
109
|
|
|
* |
|
110
|
|
|
* @param \CaptainHook\App\Hook\Condition $condition |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
private function isApplicable(ConditionInterface $condition) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($condition instanceof Constrained) { |
|
116
|
|
|
return $condition->getRestriction()->isApplicableFor($this->hook); |
|
117
|
|
|
} |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|