|
9 | 9 | * file that was distributed with this source code.
|
10 | 10 | */
|
11 | 11 |
|
12 |
| -// Include this file if you are using the phar version of PHPStan. Since the phar version makes use of dynamic namespaces |
13 |
| -// the hack in registration.php to overload spl_autoload_register() and spl_autoload_unregister() does not work any more. |
14 |
| -\bitExpert\PHPStan\Magento\Autoload\Autoloader::register(); |
| 12 | +use bitExpert\PHPStan\Magento\Autoload\Cache\FileCacheStorage; |
| 13 | +use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader; |
| 14 | +use bitExpert\PHPStan\Magento\Autoload\MockAutoloader; |
| 15 | +use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader; |
| 16 | +use Nette\Neon\Neon; |
| 17 | +use PHPStan\Cache\Cache; |
| 18 | + |
| 19 | +// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation |
| 20 | +(function (array $argv = []) { |
| 21 | + // Sadly we don't have access to the parsed phpstan.neon configuration at this point we need to look up the |
| 22 | + // location of the config file and parse it with the Neon parser to be able to extract the tmpDir definition! |
| 23 | + $configFile = ''; |
| 24 | + if (count($argv) > 0) { |
| 25 | + foreach($argv as $idx => $value) { |
| 26 | + if ((strtolower($value) === '-c') && isset($argv[$idx + 1])) { |
| 27 | + $configFile = $argv[$idx + 1]; |
| 28 | + break; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (empty($configFile)) { |
| 34 | + $currentWorkingDirectory = getcwd(); |
| 35 | + foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) { |
| 36 | + $discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName; |
| 37 | + if (file_exists($discoverableConfigFile) && is_readable(($discoverableConfigFile))) { |
| 38 | + $configFile = $discoverableConfigFile; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + $tmpDir = sys_get_temp_dir() . '/phpstan'; |
| 45 | + if (!empty($configFile)) { |
| 46 | + $neonConfig = Neon::decode(file_get_contents($configFile)); |
| 47 | + if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) { |
| 48 | + $tmpDir = $neonConfig['parameters']['tmpDir']; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + $cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan')); |
| 53 | + |
| 54 | + $mockAutoloader = new MockAutoloader(); |
| 55 | + $factoryAutoloader = new FactoryAutoloader($cache); |
| 56 | + $proxyAutoloader = new ProxyAutoloader($cache); |
| 57 | + |
| 58 | + \spl_autoload_register([$mockAutoloader, 'autoload'], true, true); |
| 59 | + \spl_autoload_register([$factoryAutoloader, 'autoload'], true, false); |
| 60 | + \spl_autoload_register([$proxyAutoloader, 'autoload'], true, false); |
| 61 | +})($GLOBALS['argv'] ?? []); |
0 commit comments