|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the dotfiles project. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Anthonius Munthi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Dotfiles\Core\Processor; |
|
15
|
|
|
|
|
16
|
|
|
use Dotfiles\Core\Config\Config; |
|
17
|
|
|
use Dotfiles\Core\Event\Dispatcher; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
use Symfony\Component\Console\Helper\DebugFormatterHelper; |
|
20
|
|
|
use Symfony\Component\Finder\Finder; |
|
21
|
|
|
use Symfony\Component\Process\Process; |
|
22
|
|
|
|
|
23
|
|
|
class Hooks |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Config |
|
27
|
|
|
*/ |
|
28
|
|
|
private $config; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Dispatcher |
|
32
|
|
|
*/ |
|
33
|
|
|
private $dispatcher; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $hooks; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var LoggerInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $logger; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
Config $config, |
|
47
|
|
|
Dispatcher $dispatcher, |
|
48
|
|
|
LoggerInterface $logger |
|
49
|
|
|
) { |
|
50
|
|
|
$this->config = $config; |
|
51
|
|
|
$this->dispatcher = $dispatcher; |
|
52
|
|
|
$this->logger = $logger; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function run(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->hooks = array(); |
|
58
|
|
|
$this->registerHooks(); |
|
59
|
|
|
$this->doPreRestoreHooks(); |
|
60
|
|
|
$this->doPostRestoreHooks(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function debug($messages, $context = array()): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->logger->info($messages, $context); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function doPostRestoreHooks(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$hooks = $this->hooks['post']['restore']; |
|
71
|
|
|
$this->debug('Processing post-restore hooks'); |
|
72
|
|
|
foreach ($hooks as $relPath => $realPath) { |
|
73
|
|
|
$this->doProcessHooks($relPath, $realPath); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function doPreRestoreHooks(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$hooks = $this->hooks['pre']['restore']; |
|
80
|
|
|
$this->debug('Start Processing pre-restore hooks'); |
|
81
|
|
|
foreach ($hooks as $relPath => $realPath) { |
|
82
|
|
|
$this->doProcessHooks($relPath, $realPath); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function doProcessHooks($relPath, $realPath): void |
|
87
|
|
|
{ |
|
88
|
|
|
$helper = new DebugFormatterHelper(); |
|
89
|
|
|
$logger = $this->logger; |
|
90
|
|
|
$logger->debug("Executing <comment>$relPath</comment>"); |
|
91
|
|
|
$process = new Process($realPath); |
|
92
|
|
|
$process->run(function ($type, $buffer) use ($relPath,$logger,$helper,$process): void { |
|
|
|
|
|
|
93
|
|
|
$contents = $helper->start( |
|
94
|
|
|
spl_object_hash($process), |
|
95
|
|
|
$buffer, |
|
96
|
|
|
Process::ERR === $type |
|
97
|
|
|
); |
|
98
|
|
|
$logger->debug('OUTPUT >>'.$contents); |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function registerHooks(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->hooks['pre']['restore'] = array(); |
|
105
|
|
|
$this->hooks['post']['restore'] = array(); |
|
106
|
|
|
|
|
107
|
|
|
$backupPath = $this->config->get('dotfiles.backup_dir'); |
|
108
|
|
|
$finder = Finder::create() |
|
109
|
|
|
->in($backupPath.'/src') |
|
110
|
|
|
->path('hooks') |
|
111
|
|
|
->name('pre-*') |
|
112
|
|
|
->name('post-*') |
|
113
|
|
|
; |
|
114
|
|
|
|
|
115
|
|
|
/* @var \SplFileInfo $file */ |
|
116
|
|
|
foreach ($finder->files() as $file) { |
|
117
|
|
|
$relPath = $file->getRelativePathname(); |
|
118
|
|
|
$realPath = $file->getRealPath(); |
|
119
|
|
|
|
|
120
|
|
|
$baseName = basename($file->getRealPath()); |
|
121
|
|
|
if (false !== ($tlength = strpos($baseName, '.'))) { |
|
122
|
|
|
$baseName = substr($baseName, 0, $tlength); |
|
123
|
|
|
} |
|
124
|
|
|
$exp = explode('-', $baseName); |
|
125
|
|
|
|
|
126
|
|
|
if (!is_executable($realPath)) { |
|
127
|
|
|
$this->debug('-hooks not executable: '.$relPath); |
|
128
|
|
|
|
|
129
|
|
|
continue; |
|
130
|
|
|
} |
|
131
|
|
|
$type = $exp[0]; |
|
132
|
|
|
$hookOn = $exp[1]; |
|
133
|
|
|
$this->hooks[$type][$hookOn][$relPath] = $realPath; |
|
134
|
|
|
$this->debug('+hooks '.$relPath); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
This check looks for imports that have been defined, but are not used in the scope.