PHP Classes

File: Command/CMD.php

Recommend this page to a friend!
  Classes of Till Wehowski   aSQL CLI   Command/CMD.php   Download  
File: Command/CMD.php
Role: Example script
Content type: text/plain
Description: Example Command Class
Class: aSQL CLI
Parse and extract a command line arguments string
Author: By
Last change: Update composer.json
Date: 8 years ago
Size: 6,798 bytes
 

Contents

Class file image Download
<?php
/**
 * Copyright (c) 2015, Till Wehowski
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. 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.
 * 3. Neither the name of frdl/webfan 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 frdl/webfan ''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 frdl/webfan 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.
 *
 *
 */
namespace frdl\ApplicationComposer\Command;

abstract class
CMD
{
   protected
$aSess;
  
   protected
$Console;
   protected
$argtoks;
   protected
$result;
   abstract public function
process();
   abstract public function
required();
  
   function
__construct(){
       
$this->aSess = & $_SESSION['frdl\xGlobal\webfan'] ;
   }
  
  
  
  
   protected function
_read(&$handle, $blocksize) {
   
$ret = '';
    while (!
feof($handle)) {
         @
set_time_limit(ini_get('max_execution_time'));
      
$ret .= fread($handle, $blocksize);
    }
    return
$ret;
   }

  public static function
blocksize()
     {
      return (
1024*1024);
    }

   public static function
read($file, $modus = 'rb', $blocksize = null)
    {
      if(!
is_readable($file))return false;
     
$handle = fopen($file, $modus);
     
$buffer = $this->_read($handle, ((!is_numeric($blocksize) || 0 !== $blocksize % 8) ? $this->blocksize() : intval($blocksize)));
     
fclose($handle);
     return
$buffer;
   }
  
  
  
    protected function
writeToConfigFile(){
       if(!isset(
$this->aSess['ADMINDATA']['CONFIGFILE']) || !file_exists($this->aSess['ADMINDATA']['CONFIGFILE'])){
              return
false;
       }
       
$this->file = $this->aSess['ADMINDATA']['CONFIGFILE'];
       
            
               
$php = "<?php
                         /*
                           - Do not edit this file manually!
                           Application Composer - Config
                           Download: http://www.webfan.de/install/
                          
                         */
                             if(isset(\$this) &&
                               (
                                     get_class(\$this) === '\\frdl\xGlobal\webfan'
                                  || is_subclass_of(\$this, '\\frdl\ApplicationComposer\Command\CMD')
                                  || get_class(\$this) === 'frdl\xGlobal\webfan'
                                  || is_subclass_of(\$this, 'frdl\ApplicationComposer\Command\CMD')
                               )){
                                  \$this->data['config'] = "
.var_export($this->data['config'], true).";
                            }
                        "
;
                        
        try{
           
chmod($this->file,0755);
        } catch(\
Exception $e){
           
        }
        if(
false !== file_put_contents($this->file, $php)) {
        try{
           
chmod($this->file,0644);
        } catch(\
Exception $e){
           
        }
            return
true;
        }else{
            return
false;
        }

                        
   }
      
   public function
loadConfigFromFile($required = true){
       if(
false !== $required && (!isset($this->aSess['ADMINDATA']['CONFIGFILE']) || !file_exists($this->aSess['ADMINDATA']['CONFIGFILE']))){
              return
false;
       }
      
        
$this->data = array();
        
$this->file = $this->aSess['ADMINDATA']['CONFIGFILE'];
         if(
false !== $required){
              require
$this->file;
         }else{
              include
$this->file;
         }
       
      return
true;
   }
  
  
   public function
Console(){
      return
$this->Console;
   }
  
   public function
help(){
         
$required = $this->required;
         
   }
  
   final public function
getRequestOption($opt){
        foreach(
$this->argtoks['options'] as $num => $o){
         if(
$opt === $o['opt']){
            return
$o['value'];
        }
     }
     return
null;
   }
  
   final public function
isArg($argument){
         return (
$this->argpos($argument) > -1);
   }
  
  final public function
argpos($argument){
        foreach(
$this->argtoks['arguments'] as $num => $arg){
         if(
$argument === $arg['cmd']){
            return
$arg['pos'];
        }
     }
     return -
1;
   }
     
  final public function
updateRequestOption($opt, $v){
        foreach(
$this->argtoks['options'] as $num => &$o){
         if(
$opt === $o['opt']){
           
$o['value'] = $v;
            return
true;
        }
     }
     return
false;
   }
  
  final public function
putRequestOption($opt, $v){
        if(
true !== $this->updateRequestOption($opt, $v)){
        
$this->argtoks['options'][] = array(
          
'opt' => $opt,
          
'value' => $v,
          
'pos' => count($this->argtoks['options']),
         );
        return
true;
     }else{
         return
true;
     }
    
   }
       
   final public function
invoke(&$Console = null, $argtoks){
         
$this->Console = $Console;
         
$this->argtoks = $argtoks;
         
          if(isset(
$this->argtoks['flags']['d'])){
              
ini_set('display_errors', 1);
        
error_reporting(E_ALL);
       }

         
         
$this->result = new \frdl\ApplicationComposer\AjaxResult;
         
$this->result->type = 'print';
         
$this->result->out = '';
          if(
null !== $this->getRequestOption('callback'))$this->result->callback = $this->getRequestOption('callback');
      
call_user_func_array(array($this, 'process'), func_get_args());
       return
$this->result;
   }
  
   final public function
__invoke(){
        return
call_user_func_array(array($this, 'invoke'), func_get_args());
    }
      
   final public function
getName(){
       
$n = explode('\\', get_class($this));
        return
$n[count($n)-1];
    }
   
   final public function
OutData(){
       
$p = func_get_args();
       
        if(!isset(
$this->result))$this->result = new \frdl\ApplicationComposer\AjaxResult;
         if(!isset(
$this->result->type)) $this->result->type = 'print';
        
            
        if(
0 === count($p)){
            return
$this->result;
        }elseif(
1 === count($p)){
           
$this->result = $p[0];
        }elseif(
2 === count($p) && is_string($p[0])){
           
$this->result->{$p[0]} = $p[1];
        }else{
            return
trigger_error('Invalid number of arguments in '.__METHOD__.' '.__LINE__, E_USER_ERROR);
        }
       
        return
$this;
    }
   
   
}