phpci/PHPCI/Plugin/Shell.php

99 lines
2.4 KiB
PHP
Raw Normal View History

2013-05-17 16:10:48 +02:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2013-05-17 16:10:48 +02:00
namespace PHPCI\Plugin;
2013-10-10 02:01:06 +02:00
use PHPCI\Builder;
2014-12-04 16:48:52 +01:00
use PHPCI\Helper\Lang;
2013-10-10 02:01:06 +02:00
use PHPCI\Model\Build;
2013-05-17 16:10:48 +02:00
/**
* Shell Plugin - Allows execute shell commands.
* @author Kinn Coelho Julião <kinncj@gmail.com>
* @package PHPCI
* @subpackage Plugins
*/
class Shell extends AbstractPlugin
2013-05-17 16:10:48 +02:00
{
2014-05-02 15:48:40 +02:00
/**
* @var \PHPCI\Builder
*/
2013-05-17 16:10:48 +02:00
protected $phpci;
2014-05-02 15:48:40 +02:00
/**
* @var \PHPCI\Model\Build
*/
protected $build;
protected $args;
2013-05-17 16:10:48 +02:00
/**
* @var string[] $commands The commands to be executed
2013-05-17 16:10:48 +02:00
*/
protected $commands = array();
2013-05-17 16:10:48 +02:00
/**
* Standard Constructor
*
* $options['directory'] Output Directory. Default: %BUILDPATH%
* $options['filename'] Phar Filename. Default: build.phar
* $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/
* $options['stub'] Stub Content. No Default Value
*
* @param Builder $phpci
* @param Build $build
* @param array $options
*/
2013-10-10 02:01:06 +02:00
public function __construct(Builder $phpci, Build $build, array $options = array())
2013-05-17 16:10:48 +02:00
{
2014-05-02 15:48:40 +02:00
$this->phpci = $phpci;
$this->build = $build;
2013-05-17 16:10:48 +02:00
if (isset($options['command'])) {
// Keeping this for backwards compatibility, new projects should use interpolation vars.
$options['command'] = str_replace("%buildpath%", $this->phpci->buildPath, $options['command']);
$this->commands = array($options['command']);
return;
}
/*
* Support the new syntax:
*
* shell:
* - "cd /www"
* - "rm -f file.txt"
*/
if (is_array($options)) {
$this->commands = $options;
2013-05-17 16:10:48 +02:00
}
}
/**
* Runs the shell command.
*/
2013-05-17 16:10:48 +02:00
public function execute()
{
if (!defined('ENABLE_SHELL_PLUGIN') || !ENABLE_SHELL_PLUGIN) {
2014-12-04 16:48:52 +01:00
throw new \Exception(Lang::get('shell_not_enabled'));
}
$success = true;
foreach ($this->commands as $command) {
$command = $this->phpci->interpolate($command);
if (!$this->phpci->executeCommand($command)) {
$success = false;
}
}
2013-05-17 16:10:48 +02:00
return $success;
}
}