NYCPHP Meetup

NYPHP.org

[nycphp-talk] Finding instances of include file

Mark Armendariz lists at enobrev.com
Thu Jul 26 17:01:13 EDT 2007


Jon Baer wrote:
> Nice even a MacPort for it ...
>
> [MacBookPro:~]$ port search rpl
> rpl                            textproc/rpl   1.4.0        Rpl is a 
> Unix replacement utility
>
> Handy stuff ... thanks.
>
> - Jon

Rpl is nice...

I went ahead and got carried away with a php5 solution...  I know it 
could be refactored pretty far down, using filters and a recursive 
directory iterator, but f-it, i need to actually get some work done this 
evening.  Sure would be great to have the time to make a refactoring 
library, no?  Or even pick up some java to do such things for zend's 
eclipse project (pdt).  one can dream... but not too long lest he end up 
homeless.

<?php  
    function getExtension($sFile) {
        return array_pop(explode('.', $sFile));
    }
   
    function findFiles($sDir, $sPattern, $aFiles = array()) {
        $oFiles = new DirectoryIterator($sDir);
       
        foreach ($oFiles as $oFile) {
            if ($oFile->isDir()) {
                if ($oFile->isDot() === false
                &&  $oFile->getFilename() != '.svn'     // Shake fists 
violently at subversion directories
                &&  $oFile->getFilename() != '.bzr') { // Shake fists 
violently at bazaar directories
                    $aFiles = findFiles($oFile->getPathname(), 
$sPattern, $aFiles);
                }
            } else if (getExtension($oFile->getFilename()) == 'php') {
                $sCode  = file_get_contents($oFile->getPathname());
                if (preg_match_all($sPattern, $sCode, $aMatches)) {
                    $aFiles[$oFile->getPathname()] = $aMatches[0];
                }
            }
        }

        return $aFiles;
    }
   
    function findIncludes($sDir) {
        return findFiles($sDir, 
'/(include|require)_?(once)?\s+(?:\'([^\']+)\'|"([^"]+)")/');
    }
   
    function findClasses($sDir) {
        return findFiles($sDir, 
'/class\s+([a-zA-Z0-9]+)(\s+extends\s+([a-zA-Z0-9]+))?\s?{/');
    }

    function dbg($var) {
        echo '<pre>' . print_r($var, 1) . '</pre>';
    }
   
    dbg(findIncludes('D:/W/Enobrev/Library/'));
    dbg(findClasses('D:/W/Enobrev/Library/'));
?>

Have a funtastic evening...

Mark



More information about the talk mailing list