Skip to content

Instantly share code, notes, and snippets.

@vglebov
Created June 28, 2012 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vglebov/3010104 to your computer and use it in GitHub Desktop.
Save vglebov/3010104 to your computer and use it in GitHub Desktop.
object wrapper for PHP "exec" function
<?php
$e = Exec::withCommandLine($cmd)->launch();
if ($e->getResultCode() !== 0) {
$message = join("\n", $e->getOutput());
throw new Exception($message);
}
<?php
class Exec{
var $commandLine;
var $resultCode = 0;
var $output = array();
public static function withCommandLine($commandLine){
return new Exec($commandLine);
}
function __construct($commandLine)
{
$this->commandLine = $commandLine;
}
public function launch()
{
exec($this->commandLine, $this->output, $this->resultCode);
return $this;
}
public function getCommandLine()
{
return $this->commandLine;
}
public function getResultCode()
{
return $this->resultCode;
}
public function getOutput()
{
return $this->output;
}
}
@kalys
Copy link

kalys commented Jun 30, 2012

Лаконичный руби такой лаконичный.

class Exec
  attr_reader :result_code, :command_line, :output

  def initialize command_line
     @command_line = command_line
  end

  def launch
    @output = `#{@command_line}`
    @result_code = $?.to_i
    self
  end
end

@vglebov
Copy link
Author

vglebov commented Jun 30, 2012

Разработчик Руби унаследовал от Перла лучшие его качества, в частности и то, что лень дожна работать на программиста, а не против него. А разработчики PHP унаследовали не самые лучшие стороны Перла.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment