2020-12-26 00:17:31 +01:00
< ? php
declare ( strict_types = 1 );
2020-12-26 12:21:15 +01:00
use Nette\Utils\DateTime ;
2020-12-27 21:31:30 +01:00
use Nette\Utils\Strings ;
2020-12-26 00:17:31 +01:00
use Rector\Compiler\PhpScoper\StaticEasyPrefixer ;
2020-12-28 23:09:11 +01:00
use Rector\Compiler\Unprefixer ;
2020-12-26 00:17:31 +01:00
use Rector\Compiler\ValueObject\ScoperOption ;
require_once __DIR__ . '/vendor/autoload.php' ;
// [BEWARE] this path is relative to the root and location of this file
$filePathsToSkip = [
// @see https://github.com/rectorphp/rector/issues/2852#issuecomment-586315588
'vendor/symfony/deprecation-contracts/function.php'
];
// remove phpstan, because it is already prefixed in its own scope
2020-12-26 12:21:15 +01:00
$dateTime = DateTime :: from ( 'now' );
2020-12-26 22:04:46 +01:00
$timestamp = $dateTime -> format ( 'Ymd' );
2020-12-26 00:17:31 +01:00
// see https://github.com/humbug/php-scoper
return [
2020-12-26 12:21:15 +01:00
ScoperOption :: PREFIX => 'RectorPrefix' . $timestamp ,
2020-12-26 00:17:31 +01:00
ScoperOption :: FILES_WHITELIST => $filePathsToSkip ,
ScoperOption :: WHITELIST => StaticEasyPrefixer :: getExcludedNamespacesAndClasses (),
ScoperOption :: PATCHERS => [
// [BEWARE] $filePath is absolute!
2021-03-15 01:30:02 +07:00
// fixes https://github.com/rectorphp/rector-prefixed/runs/2103759172
// and https://github.com/rectorphp/rector-prefixed/blob/0cc433e746b645df5f905fa038573c3a1a9634f0/vendor/jean85/pretty-package-versions/src/PrettyVersions.php#L6
function ( string $filePath , string $prefix , string $content ) : string {
2021-03-15 02:49:55 +07:00
if ( ! Strings :: endsWith ( $filePath , 'vendor/jean85/pretty-package-versions/src/PrettyVersions.php' )) {
2021-03-15 01:30:02 +07:00
return $content ;
}
// see https://regex101.com/r/v8zRMm/1
return Strings :: replace ( $content , '#' . $prefix . '\\\\Composer\\\\InstalledVersions#' , 'Composer\InstalledVersions' );
},
2020-12-28 23:09:11 +01:00
// unprefix string classes, as they're string on purpose - they have to be checked in original form, not prefixed
function ( string $filePath , string $prefix , string $content ) : string {
// skip vendor
if ( Strings :: contains ( $filePath , 'vendor/' )) {
return $content ;
}
// skip bin/rector.php for composer autoload class
if ( Strings :: endsWith ( $filePath , 'bin/rector.php' )) {
return $content ;
}
// skip scoper-autoload
if ( Strings :: endsWith ( $filePath , 'vendor/scoper-autoload.php' )) {
return $content ;
}
return Unprefixer :: unprefixQuoted ( $content , $prefix );
},
2020-12-27 21:31:30 +01:00
// scoper missed PSR-4 autodiscovery in Symfony
2020-12-26 00:17:31 +01:00
function ( string $filePath , string $prefix , string $content ) : string {
2020-12-27 22:14:39 +01:00
// scoper missed PSR-4 autodiscovery in Symfony
if ( ! Strings :: endsWith ( $filePath , 'config.php' ) && ! Strings :: endsWith ( $filePath , 'services.php' )) {
2020-12-27 21:31:30 +01:00
return $content ;
}
// skip "Rector\\" namespace
if ( Strings :: contains ( $content , '$services->load(\'Rector' )) {
return $content ;
}
return Strings :: replace ( $content , '#services\->load\(\'#' , 'services->load(\'' . $prefix . '\\' );
2020-12-27 22:14:39 +01:00
},
2020-12-26 00:17:31 +01:00
],
];