1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
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
|
|
|
namespace sebastianfeldmann\CaptainHook\Hook\Composer\Action; |
11
|
|
|
|
12
|
|
|
use sebastianfeldmann\CaptainHook\Config; |
13
|
|
|
use sebastianfeldmann\CaptainHook\Console\IO; |
14
|
|
|
use sebastianfeldmann\CaptainHook\Git\Repository; |
15
|
|
|
use sebastianfeldmann\CaptainHook\Hook\Action; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CheckLockFile |
19
|
|
|
* |
20
|
|
|
* @package CaptainHook |
21
|
|
|
* @author Sebastian Feldmann <[email protected]> |
22
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
23
|
|
|
* @since Class available since Release 1.0.1 |
24
|
|
|
*/ |
25
|
|
|
class CheckLockFile implements Action |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Executes the action. |
29
|
|
|
* |
30
|
|
|
* @param \sebastianfeldmann\CaptainHook\Config $config |
31
|
|
|
* @param \sebastianfeldmann\CaptainHook\Console\IO $io |
32
|
|
|
* @param \sebastianfeldmann\CaptainHook\Git\Repository $repository |
33
|
|
|
* @param \sebastianfeldmann\CaptainHook\Config\Action $action |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
3 |
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action) |
37
|
|
|
{ |
38
|
3 |
|
$path = $action->getOptions()->get('path', getcwd()); |
39
|
3 |
|
$lockFileHash = $this->getLockFileHash($path); |
40
|
2 |
|
$configFileHash = $this->getConfigFileHash($path); |
41
|
|
|
|
42
|
2 |
|
if ($lockFileHash !== $configFileHash) { |
43
|
1 |
|
throw new \Exception('composer.lock is out of date'); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$io->write('<info>composer.lock is up to date</info>'); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Read the composer.lock file and extract the composer.json hash. |
51
|
|
|
* |
52
|
|
|
* @param string $path |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
3 |
|
private function getLockFileHash($path) |
56
|
|
|
{ |
57
|
3 |
|
$lockFile = json_decode($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.lock')); |
58
|
|
|
|
59
|
2 |
|
return $lockFile->hash; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Read the composer.json file and create a md5 hash on its contents. |
64
|
|
|
* |
65
|
|
|
* @param string $path |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
2 |
|
private function getConfigFileHash($path) |
69
|
|
|
{ |
70
|
2 |
|
return md5($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.json')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Load a composer file. |
75
|
|
|
* |
76
|
|
|
* @param string $file |
77
|
|
|
* @return \stdClass |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
3 |
|
private function loadFile($file) |
81
|
|
|
{ |
82
|
3 |
|
if (!file_exists($file)) { |
83
|
1 |
|
throw new \Exception($file . ' not found'); |
84
|
|
|
} |
85
|
2 |
|
return file_get_contents($file); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|