Skip to content

Commit

Permalink
Created CLIHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
fititnt committed Oct 22, 2011
1 parent 8912aaa commit bcb18b4
Show file tree
Hide file tree
Showing 8 changed files with 932 additions and 2 deletions.
1 change: 1 addition & 0 deletions CLIHelper/doc/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@todo: add documentation here
16 changes: 16 additions & 0 deletions CLIHelper/examples/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/*
* @package CLIHelper
* @author Emerson Rocha Luiz - emerson at webdesign.eng.br - http://fititnt.org
* @copyright Copyright (C) 2011 Webdesign Assessoria em Tecniligia da Informacao. All rights reserved.
* @license GNU General Public License version 3. See license-gpl3.txt
* @license Massachusetts Institute of Technology. See license-mit.txt
* @version 0.1alpha
*
*/
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); //better debug
include_once '../library/CLIHelper.php';

$clih = new CLIHelper();

echo $clih->getSapi(TRUE);
173 changes: 173 additions & 0 deletions CLIHelper/library/CLIHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/*
* @package CLIHelper
* @author Emerson Rocha Luiz - emerson at webdesign.eng.br - http://fititnt.org
* @copyright Copyright (C) 2011 Webdesign Assessoria em Tecniligia da Informacao. All rights reserved.
* @license GNU General Public License version 3. See license-gpl3.txt
* @license Massachusetts Institute of Technology. See license-mit.txt
* @version 0.1alpha
*
*/

class CLIHelper {

/*
* @var String Name of SAPI
*/
private $sapiName;


/*
* Determine if access is made by Command Line Interface
*
* @var Boolean Full name of SAPI
*/
private $cli = FALSE;

/*
* Determine if access is made by Browser
*
* @var Boolean Full name of SAPI
*/
private $browser = FALSE;


/*
* Initialize values
*/
function __construct()
{
$this->sapiName = PHP_SAPI;

if ( defined('STDIN') || isset($_SERVER['SHELL']) ){
$this->cli = TRUE;
} else {
$this->browser = TRUE;
}
}

function __destruct()
{
//
}

/*
* Function to debug $this object
*
* @var String $method: print_r or, var_dump
*
* @var Boolean $format: true for print <pre> tags. Default false
*
* @return Void
*/
public function debug( $method = 'print_r', $format = FALSE )
{
if ($format){
echo '<pre>';
}
if ($method === 'print_r'){
print_r( $this );
} else {
var_dump( $this );
}
if ( $format ){
echo '</pre>';
}
}

/*
* Delete (set to NULL) generic variable
*
* @var String $name: name of var do delete
*
* return Object $this
*/
public function del( $name )
{
$this->$name = NULL;
return $this;
}

/*
* Return generic variable
*
* @var String $name: name of var to return
*
* return Mixed $this->$name: value of var
*/
public function get( $name )
{
return $this->$name;
}

/*
* Return SAPI name
*
*
* @return String $this->$name: value of var
*/
public function getSapi( )
{
$name = $this->sapiName;
return $name;
}

/*
* Emulate CLI $argv even if accessed by browser
*
* This script run both on PHP Command Line Interface and Browser
* For set directory on browser: add one param to url with any key but with one path
* example: /script.php?foo=C:/xampp/htaccess/mysyte
* on CLI, just add a new parameter
* example: php script.php C:/xampp/htaccess/mysyte
*
* @see https://gist.github.com/1210131
*
*
* @return String $this->$name: value of var
*/
public function getVar( $name = NULL, $method = NULL )
{
if ( $name === NULL ){
if ( !isset($_SERVER['HTTP_USER_AGENT']) ) {
global $argv;
$arguments = $argv;
} else {
$arguments = array();
$arguments[] = $_SERVER['SCRIPT_FILENAME'];
foreach ($_GET as $key => $value){
$arguments[] = $value;
}
}
}
return $name;
}

/*
* Set one generic variable the desired value
*
* @var String $name: name of var to set value
*
* @var Mixed $value: value to set to desired variable
*
* return Object $this
*/
public function set( $name, $value )
{
$this->$name = $value;
return $this;
}

/*
* Example of private method. Its a good pratice start with _ (undescore)
*
* @var <vartype> <vardescription>
*
* @return <returntype> <returndescription>
*/
private function _getShortSapiName( )
{
$name = '';
return $name;
}
}
Loading

0 comments on commit bcb18b4

Please sign in to comment.