1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCensor - Continuous Integration for PHP |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Fabrica\Tools\Plugin; |
7
|
|
|
|
8
|
|
|
use Fabrica\Tools\Builder; |
9
|
|
|
use Fabrica\Models\Infra\Ci\Build; |
10
|
|
|
use \PHPCensor\Plugin; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Integrates PHPCensor with Mage v3: https://github.com/andres-montanez/Magallanes |
14
|
|
|
* |
15
|
|
|
* @package PHPCensor |
16
|
|
|
* @subpackage Plugins |
17
|
|
|
*/ |
18
|
|
|
class Mage3 extends Plugin |
19
|
|
|
{ |
20
|
|
|
protected $mageBin = 'mage'; |
21
|
|
|
protected $mageEnv; |
22
|
|
|
protected $mageLogDir; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public static function pluginName() |
28
|
|
|
{ |
29
|
|
|
return 'mage3'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Builder $builder, Build $build, array $options = []) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($builder, $build, $options); |
38
|
|
|
|
39
|
|
|
$config = $builder->getSystemConfig('mage3'); |
40
|
|
|
if (!empty($config['bin'])) { |
41
|
|
|
$this->mageBin = $config['bin']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (isset($options['env'])) { |
45
|
|
|
$this->mageEnv = $builder->interpolate($options['env']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (isset($options['log_dir'])) { |
49
|
|
|
$this->mageLogDir = $builder->interpolate($options['log_dir']); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function execute() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (empty($this->mageEnv)) { |
59
|
|
|
$this->builder->logFailure('You must specify environment.'); |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$result = $this->builder->executeCommand($this->mageBin . ' -n deploy ' . $this->mageEnv); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$this->builder->log('########## MAGE LOG BEGIN ##########'); |
67
|
|
|
$this->builder->log($this->getMageLog()); |
68
|
|
|
$this->builder->log('########## MAGE LOG END ##########'); |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
$this->builder->logFailure($e->getMessage()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get mage log lines |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
protected function getMageLog() |
83
|
|
|
{ |
84
|
|
|
$logsDir = $this->build->getBuildPath() . (!empty($this->mageLogDir) ? '/' . $this->mageLogDir : ''); |
85
|
|
|
if (!is_dir($logsDir)) { |
86
|
|
|
throw new \Exception('Log directory not found'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$list = scandir($logsDir); |
90
|
|
|
if ($list === false) { |
91
|
|
|
throw new \Exception('Log dir read fail'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$list = array_filter( |
95
|
|
|
$list, function ($name) { |
96
|
|
|
return preg_match('/^\d+_\d+\.log$/', $name); |
97
|
|
|
} |
98
|
|
|
); |
99
|
|
|
if (empty($list)) { |
100
|
|
|
throw new \Exception('Log dir filter fail'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$res = sort($list); |
104
|
|
|
if ($res === false) { |
105
|
|
|
throw new \Exception('Logs sort fail'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$lastLogFile = end($list); |
109
|
|
|
if ($lastLogFile === false) { |
110
|
|
|
throw new \Exception('Get last Log name fail'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$logContent = file_get_contents($logsDir . '/' . $lastLogFile); |
114
|
|
|
if ($logContent === false) { |
115
|
|
|
throw new \Exception('Get last Log content fail'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$lines = explode("\n", $logContent); |
119
|
|
|
$lines = array_map('trim', $lines); |
120
|
|
|
$lines = array_filter($lines); |
121
|
|
|
|
122
|
|
|
return $lines; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.