|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCensor - Continuous Integration for PHP |
| 4 | + */ |
| 5 | + |
| 6 | +namespace PHPCensor\Plugin; |
| 7 | + |
| 8 | +use PHPCensor\Builder; |
| 9 | +use PHPCensor\Model\Build; |
| 10 | +use Psr\Log\LogLevel; |
| 11 | + |
| 12 | +/** |
| 13 | + * Integrates PHPCensor with Mage: https://github.com/andres-montanez/Magallanes |
| 14 | + * @package PHPCensor |
| 15 | + * @subpackage Plugins |
| 16 | + */ |
| 17 | +class Mage extends \PHPCensor\Plugin |
| 18 | +{ |
| 19 | + protected $mage_bin = 'mage'; |
| 20 | + protected $mage_env; |
| 21 | + |
| 22 | + /** |
| 23 | + * {@inheritdoc} |
| 24 | + */ |
| 25 | + public static function pluginName() |
| 26 | + { |
| 27 | + return 'mage'; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + public function __construct(Builder $builder, Build $build, array $options = []) |
| 34 | + { |
| 35 | + parent::__construct($builder, $build, $options); |
| 36 | + |
| 37 | + $config = $builder->getSystemConfig('mage'); |
| 38 | + if (isset($config['bin'])) { |
| 39 | + $this->mage_bin = $config['bin']; |
| 40 | + } |
| 41 | + |
| 42 | + if (isset($options['env'])) { |
| 43 | + $this->mage_env = $options['env']; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public function execute() |
| 51 | + { |
| 52 | + if (empty($this->mage_env)) { |
| 53 | + $this->builder->logFailure('You must specify environment.'); |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + $result = $this->builder->executeCommand($this->mage_bin . ' deploy to:' . $this->mage_env); |
| 58 | + |
| 59 | + try { |
| 60 | + $this->builder->log('########## MAGE LOG BEGIN ##########'); |
| 61 | + $this->builder->log($this->getMageLog()); |
| 62 | + $this->builder->log('########## MAGE LOG END ##########'); |
| 63 | + } catch (\Exception $e) { |
| 64 | + $this->builder->log($e->getMessage(), LogLevel::NOTICE); |
| 65 | + } |
| 66 | + |
| 67 | + return $result; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get mage log lines |
| 72 | + * @return array |
| 73 | + * @throws \Exception |
| 74 | + */ |
| 75 | + protected function getMageLog() |
| 76 | + { |
| 77 | + $logs_dir = $this->build->getBuildPath() . '/.mage/logs'; |
| 78 | + if (!is_dir($logs_dir)) { |
| 79 | + throw new \Exception('Log directory not found'); |
| 80 | + } |
| 81 | + |
| 82 | + $list = scandir($logs_dir); |
| 83 | + if ($list === false) { |
| 84 | + throw new \Exception('Log dir read fail'); |
| 85 | + } |
| 86 | + |
| 87 | + $list = array_filter($list, function ($name) { |
| 88 | + return preg_match('/^log-\d+-\d+\.log$/', $name); |
| 89 | + }); |
| 90 | + if (empty($list)) { |
| 91 | + throw new \Exception('Log dir filter fail'); |
| 92 | + } |
| 93 | + |
| 94 | + $res = sort($list); |
| 95 | + if ($res === false) { |
| 96 | + throw new \Exception('Logs sort fail'); |
| 97 | + } |
| 98 | + |
| 99 | + $last_log_file = end($list); |
| 100 | + if ($last_log_file === false) { |
| 101 | + throw new \Exception('Get last Log name fail'); |
| 102 | + } |
| 103 | + |
| 104 | + $log_content = file_get_contents($logs_dir . '/' . $last_log_file); |
| 105 | + if ($log_content === false) { |
| 106 | + throw new \Exception('Get last Log content fail'); |
| 107 | + } |
| 108 | + |
| 109 | + $lines = explode("\n", $log_content); |
| 110 | + $lines = array_map('trim', $lines); |
| 111 | + $lines = array_filter($lines); |
| 112 | + |
| 113 | + return $lines; |
| 114 | + } |
| 115 | +} |
0 commit comments