1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of SebastianFeldmann\Git. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace CaptainHook\App\Runner; |
12
|
|
|
|
13
|
|
|
use CaptainHook\App\Config; |
14
|
|
|
use CaptainHook\App\Console\IO; |
15
|
|
|
use CaptainHook\App\Hook\Condition as ConditionInterface; |
16
|
|
|
use CaptainHook\App\Hook\Condition\Cli; |
17
|
|
|
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor; |
18
|
|
|
use SebastianFeldmann\Git\Repository; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Condition |
23
|
|
|
* |
24
|
|
|
* Executes an action condition by creating a condition object from a condition configuration. |
25
|
|
|
* |
26
|
|
|
* @package CaptainHook |
27
|
|
|
* @author Sebastian Feldmann <[email protected]> |
28
|
|
|
* @link https://github.com/captainhookphp/captainhook |
29
|
|
|
* @since Class available since Release 4.2.0 |
30
|
|
|
*/ |
31
|
|
|
class Condition |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var \CaptainHook\App\Console\IO |
35
|
|
|
*/ |
36
|
|
|
private $io; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \SebastianFeldmann\Git\Repository |
40
|
|
|
*/ |
41
|
|
|
private $repository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Condition constructor. |
45
|
|
|
* |
46
|
|
|
* @param \CaptainHook\App\Console\IO $io |
47
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
48
|
|
|
*/ |
49
|
12 |
|
public function __construct(IO $io, Repository $repository) |
50
|
|
|
{ |
51
|
12 |
|
$this->io = $io; |
52
|
12 |
|
$this->repository = $repository; |
53
|
12 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Creates the configured condition and evaluates it |
57
|
|
|
* |
58
|
|
|
* @param \CaptainHook\App\Config\Condition $config |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
5 |
|
public function doesConditionApply(Config\Condition $config) : bool |
62
|
|
|
{ |
63
|
5 |
|
$condition = $this->createCondition($config); |
64
|
4 |
|
return $condition->isTrue($this->io, $this->repository); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the configured condition |
69
|
|
|
* |
70
|
|
|
* In case of a cli condition it returns an special condition class that deals with |
71
|
|
|
* the binary execution with implementing the same interface. |
72
|
|
|
* |
73
|
|
|
* @param \CaptainHook\App\Config\Condition $config |
74
|
|
|
* @return \CaptainHook\App\Hook\Condition |
75
|
|
|
* @throws \RuntimeException |
76
|
|
|
*/ |
77
|
5 |
|
private function createCondition(Config\Condition $config) : ConditionInterface |
78
|
|
|
{ |
79
|
5 |
|
if (Util::getExecType($config->getExec()) === 'cli') { |
80
|
3 |
|
return new Cli(new Processor(), $config->getExec()); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$class = $config->getExec(); |
84
|
2 |
|
if (!class_exists($class)) { |
85
|
1 |
|
throw new RuntimeException('could not find condition class: ' . $class); |
86
|
|
|
} |
87
|
1 |
|
return new $class(...$config->getArgs()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|