php-censor/src/Plugin/Mage3.php

123 lines
3.1 KiB
PHP
Raw Normal View History

2017-09-04 13:36:41 +02:00
<?php
/**
* PHPCensor - Continuous Integration for PHP
*/
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use Psr\Log\LogLevel;
use \PHPCensor\Plugin;
/**
* Integrates PHPCensor with Mage v3: https://github.com/andres-montanez/Magallanes
2018-02-04 08:22:07 +01:00
*
2017-09-04 13:36:41 +02:00
* @package PHPCensor
* @subpackage Plugins
*/
class Mage3 extends Plugin
{
2018-02-04 08:22:07 +01:00
protected $mageBin = 'mage';
protected $mageEnv;
protected $mageLogDir;
2017-09-04 13:36:41 +02:00
/**
* {@inheritdoc}
*/
public static function pluginName()
{
return 'mage3';
}
/**
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$config = $builder->getSystemConfig('mage3');
if (!empty($config['bin'])) {
2018-02-04 08:22:07 +01:00
$this->mageBin = $config['bin'];
2017-09-04 13:36:41 +02:00
}
if (isset($options['env'])) {
2018-02-04 08:22:07 +01:00
$this->mageEnv = $builder->interpolate($options['env']);
2017-09-04 13:36:41 +02:00
}
if (isset($options['log_dir'])) {
2018-02-04 08:22:07 +01:00
$this->mageLogDir = $builder->interpolate($options['log_dir']);
2017-09-04 13:36:41 +02:00
}
}
/**
* {@inheritdoc}
*/
public function execute()
{
2018-02-04 08:22:07 +01:00
if (empty($this->mageEnv)) {
2017-09-04 13:36:41 +02:00
$this->builder->logFailure('You must specify environment.');
return false;
}
2018-02-04 08:22:07 +01:00
$result = $this->builder->executeCommand($this->mageBin . ' -n deploy ' . $this->mageEnv);
2017-09-04 13:36:41 +02:00
try {
$this->builder->log('########## MAGE LOG BEGIN ##########');
$this->builder->log($this->getMageLog());
$this->builder->log('########## MAGE LOG END ##########');
} catch (\Exception $e) {
$this->builder->log($e->getMessage(), LogLevel::NOTICE);
}
return $result;
}
/**
* Get mage log lines
* @return array
* @throws \Exception
*/
protected function getMageLog()
{
2018-02-04 08:22:07 +01:00
$logsDir = $this->build->getBuildPath() . (!empty($this->mageLogDir) ? '/' . $this->mageLogDir : '');
if (!is_dir($logsDir)) {
2017-09-04 13:36:41 +02:00
throw new \Exception('Log directory not found');
}
2018-02-04 08:22:07 +01:00
$list = scandir($logsDir);
2017-09-04 13:36:41 +02:00
if ($list === false) {
throw new \Exception('Log dir read fail');
}
$list = array_filter($list, function ($name) {
return preg_match('/^\d+_\d+\.log$/', $name);
});
if (empty($list)) {
throw new \Exception('Log dir filter fail');
}
$res = sort($list);
if ($res === false) {
throw new \Exception('Logs sort fail');
}
2018-02-04 08:22:07 +01:00
$lastLogFile = end($list);
if ($lastLogFile === false) {
2017-09-04 13:36:41 +02:00
throw new \Exception('Get last Log name fail');
}
2018-02-04 08:22:07 +01:00
$logContent = file_get_contents($logsDir . '/' . $lastLogFile);
if ($logContent === false) {
2017-09-04 13:36:41 +02:00
throw new \Exception('Get last Log content fail');
}
2018-02-04 08:22:07 +01:00
$lines = explode("\n", $logContent);
2017-09-04 13:36:41 +02:00
$lines = array_map('trim', $lines);
$lines = array_filter($lines);
return $lines;
}
}