Skip to content

Instantly share code, notes, and snippets.

@JanMikes
Created May 17, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanMikes/08fa6ba181f44285266684c278eae78d to your computer and use it in GitHub Desktop.
Save JanMikes/08fa6ba181f44285266684c278eae78d to your computer and use it in GitHub Desktop.
PHP - Replace all classes as strings usages in project
#!/usr/bin/php
<?php declare(strict_types=1);
use Nette\Loaders\RobotLoader;
use Nette\Utils\FileSystem as NetteFileSystem;
use Nette\Utils\Finder;
use Nette\Utils\Strings;
require __DIR__ . '/../vendor/autoload.php';
// Delete RobotLoader's cache because of next runs
$tempDir = __DIR__ . '/../temp/cache/PSR_4_Classes_as_string';
NetteFileSystem::delete($tempDir);
$pathsToProcess = [
__DIR__ . '/../src',
__DIR__ . '/../app',
__DIR__ . '/../tests',
__DIR__ . '/../tests_db',
__DIR__ . '/../tests_selenium',
__DIR__ . '/../www',
];
$robotLoader = new RobotLoader();
$robotLoader->addDirectory($pathsToProcess)
->setTempDirectory($tempDir)
->register();
$classes = array_keys($robotLoader->getIndexedClasses());
$files = Finder::findFiles('*.php')->from($pathsToProcess);
foreach ($files as $file) {
$filePath = $file->getPathname();
$content = NetteFileSystem::read($filePath);
foreach ($classes as $class) {
$pattern = '/["\']\\?' . str_replace('\\', '\\\\', $class) . '["\']/m';
$match = Strings::match($content, $pattern);
if ($match !== null) {
echo "[$filePath] $class\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment