Services\ModuleBuilder::SearchandReplace PHP Метод

SearchandReplace() публичный Метод

Recursively replace all occurences of string within a directory
public SearchandReplace ( string $dir, string $stringsearch, string $stringreplace ) : array
$dir string Source Directory
$stringsearch string String to search
$stringreplace string String to replace
Результат array
    public function SearchandReplace($dir, $stringsearch, $stringreplace)
    {
        $listDir = array();
        if ($handler = opendir($dir)) {
            while (($sub = readdir($handler)) !== false) {
                if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
                    if (is_file($dir . "/" . $sub)) {
                        if (substr_count($sub, '.php')) {
                            $getfilecontents = file_get_contents($dir . "/" . $sub);
                            if (substr_count($getfilecontents, $stringsearch) > 0) {
                                $replacer = str_replace($stringsearch, $stringreplace, $getfilecontents);
                                // Let's make sure the file exists and is writable first.
                                if (is_writable($dir . "/" . $sub)) {
                                    if (!($handle = fopen($dir . "/" . $sub, 'w'))) {
                                        // echo "Cannot open file (".$dir."/".$sub.")";
                                        exit;
                                    }
                                    // Write $somecontent to our opened file.
                                    if (fwrite($handle, $replacer) === false) {
                                        // echo "Cannot write to file (".$dir."/".$sub.")";
                                        exit;
                                    }
                                    // echo "Success, removed searched content from:".$dir."/".$sub."";
                                    fclose($handle);
                                } else {
                                    // echo "The file ".$dir."/".$sub." is not writable ";
                                }
                            }
                        }
                        $listDir[] = $sub;
                    } elseif (is_dir($dir . "/" . $sub)) {
                        $listDir[$sub] = $this->SearchandReplace($dir . "/" . $sub, $stringsearch, $stringreplace);
                    }
                }
            }
            closedir($handler);
        }
        return $listDir;
    }