-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2dd935b
Showing
6 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "czproject/runner", | ||
"type": "library", | ||
"description": "Process runner.", | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Jan Pecha", | ||
"homepage": "https://www.janpecha.cz/" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"czproject/path-helper": "^4.0" | ||
}, | ||
"autoload": { | ||
"classmap": ["src/"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
New BSD License | ||
--------------- | ||
|
||
Copyright © 2016 Jan Pecha (https://www.janpecha.cz/) All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, this | ||
list of conditions and the following disclaimer in the documentation and/or | ||
other materials provided with the distribution. | ||
* Neither the name of “CzProject“ nor the names of its contributors may be used to | ||
endorse or promote products derived from this software without specific prior | ||
written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
# CzProject\Runner | ||
|
||
## Usage | ||
|
||
``` php | ||
<?php | ||
|
||
use CzProject\Runner\Runner; | ||
|
||
$runner = new Runner('/path/to/working/directory'); | ||
|
||
$result = $runner->run('ls'); | ||
$result = $runner->run(array('git', 'remote', 'add', $remoteName, $remoteUrl)); | ||
$result = $runner->run(array( | ||
'git', | ||
'clone', | ||
$cloneUrl, | ||
'--bare' => TRUE, | ||
'--branch' => 'master', | ||
)); | ||
|
||
$result->isOk(); | ||
$result->getCommand(); | ||
$result->getCode(); | ||
$result->getOutput(); | ||
|
||
``` | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
[Download a latest package](https://github.com/czproject/runner/releases) or use [Composer](http://getcomposer.org/): | ||
|
||
``` | ||
composer require [--dev] czproject/runner | ||
``` | ||
|
||
`CzProject\Runner` requires PHP 5.3.0 or later. | ||
|
||
|
||
------------------------------ | ||
|
||
License: [New BSD License](license.md) | ||
<br>Author: Jan Pecha, https://www.janpecha.cz/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace CzProject\Runner; | ||
|
||
use CzProject\PathHelper; | ||
|
||
|
||
class Runner | ||
{ | ||
/** @var string */ | ||
private $directory; | ||
|
||
|
||
public function __construct($directory) | ||
{ | ||
$this->directory = PathHelper::absolutizePath($directory); | ||
} | ||
|
||
|
||
/** | ||
* @param string|string[] | ||
* @param string|NULL | ||
* @return RunnerResult | ||
*/ | ||
public function run($command, $subdirectory = NULL) | ||
{ | ||
$directory = $this->getDirectory($subdirectory); | ||
|
||
if (!is_dir($directory)) { | ||
throw new RunnerException("Directory '$directory' not found"); | ||
} | ||
|
||
$cwd = getcwd(); | ||
chdir($directory); | ||
|
||
$cmd = NULL; | ||
|
||
if (is_string($command)) { | ||
$cmd = $command; | ||
|
||
} else { | ||
$cmd = $this->processCommand((array) $command); | ||
} | ||
|
||
exec($cmd . ' 2>&1', $output, $returnCode); | ||
chdir($cwd); | ||
return new RunnerResult($cmd, $returnCode, $output); | ||
} | ||
|
||
|
||
/** | ||
* @param string|NULL | ||
* @return string | ||
*/ | ||
public function getDirectory($subdirectory = NULL) | ||
{ | ||
$directory = $this->directory; | ||
|
||
if ($subdirectory !== NULL) { | ||
$directory = rtrim($directory . '/' . PathHelper::absolutizePath($subdirectory, NULL), '/'); | ||
} | ||
|
||
return $directory; | ||
} | ||
|
||
|
||
/** | ||
* @param string | ||
* @return string | ||
*/ | ||
public function escapeArgument($argument) | ||
{ | ||
return escapeshellarg($argument); | ||
} | ||
|
||
|
||
/** | ||
* Example: [ | ||
* 'program', | ||
* '--directory' => 'test', | ||
* '--bare', | ||
* 'value' | ||
* ] | ||
* @param array | ||
* @return string | ||
*/ | ||
protected function processCommand(array $args) | ||
{ | ||
$cmd = array(); | ||
|
||
$programName = $this->escapeArgument(array_shift($args)); | ||
|
||
foreach ($args as $opt => $arg) { | ||
if (is_string($opt)) { | ||
if ($arg !== FALSE && $arg !== NULL) { | ||
$cmd[] = $opt; | ||
} | ||
} | ||
|
||
if (!is_bool($arg)) { | ||
$cmd[] = $this->escapeArgument($arg); | ||
} | ||
} | ||
|
||
return "$programName " . implode(' ', $cmd); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace CzProject\Runner; | ||
|
||
|
||
class RunnerResult | ||
{ | ||
/** @var string */ | ||
private $command; | ||
|
||
/** @var int */ | ||
private $code; | ||
|
||
/** @var string[] */ | ||
private $output; | ||
|
||
|
||
/** | ||
* @param string | ||
* @param int | ||
* @param string[] | ||
*/ | ||
public function __construct($command, $code, array $output) | ||
{ | ||
$this->command = (string) $command; | ||
$this->code = (int) $code; | ||
$this->output = $output; | ||
} | ||
|
||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isOk() | ||
{ | ||
return $this->code === 0; | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCommand() | ||
{ | ||
return $this->command; | ||
} | ||
|
||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getCode() | ||
{ | ||
return $this->code; | ||
} | ||
|
||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getOutput() | ||
{ | ||
return $this->output; | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toText() | ||
{ | ||
return '$ ' . $this->getCommand() . "\n\n" | ||
. implode("\n", $this->getOutput()) . "\n\n" | ||
. '=> ' . $this->getCode() . "\n\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace CzProject\Runner; | ||
|
||
|
||
class Exception extends \Exception | ||
{ | ||
} | ||
|
||
|
||
class RunnerException extends Exception | ||
{ | ||
} |