function testaddFilterDirectory() { $sDir = PHP_Beautifier_Common::normalizeDir(dirname(__FILE__)); $this->oBeaut->addFilterDirectory($sDir); $aDirs = $this->oBeaut->getFilterDirectories(); $this->assertEquals(end($aDirs), $sDir); }
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(); $aOutputFiles = PHP_Beautifier_Common::getSavePath($aInputFiles); for ($x = 0; $x < count($aInputFiles); $x++) { $this->beautifierSetInputFile($aInputFiles[$x]); $this->beautifierProcess(); $this->oTar->addString($aOutputFiles[$x], $this->beautifierGet()); } }
public function save() { $aInputFiles = $this->oBatch->getInputFiles(); $sOutputPath = $this->oBatch->getOutputPath(); $aOutputFiles = PHP_Beautifier_Common::getSavePath($aInputFiles, $sOutputPath); for ($x = 0; $x < count($aInputFiles); $x++) { unset($oTar); $oTar = $this->getTar($aOutputFiles[$x]); $this->beautifierSetInputFile($aInputFiles[$x]); $this->beautifierProcess(); PHP_Beautifier_Common::createDir($aOutputFiles[$x]); $oTar->addString(basename($aOutputFiles[$x]), $this->beautifierGet()); } return true; }
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; }
/** * __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(); }
/** * getBatchEngine * * @access private * @return void */ private function getBatchEngine() { $sCompress = $this->sCompress ? ucfirst($this->sCompress) : ''; $sClass = $this->sOutputMode . $sCompress; $sClassEngine = 'PHP_Beautifier_Batch_Output_' . $sClass; $sClassFile = PHP_Beautifier_Common::normalizeDir(dirname(__FILE__)) . 'Batch/Output/' . $sClass . '.php'; if (!file_exists($sClassFile)) { throw new Exception("Doesn't exists file definition for {$sClass} ({$sClassFile})"); } else { include_once $sClassFile; if (!class_exists($sClassEngine)) { throw new Exception("{$sClassFile} exists, but {$sClassEngine} isn't defined"); } else { return new $sClassEngine($this); } } }
/** * addHeaderComment * * @access public * @return void */ function addHeaderComment() { if (!($sLicense = $this->getSetting('add_header'))) { return; } // if Header is a path, try to load the file if (file_exists($sLicense)) { $sDataPath = $sLicense; } else { $sDataPath = PHP_Beautifier_Common::normalizeDir(CODEFORMATTER_LIBPATH) . 'Beautifier/Licenses/' . $sLicense . '.txt'; } if (file_exists($sDataPath)) { $sLicenseText = file_get_contents($sDataPath); } else { throw new Exception("Can't load license '" . $sLicense . "'"); } $this->oBeaut->removeWhitespace(); $this->oBeaut->addNewLine(); $this->oBeaut->add($sLicenseText); $this->oBeaut->addNewLineIndent(); }
/** * Return the whitespace previous to current token * Ex.: You have * ' if($a)' * if you call this funcion on 'if', you get ' ' * @todo implements a more economic way to handle this. * @return string previous whitespace */ public function getPreviousWhitespace() { $sWhiteSpace = ''; for ($x = $this->iCount - 1; $x >= 0; $x--) { $this->oLog->log("sp n:{$x}", PEAR_LOG_DEBUG); $aToken = $this->getToken($x); if (is_array($aToken)) { if ($aToken[0] == T_WHITESPACE) { $sWhiteSpace .= $aToken[1]; } elseif (preg_match("/([\\s\r\n]+)\$/", $aToken[1], $aMatch)) { $sWhiteSpace .= $aMatch[0]; // ArrayNested->off(); $this->oLog->log("+space-token-with-sp:[" . PHP_Beautifier_Common::wsToString($sWhiteSpace) . "]", PEAR_LOG_DEBUG); // ArrayNested->on(); return $sWhiteSpace; } } else { $this->oLog->log("+space-token-without-sp:[" . PHP_Beautifier_Common::wsToString($sWhiteSpace) . "]", PEAR_LOG_DEBUG); return $sWhiteSpace; } } // Strange, but... $this->oLog->log("+space:[" . PHP_Beautifier_Common::wsToString($sWhiteSpace) . "]", PEAR_LOG_DEBUG); return $sWhiteSpace; }
/** * 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; }
/** * Search, inside a dir, for a file pattern using glob(* and ?) * @param string path * @param bool recursive * @return array path to files */ public static function getFilesByGlob($sPath, $bRecursive = false) { if (!$bRecursive) { return glob($sPath); } else { $sDir = dirname($sPath) ? realpath(dirname($sPath)) : realpath('./'); $sGlob = basename($sPath); $dh = @opendir($sDir); if (!$dh) { throw new Exception("Cannot open directory '{$sPath}'"); } $aMatches = glob($sDir . '/' . $sGlob); while ($entry = @readdir($dh)) { if ($entry == '.' or $entry == '..') { continue; } elseif (is_dir($sDir . '/' . $entry)) { $aMatches = array_merge($aMatches, PHP_Beautifier_Common::getFilesByGlob($sDir . '/' . $entry . '/' . $sGlob, true)); } } return $aMatches; } }
/** * 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)) {
function testWsToString() { $this->assertEquals(' \\t\\r\\n', PHP_Beautifier_Common::wsToString(" \t\r\n")); }