Created
May 14, 2021 10:24
-
-
Save NickHatBoecker/13b585e4193c06abb7d768cf5b8f16b0 to your computer and use it in GitHub Desktop.
A simple recursive file looper in order to search for specific contents or to overwrite data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example: | |
* php index.php /var/www/my_directory_to_search | |
*/ | |
class Inputter | |
{ | |
/** @var string */ | |
private $searchDirectory; | |
/** | |
* @param string $searchDirectory | |
*/ | |
public function __construct($searchDirectory) | |
{ | |
$this->searchDirectory = $searchDirectory; | |
} | |
public function run(): void | |
{ | |
$filepaths = $this->getFilepaths(); | |
foreach ($filepaths as $filepath) { | |
// Get file input | |
$content = file_get_contents($filepath); | |
// @TODO Implement logic here | |
} | |
} | |
/** | |
* Get all valid filepaths from search directory | |
* | |
* @return array | |
*/ | |
private function getFilepaths(): array | |
{ | |
$files = []; | |
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->searchDirectory)); | |
foreach ($rii as $file) { | |
/** @var SplFileInfo#6 $file */ | |
if ($file->isDir()) { | |
continue; | |
} | |
$files[] = $file->getPathname(); | |
} | |
return $files; | |
} | |
} | |
$searchDirectory = $argv[1] ?? ''; | |
if ($searchDirectory) { | |
$inputter = new Inputter($searchDirectory); | |
$inputter->run(); | |
} else { | |
echo "\nERROR: Please provide a path to the search directory.\n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment