Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up PSR and array syntax #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ What is it?
It's a beautiful way to use powerful Linux/Unix tools in PHP. Easily and logically pipe commands together,
capture errors as PHP Exceptions and use a simple yet powerful syntax. Works with any command line tool automagically.

Features
Features
------------------

* Flexible and sexy syntax.
Expand All @@ -20,7 +20,7 @@ Examples
------------------

```php
<?php
<?php
require_once 'vendor/autoload.php';
use MrRio\ShellWrap as sh;

Expand All @@ -31,11 +31,11 @@ echo sh::ls();
sh::git('checkout', 'master');

// You can also pipe the output of one command, into another
// This downloads example.com through cURL, follows location, then pipes through grep to
// This downloads example.com through cURL, follows location, then pipes through grep to
// filter for 'html'
echo sh::grep('html', sh::curl('http://example.com', array(
echo sh::grep('html', sh::curl('http://example.com', [
'location' => true
)));
]));

// Touch a file to create it
sh::touch('file.html');
Expand All @@ -60,21 +60,21 @@ try {
}

// Commands can be written multiple ways
sh::git('reset', array('hard' => true), 'HEAD');
sh::git('reset', ['hard' => true], 'HEAD');
sh::git('reset', '--hard', 'HEAD');
sh::git(array('reset', '--hard', 'HEAD'));

// Arguments passed in are automatically escaped, this expands to
// date --date '2012-10-10 10:00:00'
echo sh::date(array(
echo sh::date([
'date' => '2012-10-10 10:00:00'
));
]);

// If arg keys are one letter, is assumes one dash prefixing it
// date -d '2012-10-10 10:00:00'
echo sh::date(array(
echo sh::date([
'd' => '2012-10-10 10:00:00'
));
]);


?>
Expand Down Expand Up @@ -107,8 +107,8 @@ ShellWrap also ships with an interactive shell mode. You can access this by typi
Warning
--------

Don't use any user inputted data with these commands. Even with very paranoid filtering, you
can't know all the potential pitfalls of each command you're using. Use your noggin.
Don't use any user inputted data with these commands. Even with very paranoid filtering, you
can't know all the potential pitfalls of each command you're using. Use your noggin.

Acknowledgements
--------------------
Expand Down
37 changes: 19 additions & 18 deletions bin/shellwrap
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
#!/usr/bin/env php
<?php
error_reporting(E_ALL);

// If we're inside someone else's project - use that autoload
if (file_exists(__DIR__ . '/../../../autoload.php')) {
require_once __DIR__ . '/../../../autoload.php';
require_once __DIR__ . '/../../../autoload.php';
} else {
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
}

use MrRio\ShellWrap as sh;
use MrRio\ShellInspector;

$sh = new MrRio\ShellWrap;
//use MrRio\ShellInspector;
use Psy\Shell;
use Psy\Configuration;

error_reporting(0);
//error_reporting(0);
define('SHELL_WRAP_INTERACTIVE', true);

echo "ShellWrap Interactive Shell\n";
echo "\n";
echo 'Mapped to $sh due to eval() limitations. Ctrl+D to exit.' . "\n\n";

$boris = new \Boris\Boris('shellwrap> ');

$boris->setInspector(new ShellInspector());

$boris->setLocal(array('sh' => $sh));

try {
$boris->start();
} catch (ShellWrapException $e) {
}
echo 'Ctrl+D to exit.' . "\n\n";

$config = new Configuration;
$config->addCasters(
[
'MrRio\ShellWrap' => 'MrRio\ShellWrap::toArray'
]
);
$shell = new Shell($config);
$shell->addCode('use MrRio\ShellWrap as sh;');
$shell->run();
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"bin": ["bin/shellwrap"],
"require": {
"php": ">=5.3.0",
"d11wtq/boris": "1.0.*"
"psy/psysh": "0.8.0"
},

"autoload": {
"psr-0": {
"MrRio": "src/"
Expand Down
10 changes: 5 additions & 5 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';

use MrRio\ShellWrap as sh;

Expand All @@ -27,13 +27,13 @@
// You can also pipe the output of one command, into another
// This downloads example.com through cURL, follows location, then pipes through grep to
// filter for 'html'
echo sh::grep('html', sh::curl('http://example.com', array(
'location' => true
)));
echo sh::grep('html', sh::curl('http://example.com', [
'location' => true,
]));

// This throws an exception, as 'invalidoption' is not a valid argument
try {
echo sh::ls(array('invalidoption' => true));
echo sh::ls(['invalidoption' => true]);
} catch (Exception $e) {
echo 'Caught failing sh::ls() call';
}
4 changes: 2 additions & 2 deletions examples/grep.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__.'/vendor/autoload.php';

use MrRio\ShellWrap as sh;

$sh = new sh();

try {
$curl = $sh->curl('https://raw.github.com/guumaster/sh/master/README.md' );
$curl = $sh->curl('https://raw.github.com/guumaster/sh/master/README.md');
$grep = $sh->grep('ASDFInstallation', $curl);
echo "match\n";
echo $grep;
Expand Down
2 changes: 1 addition & 1 deletion examples/ls.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';

use MrRio\ShellWrap as sh;

Expand Down
25 changes: 13 additions & 12 deletions src/MrRio/ShellInspector.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<?php

namespace MrRio;

class ShellInspector extends \Boris\ColoredInspector
class ShellInspector
{
/**
* Override the public dump function, so we can display plain
* text from ShellWrap instead of an object dump.
**/
public function _dump($value) {

/**
* Override the public dump function, so we can display plain
* text from ShellWrap instead of an object dump.
**/
public function _dump($value)
{
if (is_object($value) && get_class($value) == 'MrRio\ShellWrap') {
echo strval($value);
// Return zero, as this is the exit code
// Otherwise an exception was thrown
return 0;
echo strval($value);
// Return zero, as this is the exit code
// Otherwise an exception was thrown
return 0;
}

return parent::_dump($value);
}
}
}
70 changes: 36 additions & 34 deletions src/MrRio/ShellWrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

namespace MrRio;

use MrRio\ShellWrapException;

class ShellWrap
{
/**
* If set to true, will output standard output, if set to a function, will send through function
* @var boolean
* If set to true, will output standard output, if set to a function, will send through function.
*
* @var bool
*/
static public $displayStdout = false;
public static $displayStdout = false;

/**
* If set to true, will output stderr to standard output, if set to a function, will send through function
* @var boolean
* If set to true, will output stderr to standard output, if set to a function, will send through function.
*
* @var bool
*/
static public $displayStderr = false;
public static $displayStderr = false;

/**
* If set to true, will throw an exception on error
* @var boolean
* If set to true, will throw an exception on error.
*
* @var bool
*/
static public $exceptionOnError = true;

public static $exceptionOnError = true;

private static $output = array();
private static $prepend = array();
private static $output = [];
private static $prepend = [];
private static $stdin = null;

public static $exec_string;
Expand All @@ -43,7 +43,7 @@ public function __toString()

/**
* Check if array is associative, thanks to
* http://stackoverflow.com/questions/173400/#4254008
* http://stackoverflow.com/questions/173400/#4254008.
**/
private static function __isAssociative($array)
{
Expand All @@ -68,7 +68,6 @@ private static function __run($arguments)
self::$stdin = strval($argument);
unset($arguments[$arg_key]);
}

} elseif (is_array($argument)) {
if (self::__isAssociative($argument)) {

Expand All @@ -77,7 +76,6 @@ private static function __run($arguments)
$output = '';

foreach ($argument as $key => $val) {

if ($output != '') {
$output .= ' ';
}
Expand All @@ -94,10 +92,8 @@ private static function __run($arguments)

// If you just pass in 'true', it'll just add the arg
if ($val !== true) {

$output .= ' ' . escapeshellarg($val);
$output .= ' '.escapeshellarg($val);
}

}
}

Expand All @@ -113,7 +109,7 @@ private static function __run($arguments)

$shell = implode(' ', $arguments);

$output = array();
$output = [];
$return_var = null;

// Set exec_string for testing purposes
Expand All @@ -122,30 +118,31 @@ private static function __run($arguments)
// Prepend the path

$parts = explode(' ', $shell);
$parts[0] = exec('which ' . $parts[0]);
$parts[0] = exec('which '.$parts[0]);

if ($parts[0] != '') {
$shell = implode(' ', $parts);
}

$descriptor_spec = array(
0 => array('pipe', 'r'), // Stdout
1 => array('pipe', 'w'), // Stdin
2 => array('pipe', 'w') // Stderr
);
$descriptor_spec = [
0 => ['pipe', 'r'], // Stdout
1 => ['pipe', 'w'], // Stdin
2 => ['pipe', 'w'], // Stderr
];

$process = proc_open($shell, $descriptor_spec, $pipes);

if (is_resource($process)) {

fwrite($pipes[0], self::$stdin);
fclose($pipes[0]);

$output = '';

while(! feof($pipes[1])) {
while (!feof($pipes[1])) {
$stdout = fgets($pipes[1], 1024);
if (strlen($stdout) == 0) break;
if (strlen($stdout) == 0) {
break;
}

if (self::$displayStdout === true) {
echo $stdout;
Expand Down Expand Up @@ -175,20 +172,17 @@ private static function __run($arguments)
$return_value = proc_close($process);

if ($return_value != 0) {

if (self::$exceptionOnError) {
throw new ShellWrapException($error_output, $return_value);
} else {
return $error_output;
}
}

} else {
throw new ShellWrapException('Process failed to spawn');
}

//exec($shell, $output, $return_var);

}

// Raw arguments
Expand Down Expand Up @@ -220,5 +214,13 @@ public static function __callStatic($name, $arguments)

return new self();
}

/**
* Get an array representing the properties of a shellwrap.
*
* @return array
*/
public static function toArray()
{
return [ self::$output ];
}
}
Loading