home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHP / Beautifier / Common.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.0 KB  |  239 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * PHP_Beautifier_Common and PHP_Beautifier_Interface
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. * @category   PHP
  14. * @package PHP_Beautifier
  15. * @author Claudio Bustos <cdx@users.sourceforge.com>
  16. * @copyright  2004-2006 Claudio Bustos
  17. * @link     http://pear.php.net/package/PHP_Beautifier
  18. * @link     http://beautifyphp.sourceforge.net
  19. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20. * @version    CVS: $Id:$
  21. */
  22. /**
  23. * Wraps commons method por PHP_Beautifier
  24. *
  25. * Common methods for PHP_Beautifier, almost file management.
  26. * All the methods are static
  27. *
  28. * @category   PHP
  29. * @package PHP_Beautifier
  30. * @author Claudio Bustos <cdx@users.sourceforge.com>
  31. * @copyright  2004-2006 Claudio Bustos
  32. * @link     http://pear.php.net/package/PHP_Beautifier
  33. * @link     http://beautifyphp.sourceforge.net
  34. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  35. * @version    Release: 0.1.14
  36. */
  37. class PHP_Beautifier_Common {
  38.     /**
  39.     * Normalize reference to directories
  40.     * @param  string path to directory
  41.     * @return string normalized path to directory
  42.     */
  43.     public static function normalizeDir($sDir) 
  44.     {
  45.         $sDir = str_replace(DIRECTORY_SEPARATOR, '/', $sDir);
  46.         if (substr($sDir, -1) != '/') {
  47.             $sDir.= '/';
  48.         }
  49.         return $sDir;
  50.     }
  51.     /**
  52.     * Search, inside a dir, for a file pattern, using regular expresion
  53.     * Example:
  54.     *
  55.     * <code>PHP_Beautifier_Common::getFilesByPattern('.','*.php',true);</code>
  56.     * Search recursively for all the files with php extensions
  57.     * in the current dir
  58.     * @param    string  path to a dir
  59.     * @param    string  file pattern
  60.     * @param    bool    recursive?
  61.     * @return   array   path to files
  62.     */
  63.     public static function getFilesByPattern($sDir, $sFilePattern, $bRecursive = false) 
  64.     {
  65.         if (substr($sDir, -1) == '/') {
  66.             $sDir = substr($sDir, 0, -1);
  67.         }
  68.         $dh = @opendir($sDir);
  69.         if (!$dh) {
  70.             throw (new Exception("Cannot open directory '$sDir'"));
  71.         }
  72.         $matches = array();
  73.         while ($entry = @readdir($dh)) {
  74.             if ($entry == '.' or $entry == '..') {
  75.                 continue;
  76.             } elseif (is_dir($sDir.'/'.$entry) and $bRecursive) {
  77.                 $matches = array_merge($matches, PHP_Beautifier_Common::getFilesByPattern($sDir.'/'.$entry, $sFilePattern, $bRecursive));
  78.             } elseif (preg_match("/".$sFilePattern."$/", $entry)) {
  79.                 $matches[] = $sDir."/".$entry;
  80.             }
  81.         }
  82.         if (!$matches) {
  83.             PHP_Beautifier_Common::getLog()->log("$sDir/$sFilePattern pattern don't match any file", PEAR_LOG_DEBUG);
  84.         }
  85.         return $matches;
  86.     }
  87.     /**
  88.     * Create a dir for a file path
  89.     * @param    string  file path
  90.     * @return   bool
  91.     * @throws   Exception
  92.     */
  93.     public static function createDir($sFile) 
  94.     {
  95.         $sDir = dirname($sFile);
  96.         if (file_exists($sDir)) {
  97.             return true;
  98.         } else {
  99.             $aPaths = explode('/', $sDir);
  100.             $sCurrentPath = '';
  101.             foreach($aPaths as $sPartialPath) {
  102.                 $sCurrentPath.= $sPartialPath.'/';
  103.                 if (file_exists($sCurrentPath)) {
  104.                     continue;
  105.                 } else {
  106.                     if (!@mkdir($sCurrentPath)) {
  107.                         throw (new Exception("Can't create directory '$sCurrentPath'"));
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.         return true;
  113.     }
  114.     /**
  115.     * Return an array with the paths to save for an array of files
  116.     * @param    array  Array of files (input)
  117.     * @param    string Init path
  118.     * @return   array  Array of files (output)
  119.     */
  120.     public static function getSavePath($aFiles, $sPath = './') 
  121.     {
  122.         $sPath = PHP_Beautifier_Common::normalizeDir($sPath);
  123.         // get the lowest denominator..
  124.         $sPrevious = '';
  125.         $iCut = 0;
  126.         foreach($aFiles as $i=>$sFile) {
  127.             $sFile = preg_replace("/^.*?#/", '', $sFile);
  128.             $aFiles[$i] = $sFile;
  129.             if (!$sPrevious) {
  130.                 $sPrevious = dirname($sFile);
  131.                 continue;
  132.             }
  133.             $aPreviousParts=explode("/",$sPrevious);
  134.             $aCurrentParts=explode("/",dirname($sFile));
  135.             for($x=0;$x<count($aPreviousParts);$x++) {
  136.                 if($aPreviousParts[$x]!=$aCurrentParts[$x]) {
  137.                     $sPrevious=implode("/",array_slice($aPreviousParts,0,$x));                    
  138.                 }
  139.             }
  140.         }
  141.         $iCut = strlen($sPrevious);
  142.         $aPathsOut = array();
  143.         foreach($aFiles as $sFile) {
  144.             $sFileOut = preg_replace("/^(\w:\/|\.\/|\/)/", "", substr($sFile, $iCut));
  145.             $aPathsOut[] = $sPath.$sFileOut;
  146.         }
  147.         return $aPathsOut;
  148.     }
  149.     /**
  150.     * Search, inside a dir, for a file pattern using glob(* and ?)
  151.     * @param    string  path
  152.     * @param    bool    recursive
  153.     * @return   array   path to files
  154.     */
  155.     public static function getFilesByGlob($sPath, $bRecursive = false) 
  156.     {
  157.         if (!$bRecursive) {
  158.             return glob($sPath);
  159.         } else {
  160.             $sDir = (dirname($sPath)) ? realpath(dirname($sPath)) : realpath('./');
  161.             $sDir = PHP_Beautifier_Common::normalizeDir($sDir);
  162.             $sDir = substr($sDir, 0, -1); // strip last slash
  163.             $sGlob = basename($sPath);
  164.             $dh = @opendir($sDir);
  165.             if (!$dh) {
  166.                 throw (new Exception("Cannot open directory '$sPath'"));
  167.             }
  168.             $aMatches = glob($sDir.'/'.$sGlob);
  169.             while ($entry = @readdir($dh)) {
  170.                 if ($entry == '.' or $entry == '..') {
  171.                     continue;
  172.                 } elseif (is_dir($sDir.'/'.$entry)) {
  173.                     $aMatches = array_merge($aMatches, PHP_Beautifier_Common::getFilesByGlob($sDir.'/'.$entry.'/'.$sGlob, true));
  174.                 }
  175.             }
  176.             return $aMatches;
  177.         }
  178.     }
  179.     /**
  180.     * Get a {@link Log_composite} object for PHP_Beautifier
  181.     * Always return the same object (Singleton pattern)
  182.     * @return Log_composite
  183.     */
  184.     public static function getLog() 
  185.     {
  186.         return Log::singleton('composite', 'PHP_Beautifier');
  187.     }
  188.     /**
  189.     * Transform whitespaces into its representation
  190.     * So, tabs becomes \t, newline \n and feed \r
  191.     * Useful for log
  192.     * @param string
  193.     * @return string
  194.     */
  195.     public static function wsToString($sText) 
  196.     {
  197.         // ArrayNested->off();
  198.         return str_replace(array("\r", "\n", "\t"), array('\r', '\n', '\t'), $sText);
  199.         // ArrayNested->on();
  200.         
  201.     }
  202. }
  203. // Interfaces
  204.  
  205. /**
  206. * Interface for PHP_Beautifier and subclasses.
  207. * Created to made a 'legal' Decorator implementation
  208. *
  209. * @category   PHP
  210. * @package PHP_Beautifier
  211. * @author Claudio Bustos <cdx@users.sourceforge.com>
  212. * @copyright  2004-2006 Claudio Bustos
  213. * @link     http://pear.php.net/package/PHP_Beautifier
  214. * @link     http://beautifyphp.sourceforge.net
  215. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  216. * @version    Release: 0.1.14
  217. */
  218. interface PHP_Beautifier_Interface {
  219.     /**
  220.     * Process the file(s) or string
  221.     */
  222.     public function process();
  223.     /**
  224.     * Show on screen the output
  225.     */
  226.     public function show();
  227.     /**
  228.     * Get the output on a string
  229.     * @return string
  230.     */
  231.     public function get();
  232.     /**
  233.     * Save the output to a file
  234.     * @param string path to file
  235.     */
  236.     public function save($sFile = null);
  237. }
  238. ?>
  239.