-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79d2a70
commit 3684a49
Showing
2 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
$datePrefix = (new DateTimeImmutable('now'))->format('Ym'); | ||
$prefix = 'DEPTRAC_'.$datePrefix; | ||
|
||
// Path to the PHPStan PHAR | ||
$phpstanPharPath = __DIR__.'/../vendor/phpstan/phpstan/phpstan.phar'; | ||
|
||
// Temporary directory for extraction and scoped files | ||
$tempDir = __DIR__.'/deptrac-build/phpstan'; | ||
|
||
// Extract the PHAR | ||
$phar = new Phar($phpstanPharPath); | ||
$phar->extractTo($tempDir, null, true); | ||
|
||
// Apply scoping to the extracted files | ||
applyScoping($tempDir, $prefix); | ||
|
||
// Repackage the scoped files into a new PHAR | ||
repackagePhar($tempDir, $phpstanPharPath); | ||
|
||
// Clean up the temporary directory | ||
cleanup($tempDir); | ||
|
||
echo "Scoped PHPStan PHAR created successfully!\n"; | ||
|
||
// Function to apply scoping | ||
function applyScoping(string $directory, string $prefix): void | ||
{ | ||
$finder = new Symfony\Component\Finder\Finder(); | ||
$finder->files()->in($directory); | ||
|
||
foreach ($finder as $file) { | ||
$contents = file_get_contents($file->getRealPath()); | ||
$scopedContents = preg_replace('/^namespace\s+(.+?);/m', 'namespace '.$prefix.'$1;', $contents); | ||
file_put_contents($file->getRealPath(), $scopedContents); | ||
} | ||
} | ||
|
||
// Function to repackage PHAR | ||
function repackagePhar(string $directory, string $outputPhar): void | ||
{ | ||
$phar = new Phar($outputPhar); | ||
$phar->buildFromDirectory($directory); | ||
$phar->setStub($phar->createDefaultStub('bootstrap.php')); | ||
} | ||
|
||
// Function to clean up temporary directory | ||
function cleanup(string $directory): void | ||
{ | ||
$files = new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), | ||
RecursiveIteratorIterator::CHILD_FIRST | ||
); | ||
|
||
foreach ($files as $file) { | ||
if ($file->isDir()) { | ||
rmdir($file->getRealPath()); | ||
} else { | ||
unlink($file->getRealPath()); | ||
} | ||
} | ||
|
||
rmdir($directory); | ||
} |