Tengo 2 clases, una hereda de la otra. Están contempladas para mi uso privado dentro de mi computadora, siendo ejecutadas desde la linea de comandos de Linux con un alias que implementé para facilitarme tareas que de vez encuando requiero hacer. Son clases que no están pensadas para ser instanciadas, es decir van a funcionar con el operador de scope, es decir, NombredeClase::Metodo();
La primera es una cláse abstracta y tiene algunos métodos abstractos naturalmente. Algunos otros métodos son para uso de todas las clases que van a heredar de esta clase abstracta que llamo 'Process'. En particular tengo un método llamado 'ExecuteShellCommand' y ejecuta el comando que yo le pase como argumento. En caso de haber errores los guarda en la variable estática $shell_errors.
La primer clase que hereda de 'Process' es llamada 'SavePavoz', es una clase que respalda y hace algunas configuraciones de un sistema que estoy desarrollando. Dentro de esta clase 'SavePavoz' hago referencia a $shell_errors de esta forma
Código PHP:
self::$shell_errors;
dejo el código de los archivos, es un tanto extenso, los puntos donde creo que se debe hacer enfasis en la lectura es en el método, ExecuteShellCommand y la forma en la que utilizo self:: para llamarl al método y a la variable estática.
Finalmente, todo aparentemente se soluciona si dentro de la clase hija utilizo parent::$shell_errors, pero por que? acaso una variable estática puede tener dos valores distintos en el mismo punto del script??
saludos
codigo de Process.php
Código PHP:
abstract class Process
{
public static $shell_errors = array();
public function __construct()
{
return true;
}
abstract public function StartProcess();
abstract public function EndProcess();
protected function WriteShellMessage($Message)
{
echo $Message;
}
protected function ExecuteShellCommand($Command, &$CommandOutput = array(), &$CommandReturn = 0)
{
$output = array();
$return = 0;
$last_line = exec($Command, $output, $return);
if ( $return != 0 )
{
$next = count(self::$shell_errors);
self::$shell_errors[$next]['command'] = $Command;
self::$shell_errors[$next]['output'] = $output;
self::$shell_errors[$next]['return'] = $return;
}
$CommandOutput = $output;
$CommandReturn = $return;
return $last_line;
}
protected function ThrowShellErrors()
{
foreach ( self::$shell_errors as $index => $error_data )
{
echo "Shell Error {$index}: \n";
echo "\tCommand: {$error_data['command']}\n";
echo "\tReturn: {$error_data['return']}\n";
echo "\tOutput: \n";
foreach ( $error_data['output'] as $line )
{
echo "\t\t {$line} \n";
}
}
self::AbortProcess();
}
protected function AbortProcess()
{
$message = "Process Aborted.\n";
self::WriteShellMessage($message);
die();
}
protected function CleanDirectoryFiles($DirectoryName, $ExceptionFiles = array())
{
$directory_handler = opendir($DirectoryName);
while ( ( $file_name = readdir($directory_handler) ) !== false )
{
$complete_file_name = $DirectoryName.$file_name;
if ( !in_array($complete_file_name, $ExceptionFiles) && is_file($complete_file_name) && !eregi('^\..*$',$file_name) )
{
$command = "echo > {$complete_file_name}\n";
self::ExecuteShellCommand($command);
}
}
closedir($directory_handler);
return true;
}
}
Código PHP:
include_once 'Process.php';
final class SavePavoz extends Process
{
/**
* Pavoz directories
*/
const PAVOZ_APP_DIR = '/var/www/localhost/htdocs/ivsadmin/apps/pavoz/';
const PAVOZ_LIB_DIR = '/var/lib/pavoz/';
const PAVOZ_LOG_DIR = '/var/log/pavoz/';
const PAVOZ_BIN_DIR = '/usr/local/bin/pavoz/';
const PAVOZ_BKP_DIR = '/home/moy/backups/ebuilds/';
const PAVOZ_EBUILD_DIR = '/usr/local/intervoice/ivs-admin/pavoz/';
/**
* Asterisk directories
*/
const ASTERISK_DIR = '/etc/asterisk/';
const ASTERISK_LOG_DIR = '/var/log/asterisk/';
const ASTERISK_SOUNDS_DIR = '/var/lib/asterisk/sounds/';
/**
* misc files that must be replaced in the host machine
*/
const SUDOERS_FILE = '/etc/sudoers';
const PHP_INI_FILE = '/etc/php/cli-php5/php.ini';
const SVC_RUN_FILE = '/etc/svc/asterisk/run';
const SVC_LOG_FILE = '/etc/svc/asterisk/log/run';
const ASTERISK_CDR_FILE = '/var/log/asterisk/cdr.db';
const ASTERISK_AGI_FILE = '/var/lib/asterisk/agi-bin/agi.php';
public function __constructor()
{
return true;
}
public function StartProcess()
{
self::WriteShellMessage("Starting PaVoz Saving in directory '".self::PAVOZ_APP_DIR."' in 1 seconds...\n");
sleep(1);
self::WriteShellMessage("Starting...\n");
return true;
}
public function EraseSqliteData()
{
$delete_tables = array('cdr', 'cdriss', 'ip_trunks', 'pavoz_users', 'trunks', 'trunks_numbers', 'zapata_trunks');
foreach ( $delete_tables as $table )
{
$command = 'sqlite '.self::ASTERISK_CDR_FILE.' "DELETE FROM '.$table.'"';
self::ExecuteShellCommand($command);
}
$command = 'sqlite '.self::ASTERISK_CDR_FILE.' "UPDATE config_files SET file_content = \'\'"';
self::ExecuteShellCommand($command);
if ( !empty(self::$shell_errors) )
{
self::ThrowShellErrors();
}
return true;
}
public function EraseAsteriskLogs()
{
$omit_files = array(self::ASTERISK_CDR_FILE);
self::CleanDirectoryFiles(self::ASTERISK_LOG_DIR, $omit_files);
self::CleanDirectoryFiles(self::ASTERISK_LOG_DIR.'cdr-csv/');
return true;
}
public function ErasePavozLogs()
{
self::CleanDirectoryFiles(self::PAVOZ_LOG_DIR);
return true;
}
public function ResetLocalData()
{
$default_data = "[localdata]\n";
$default_data .= "host=\n";
$default_data .= "user=\n";
$default_data .= "key=\n";
$default_data .= "keyname=\n";
$default_data .= "password=\n";
$default_data .= "netconfig=\n";
$default_data .= "localip=\n";
$default_data .= "syspanel=off\n";
$default_lines = explode("\n",$default_data);
$erase_command = "echo > ".self::PAVOZ_LIB_DIR."localdata.iss";
$command_output = array();
$command_return = 0;
self::ExecuteShellCommand($erase_command, $command_output, $command_return);
$operator = "";
foreach ( $default_lines as $line )
{
$command_output = array();
$command_return = 0;
$operator = $operator == "" ? ">" : ">>";
$init_command = "echo '{$line}' {$operator} ".self::PAVOZ_LIB_DIR."localdata.iss";
self::ExecuteShellCommand($init_command, $command_output, $command_return);
}
if ( !empty(self::$shell_errors) )
{
self::ThrowShellErrors();
}
return true;
}
public function BackupLastEbuild()
{
$command = 'zip -r '.self::PAVOZ_BKP_DIR.'ebuild '.self::PAVOZ_EBUILD_DIR."\n";
self::ExecuteShellCommand($command);
if ( !empty(self::$shell_errors) )
{
self::ThrowShellErrors();
}
return true;
}
public function CreateNewEbuild()
{
$directories = array('phpfiles', 'logfiles', 'misc', 'pavozconfig', 'pbxconfig', 'sounds', 'bashfiles');
//removing old files
foreach ( $directories as $directory_name )
{
$command = 'rm -r '.self::PAVOZ_EBUILD_DIR.'files/'.$directory_name."/*\n";
self::ExecuteShellCommand($command);
}
var_dump(self::$shell_errors);
var_dump(parent::$shell_errors);
//adding directories
$command = 'cp -r '.self::PAVOZ_APP_DIR.'* '.self::PAVOZ_EBUILD_DIR.'files/pavozphpfiles/';
self::ExecuteShellCommand($command);
$command = 'cp -r '.self::ASTERISK_LOG_DIR.'* '.self::PAVOZ_EBUILD_DIR.'files/pavozlogfiles/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::PAVOZ_LOG_DIR.'* '.self::PAVOZ_EBUILD_DIR.'files/asterisklogfiles/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::PAVOZ_LIB_DIR.'* '.self::PAVOZ_EBUILD_DIR.'files/pavozconfig/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::ASTERISK_SOUNDS_DIR.'iss_* '.self::PAVOZ_EBUILD_DIR.'files/asterisksounds/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::ASTERISK_DIR.'* '.self::PAVOZ_EBUILD_DIR.'files/asteriskconfig/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::PAVOZ_BIN_DIR.'* '.self::PAVOZ_EBUILD_DIR.'files/pavozbinfiles/';
self::ExecuteShellCommand($command);
//adding specific files
$command = 'cp '.self::SUDOERS_FILE.' '.self::PAVOZ_EBUILD_DIR.'files/misc/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::PHP_INI_FILE.' '.self::PAVOZ_EBUILD_DIR.'files/misc/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::SVC_RUN_FILE.' '.self::PAVOZ_EBUILD_DIR.'files/misc/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::SVC_LOG_FILE.' '.self::PAVOZ_EBUILD_DIR.'files/misc/';
self::ExecuteShellCommand($command);
$command = 'cp '.self::ASTERISK_AGI_FILE.' '.self::PAVOZ_EBUILD_DIR.'files/misc/';
self::ExecuteShellCommand($command);
if ( !empty(self::$shell_errors) )
{
self::ThrowShellErrors();
}
return true;
}
public function EndProcess()
{
self::WriteShellMessage("PaVoz data has been updated succesfully in '".self::PAVOZ_EBUILD_DIR."'.\n");
return true;
}
}