|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace RestartManagerE2E; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Collectors\Collector; |
| 8 | +use PHPStan\Node\CollectedDataNode; |
| 9 | +use PHPStan\Node\FileNode; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | + |
| 13 | +/** |
| 14 | + * Spawns a PHP process the way an extension compiling its own DI container |
| 15 | + * does. It runs in the main process after every worker has ended, so a worker |
| 16 | + * that took the composed ini with it leaves the spawned process with neither |
| 17 | + * the ini nor the scan dir - and without the extensions loaded from them. |
| 18 | + * |
| 19 | + * @implements Rule<CollectedDataNode> |
| 20 | + */ |
| 21 | +final class SpawnedProcessRule implements Rule |
| 22 | +{ |
| 23 | + |
| 24 | + public function getNodeType(): string |
| 25 | + { |
| 26 | + return CollectedDataNode::class; |
| 27 | + } |
| 28 | + |
| 29 | + public function processNode(Node $node, Scope $scope): array |
| 30 | + { |
| 31 | + $output = shell_exec(escapeshellarg(PHP_BINARY) . ' -r ' . escapeshellarg('echo json_encode(get_loaded_extensions());')); |
| 32 | + $spawnedExtensions = json_decode((string) $output, true); |
| 33 | + if (!is_array($spawnedExtensions)) { |
| 34 | + return [RuleErrorBuilder::message(sprintf('The spawned PHP process failed: %s', $output))->identifier('restartManager.spawn')->build()]; |
| 35 | + } |
| 36 | + |
| 37 | + $missing = array_diff(get_loaded_extensions(), $spawnedExtensions); |
| 38 | + if ($missing === []) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + |
| 42 | + return [ |
| 43 | + RuleErrorBuilder::message(sprintf( |
| 44 | + 'The spawned PHP process is missing extensions %s (PHPRC=%s, %s)', |
| 45 | + implode(', ', $missing), |
| 46 | + (string) getenv('PHPRC'), |
| 47 | + is_file((string) getenv('PHPRC')) ? 'exists' : 'does not exist', |
| 48 | + ))->identifier('restartManager.spawn')->build(), |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * CollectedDataNode rules only run when something was collected. |
| 56 | + * |
| 57 | + * @implements Collector<FileNode, true> |
| 58 | + */ |
| 59 | +final class FileCollector implements Collector |
| 60 | +{ |
| 61 | + |
| 62 | + public function getNodeType(): string |
| 63 | + { |
| 64 | + return FileNode::class; |
| 65 | + } |
| 66 | + |
| 67 | + public function processNode(Node $node, Scope $scope): bool |
| 68 | + { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments