Development

Changeset 14146

You must first sign up to be able to contribute.

Changeset 14146

Show
Ignore:
Timestamp:
12/17/08 22:53:16 (4 years ago)
Author:
geoffrey
Message:

sfConsolePlugin: added support for stored history

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfConsolePlugin/trunk/lib/task/consoleRunTask.class.php

    r14145 r14146  
    3939 
    4040    protected $errorNames = array( 
    41       E_USER_ERROR => 'User Error', 
    42       E_USER_WARNING => 'User Warning', 
    43       E_USER_NOTICE => 'User Notice', 
    44       E_PARSE => 'Parse Error', 
    45       E_NOTICE => 'Notice', 
    46       E_WARNING => 'Warning', 
    47       E_STRICT => 'Strict Standards', 
     41      E_USER_ERROR        => 'User Error', 
     42      E_USER_WARNING      => 'User Warning', 
     43      E_USER_NOTICE       => 'User Notice', 
     44      E_PARSE             => 'Parse Error', 
     45      E_NOTICE            => 'Notice', 
     46      E_WARNING           => 'Warning', 
     47      E_STRICT            => 'Strict Standards', 
    4848      E_RECOVERABLE_ERROR => 'Catchable Fatal Error', 
    49       E_DEPRECATED => 'Deprecated', 
    50       E_USER_DEPRECATED => 'User Deprecated', 
     49      E_DEPRECATED        => 'Deprecated', 
     50      E_USER_DEPRECATED   => 'User Deprecated', 
    5151    ); 
    5252 
     53  /** 
     54   * The file to store the history in 
     55   * @param string $historyFile 
     56   */ 
     57 
     58  protected $historyFile; 
     59 
    5360 
    5461  /** 
     
    6370      throw new sfException('You need the readline extension enabled to use the console'); 
    6471    } 
     72 
     73    // since we are in a task, current dir is the project root 
     74    $this->historyFile = 'cache/console.history'; 
    6575 
    6676    $this->namespace        = 'console'; 
     
    8999    set_error_handler(array($this, 'errorHandler')); 
    90100 
     101    $this->initHistory(); 
     102 
    91103    while ($line = trim((readline($this->getPrompt())))) 
    92104    { 
    93       readline_add_history($line); 
     105      $this->addHistoryLine($line); 
    94106      $this->nbLines++; 
    95107 
     
    120132      } 
    121133    } 
     134  } 
     135 
     136  /** 
     137   * Reads the history file (if any) and adds all lines 
     138   * to current history 
     139   */ 
     140 
     141  protected function initHistory() 
     142  { 
     143    if (!file_exists($this->historyFile)) 
     144    { 
     145      touch($this->historyFile); 
     146    } 
     147 
     148    $fp = fopen($this->historyFile, 'r'); 
     149    while ($line = trim(fgets($fp))) 
     150    { 
     151      if (!empty($line)) 
     152      { 
     153        readline_add_history($line); 
     154      } 
     155    } 
     156    fclose($fp); 
     157  } 
     158 
     159  /** 
     160   * Adds a line to the console history 
     161   * 
     162   * @param string $line 
     163   * @return boolean 
     164   */ 
     165 
     166  protected function addHistoryLine($line) 
     167  { 
     168    $fp = fopen($this->historyFile, 'a+'); 
     169    fputs($fp, $line.PHP_EOL); 
     170    fclose($fp); 
     171    return readline_add_history($line); 
    122172  } 
    123173 
     
    193243  protected function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) 
    194244  { 
    195     $this->logSection('console', (isset($map[$errno]) ? $map[$errno] : 'Unknown Error').': '.$errstr)
     245    echo (isset($this->errorNames[$errno]) ? $this->errorNames[$errno] : 'Unknown Error').': '.$errstr
    196246 
    197247    return true;