function __call($sMethod, $aArgs) { if (!is_array($aArgs) or count($aArgs) != 1) { throw new Exception('Call to Filter::__call with wrong argument'); } PHP_Beautifier_Common::getLog()->log('Default Filter:unhandled[' . $aArgs[0] . ']', PEAR_LOG_DEBUG); $this->oBeaut->add($aArgs[0]); }
public function save() { $aInputFiles = $this->oBatch->getInputFiles(); $sOutputPath = $this->oBatch->getOutputPath(); $aOutputFiles = PHP_Beautifier_Common::getSavePath($aInputFiles, $sOutputPath); $oLog = PHP_Beautifier_Common::getLog(); for ($x = 0; $x < count($aInputFiles); $x++) { try { $this->beautifierSetInputFile($aInputFiles[$x]); $this->beautifierProcess(); PHP_Beautifier_Common::createDir($aOutputFiles[$x]); $this->beautifierSave($aOutputFiles[$x]); } catch (Exception $oExp) { $oLog->log($oExp->getMessage(), PEAR_LOG_ERR); } } return true; }
/** * Search, inside a dir, for a file pattern, using regular expresion * Example: * * <code>PHP_Beautifier_Common::getFilesByPattern('.','*.php',true);</code> * Search recursively for all the files with php extensions * in the current dir * @param string path to a dir * @param string file pattern * @param bool recursive? * @return array path to files */ public static function getFilesByPattern($sDir, $sFilePattern, $bRecursive = false) { if (substr($sDir, -1) == '/') { $sDir = substr($sDir, 0, -1); } $dh = @opendir($sDir); if (!$dh) { throw new Exception("Cannot open directory '{$sDir}'"); } $matches = array(); while ($entry = @readdir($dh)) { if ($entry == '.' or $entry == '..') { continue; } elseif (is_dir($sDir . '/' . $entry) and $bRecursive) { $matches = array_merge($matches, PHP_Beautifier_Common::getFilesByPattern($sDir . '/' . $entry, $sFilePattern, $bRecursive)); } elseif (preg_match("/" . $sFilePattern . "\$/", $entry)) { $matches[] = $sDir . "/" . $entry; } } if (!$matches) { PHP_Beautifier_Common::getLog()->log("{$sDir}/{$sFilePattern} pattern don't match any file", PEAR_LOG_DEBUG); } return $matches; }
/** * __construct * * @param PHP_Beautifier $oBeaut PHP_Beautifier Object * @param array $aSettings Settings for the PHP_Beautifier * * @access public * @return void */ public function __construct(PHP_Beautifier $oBeaut, $aSettings = array()) { parent::__construct($oBeaut, $aSettings); $this->_oLog = PHP_Beautifier_Common::getLog(); }
/** * Constructor. * Assing values to {@link $aControlStructures},{@link $aControlStructuresEnd}, * {@link $aTokenFunctions} */ public function __construct() { $this->aControlStructures = array(T_CLASS, T_FUNCTION, T_IF, T_ELSE, T_ELSEIF, T_WHILE, T_DO, T_FOR, T_FOREACH, T_SWITCH, T_DECLARE, T_TRY, T_CATCH); $this->aControlStructuresEnd = array(T_ENDWHILE, T_ENDFOREACH, T_ENDFOR, T_ENDDECLARE, T_ENDSWITCH, T_ENDIF); $aPreTokens = preg_grep('/^T_/', array_keys(get_defined_constants())); foreach ($aPreTokens as $sToken) { $this->aTokenNames[constant($sToken)] = $sToken; $this->aTokenFunctions[constant($sToken)] = $sToken; } $aTokensToChange = array('"' => "T_DOUBLE_QUOTE", "'" => "T_SINGLE_QUOTE", '(' => 'T_PARENTHESIS_OPEN', ')' => 'T_PARENTHESIS_CLOSE', ';' => 'T_SEMI_COLON', '{' => 'T_OPEN_BRACE', '}' => 'T_CLOSE_BRACE', ',' => 'T_COMMA', '?' => 'T_QUESTION', ':' => 'T_COLON', '=' => 'T_ASSIGMENT', '<' => 'T_EQUAL', '>' => 'T_EQUAL', '.' => 'T_DOT', '[' => 'T_OPEN_SQUARE_BRACE', ']' => 'T_CLOSE_SQUARE_BRACE', '+' => 'T_OPERATOR', '-' => 'T_OPERATOR', '*' => 'T_OPERATOR', '/' => 'T_OPERATOR', '%' => 'T_OPERATOR', '&' => 'T_OPERATOR', '|' => 'T_OPERATOR', '^' => 'T_OPERATOR', '~' => 'T_OPERATOR', T_SL => 'T_OPERATOR', T_SR => 'T_OPERATOR', T_OBJECT_OPERATOR => 'T_OBJECT_OPERATOR', T_INCLUDE => 'T_INCLUDE', T_INCLUDE_ONCE => 'T_INCLUDE', T_REQUIRE => 'T_INCLUDE', T_REQUIRE_ONCE => 'T_INCLUDE', T_FUNCTION => 'T_LANGUAGE_CONSTRUCT', T_PRINT => 'T_LANGUAGE_CONSTRUCT', T_RETURN => 'T_LANGUAGE_CONSTRUCT', T_ECHO => 'T_LANGUAGE_CONSTRUCT', T_NEW => 'T_LANGUAGE_CONSTRUCT', T_CLASS => 'T_LANGUAGE_CONSTRUCT', T_VAR => 'T_LANGUAGE_CONSTRUCT', T_GLOBAL => 'T_LANGUAGE_CONSTRUCT', T_THROW => 'T_LANGUAGE_CONSTRUCT', T_IF => 'T_CONTROL', T_DO => 'T_CONTROL', T_WHILE => 'T_CONTROL', T_SWITCH => 'T_CONTROL', T_ELSEIF => 'T_ELSE', T_ELSE => 'T_ELSE', T_INTERFACE => 'T_ACCESS', T_FINAL => 'T_ACCESS', T_ABSTRACT => 'T_ACCESS', T_PRIVATE => 'T_ACCESS', T_PUBLIC => 'T_ACCESS', T_PROTECTED => 'T_ACCESS', T_CONST => 'T_ACCESS', T_STATIC => 'T_ACCESS', T_PLUS_EQUAL => 'T_ASSIGMENT_PRE', T_MINUS_EQUAL => 'T_ASSIGMENT_PRE', T_MUL_EQUAL => 'T_ASSIGMENT_PRE', T_DIV_EQUAL => 'T_ASSIGMENT_PRE', T_CONCAT_EQUAL => 'T_ASSIGMENT_PRE', T_MOD_EQUAL => 'T_ASSIGMENT_PRE', T_AND_EQUAL => 'T_ASSIGMENT_PRE', T_OR_EQUAL => 'T_ASSIGMENT_PRE', T_XOR_EQUAL => 'T_ASSIGMENT_PRE', T_DOUBLE_ARROW => 'T_ASSIGMENT', T_SL_EQUAL => 'T_EQUAL', T_SR_EQUAL => 'T_EQUAL', T_IS_EQUAL => 'T_EQUAL', T_IS_NOT_EQUAL => 'T_EQUAL', T_IS_IDENTICAL => 'T_EQUAL', T_IS_NOT_IDENTICAL => 'T_EQUAL', T_IS_SMALLER_OR_EQUAL => 'T_EQUAL', T_IS_GREATER_OR_EQUAL => 'T_EQUAL', T_LOGICAL_OR => 'T_LOGICAL', T_LOGICAL_XOR => 'T_LOGICAL', T_LOGICAL_AND => 'T_LOGICAL', T_BOOLEAN_OR => 'T_LOGICAL', T_BOOLEAN_AND => 'T_LOGICAL', T_ENDWHILE => 'T_END_SUFFIX', T_ENDFOREACH => 'T_END_SUFFIX', T_ENDFOR => 'T_END_SUFFIX', T_ENDDECLARE => 'T_END_SUFFIX', T_ENDSWITCH => 'T_END_SUFFIX', T_ENDIF => 'T_END_SUFFIX'); foreach ($aTokensToChange as $iToken => $sFunction) { $this->aTokenFunctions[$iToken] = $sFunction; } $this->addFilterDirectory(dirname(__FILE__) . '/Beautifier/Filter'); $this->addFilter('Default'); $this->oLog = PHP_Beautifier_Common::getLog(); }
/** * Function called from {@link PHP_Beautifier::process()} to process the tokens. * * If the received token is one of the keys of {@link $aFilterTokenFunctions} * a function with the same name of the value of that key is called. * If the method doesn't exists, {@link __call()} is called, and return * {@link PHP_Beautifier_Filter::BYPASS}. PHP_Beautifier, now, call the next Filter is its list. * If the method exists, it can return true or {@link PHP_Beautifier_Filter::BYPASS}. * * @param array $token Token to be handled * * @access public * @return bool true if the token is processed, false bypass to the next Filter * @see PHP_Beautifier::process() */ public function handleToken($token) { $this->aToken = $token; if (!$this->bOn) { return false; } $sMethod = $sValue = false; if (array_key_exists($token[0], $this->aFilterTokenFunctions)) { $sMethod = $this->aFilterTokenFunctions[$token[0]]; //$sValue = $token[1]; } elseif ($this->oBeaut->getTokenFunction($token[0])) { $sMethod = $this->oBeaut->getTokenFunction($token[0]); } $sValue = $token[1]; if ($sMethod) { PHP_Beautifier_Common::getLog()->log($this->getName() . "->" . $sMethod . "(" . trim($sValue) . ")", PEAR_LOG_DEBUG); // return false if PHP_Beautifier_Filter::BYPASS $result = $this->{$sMethod}($sValue) !== PHP_Beautifier_Filter::BYPASS; if ($result) { PHP_Beautifier_Common::getLog()->log($this->getName() . "->" . $sMethod . " done", PEAR_LOG_DEBUG); } return $result; } else { // WEIRD!!! -> Add the same received $this->oBeaut->add($token[1]); PHP_Beautifier_Common::getLog()->log("Add same received:" . trim($token[1]), PEAR_LOG_DEBUG); return true; } // never go here return false; }
/** * Function called from {@link PHP_Beautifier::process()} to process the tokens. * * If the received token is one of the keys of {@link $aFilterTokenFunctions} * a function with the same name of the value of that key is called. * If the method doesn't exists, {@link __call()} is called, and return * {@link PHP_Beautifier_Filter::BYPASS}. PHP_Beautifier, now, call the next Filter is its list. * If the method exists, it can return true or {@link PHP_Beautifier_Filter::BYPASS}. * @param array token * @return bool true if the token is processed, false bypass to the next Filter * @see PHP_Beautifier::process() */ public function handleToken($token) { $this->aToken = $token; if (!$this->bOn) { return false; } $sMethod = $sValue = false; if (array_key_exists($token[0], $this->aFilterTokenFunctions)) { $sMethod = $this->aFilterTokenFunctions[$token[0]]; //$sValue = $token[1]; } elseif ($this->oBeaut->getTokenFunction($token[0])) { $sMethod = $this->oBeaut->getTokenFunction($token[0]); } $sValue = $token[1]; if ($sMethod) { if ($this->oBeaut->iVerbose > 5) { echo $sMethod . ":" . trim($sValue) . PHP_EOL; } // return false if PHP_Beautifier_Filter::BYPASS return $this->{$sMethod}($sValue) !== PHP_Beautifier_Filter::BYPASS; } else { // WEIRD!!! -> Add the same received $this->oBeaut->add($token[1]); PHP_Beautifier_Common::getLog()->log("Add same received:" . trim($token[1]), PEAR_LOG_DEBUG); return true; } // never go here return false; }
/** * Require for PEAR System class */ require_once 'System.php'; /** * Require the beautify_php class.... */ require_once 'PHP/Beautifier.php'; /** * Require for PHP_Beautifier_Batch */ require_once 'PHP/Beautifier/Batch.php'; define('PHP_Beautifier_WINDOWS', substr(PHP_OS, 0, 3) == 'WIN'); error_reporting(E_ALL); // get log object $oLog = PHP_Beautifier_Common::getLog(); //default_options $aInputFiles = STDIN; $sOutputFile = STDOUT; $sIndentChar = ' '; $iIndentNumber = 4; $aFilters = array(); $bRecursive = false; $sCompress = false; $aFiltersDirectory = array(); $iVerbose = PEAR_LOG_WARNING; //end default_options $argv = Console_Getopt::readPHPArgv(); $aLongOptions = array('input=', 'output=', 'indent_tabs==', 'indent_spaces==', 'filters=', 'directory_filters=', 'recursive', 'help', 'compress==', 'verbose'); $options = Console_Getopt::getopt($argv, "f:o:d:l:t::s::c::r?hv", $aLongOptions); if (PEAR::isError($options)) {