|
13 | 13 | use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader; |
14 | 14 | use bitExpert\PHPStan\Magento\Autoload\MockAutoloader; |
15 | 15 | use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader; |
| 16 | +use Nette\Neon\Neon; |
16 | 17 | use PHPStan\Cache\Cache; |
17 | 18 |
|
18 | 19 | // This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation |
19 | | -(function () { |
20 | | - $cache = new Cache(new FileCacheStorage(sys_get_temp_dir() . '/phpstan/cache/PHPStan')); |
| 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')); |
21 | 53 |
|
22 | 54 | $mockAutoloader = new MockAutoloader(); |
23 | 55 | $factoryAutoloader = new FactoryAutoloader($cache); |
|
26 | 58 | \spl_autoload_register([$mockAutoloader, 'autoload'], true, true); |
27 | 59 | \spl_autoload_register([$factoryAutoloader, 'autoload'], true, false); |
28 | 60 | \spl_autoload_register([$proxyAutoloader, 'autoload'], true, false); |
29 | | -})(); |
| 61 | +})($GLOBALS['argv'] ?? []); |
0 commit comments