|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection\BetterReflection\SourceLocator; |
| 4 | + |
| 5 | +use PHPStan\ShouldNotHappenException; |
| 6 | +use function stat; |
| 7 | +use function stream_wrapper_register; |
| 8 | +use function stream_wrapper_restore; |
| 9 | +use function stream_wrapper_unregister; |
| 10 | +use const SEEK_CUR; |
| 11 | +use const SEEK_END; |
| 12 | +use const SEEK_SET; |
| 13 | +use const STREAM_URL_STAT_QUIET; |
| 14 | + |
| 15 | +/** |
| 16 | + * This class will operate as a stream wrapper, intercepting any access to a file while |
| 17 | + * in operation. |
| 18 | + * |
| 19 | + * @internal DO NOT USE: this is an implementation detail of |
| 20 | + * the {@see \PHPStan\BetterReflection\SourceLocator\Type\AutoloadSourceLocator} |
| 21 | + * |
| 22 | + * phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 23 | + * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 24 | + * phpcs:disable Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 25 | + */ |
| 26 | +final class FileReadTrapStreamWrapper |
| 27 | +{ |
| 28 | + |
| 29 | + private const DEFAULT_STREAM_WRAPPER_PROTOCOLS = [ |
| 30 | + 'file', |
| 31 | + 'phar', |
| 32 | + ]; |
| 33 | + |
| 34 | + /** @var string[]|null */ |
| 35 | + private static ?array $registeredStreamWrapperProtocols; |
| 36 | + |
| 37 | + public static ?string $autoloadLocatedFile = null; |
| 38 | + |
| 39 | + private bool $readFromFile = false; |
| 40 | + |
| 41 | + private int $seekPosition = 0; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param string[] $streamWrapperProtocols |
| 45 | + * |
| 46 | + * @return mixed |
| 47 | + * |
| 48 | + * @psalm-template ExecutedMethodReturnType of mixed |
| 49 | + * @psalm-param callable() : ExecutedMethodReturnType $executeMeWithinStreamWrapperOverride |
| 50 | + * @psalm-return ExecutedMethodReturnType |
| 51 | + */ |
| 52 | + public static function withStreamWrapperOverride( |
| 53 | + callable $executeMeWithinStreamWrapperOverride, |
| 54 | + array $streamWrapperProtocols = self::DEFAULT_STREAM_WRAPPER_PROTOCOLS, |
| 55 | + ) |
| 56 | + { |
| 57 | + self::$registeredStreamWrapperProtocols = $streamWrapperProtocols; |
| 58 | + self::$autoloadLocatedFile = null; |
| 59 | + |
| 60 | + try { |
| 61 | + foreach ($streamWrapperProtocols as $protocol) { |
| 62 | + stream_wrapper_unregister($protocol); |
| 63 | + stream_wrapper_register($protocol, self::class); |
| 64 | + } |
| 65 | + |
| 66 | + $result = $executeMeWithinStreamWrapperOverride(); |
| 67 | + } finally { |
| 68 | + foreach ($streamWrapperProtocols as $protocol) { |
| 69 | + stream_wrapper_restore($protocol); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + self::$registeredStreamWrapperProtocols = null; |
| 74 | + self::$autoloadLocatedFile = null; |
| 75 | + |
| 76 | + return $result; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Our wrapper simply records which file we tried to load and returns |
| 81 | + * boolean false indicating failure. |
| 82 | + * |
| 83 | + * @internal do not call this method directly! This is stream wrapper |
| 84 | + * voodoo logic that you **DO NOT** want to touch! |
| 85 | + * |
| 86 | + * @see https://php.net/manual/en/class.streamwrapper.php |
| 87 | + * @see https://php.net/manual/en/streamwrapper.stream-open.php |
| 88 | + * |
| 89 | + * @param string $path |
| 90 | + * @param string $mode |
| 91 | + * @param int $options |
| 92 | + * @param string $openedPath |
| 93 | + */ |
| 94 | + public function stream_open($path, $mode, $options, &$openedPath): bool |
| 95 | + { |
| 96 | + self::$autoloadLocatedFile = $path; |
| 97 | + $this->readFromFile = false; |
| 98 | + $this->seekPosition = 0; |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Since we allow our wrapper's stream_open() to succeed, we need to |
| 105 | + * simulate a successful read so autoloaders with require() don't explode. |
| 106 | + * |
| 107 | + * @param int $count |
| 108 | + * |
| 109 | + */ |
| 110 | + public function stream_read($count): string |
| 111 | + { |
| 112 | + $this->readFromFile = true; |
| 113 | + |
| 114 | + // Dummy return value that is also valid PHP for require(). We'll read |
| 115 | + // and process the file elsewhere, so it's OK to provide dummy data for |
| 116 | + // this read. |
| 117 | + return ''; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Since we allowed the open to succeed, we should allow the close to occur |
| 122 | + * as well. |
| 123 | + * |
| 124 | + */ |
| 125 | + public function stream_close(): void |
| 126 | + { |
| 127 | + // no op |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Required for `require_once` and `include_once` to work per PHP.net |
| 132 | + * comment referenced below. We delegate to url_stat(). |
| 133 | + * |
| 134 | + * @see https://www.php.net/manual/en/function.stream-wrapper-register.php#51855 |
| 135 | + * |
| 136 | + * @return mixed[]|bool |
| 137 | + */ |
| 138 | + public function stream_stat() |
| 139 | + { |
| 140 | + if (self::$autoloadLocatedFile === null) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + return $this->url_stat(self::$autoloadLocatedFile, STREAM_URL_STAT_QUIET); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * url_stat is triggered by calls like "file_exists". The call to "file_exists" must not be overloaded. |
| 149 | + * This function restores the original "file" stream, issues a call to "stat" to get the real results, |
| 150 | + * and then re-registers the AutoloadSourceLocator stream wrapper. |
| 151 | + * |
| 152 | + * @internal do not call this method directly! This is stream wrapper |
| 153 | + * voodoo logic that you **DO NOT** want to touch! |
| 154 | + * |
| 155 | + * @see https://php.net/manual/en/class.streamwrapper.php |
| 156 | + * @see https://php.net/manual/en/streamwrapper.url-stat.php |
| 157 | + * |
| 158 | + * @param string $path |
| 159 | + * @param int $flags |
| 160 | + * |
| 161 | + * @return mixed[]|bool |
| 162 | + */ |
| 163 | + public function url_stat($path, $flags) |
| 164 | + { |
| 165 | + if (self::$registeredStreamWrapperProtocols === null) { |
| 166 | + throw new ShouldNotHappenException(self::class . ' not registered: cannot operate. Do not call this method directly.'); |
| 167 | + } |
| 168 | + |
| 169 | + foreach (self::$registeredStreamWrapperProtocols as $protocol) { |
| 170 | + stream_wrapper_restore($protocol); |
| 171 | + } |
| 172 | + |
| 173 | + if (($flags & STREAM_URL_STAT_QUIET) !== 0) { |
| 174 | + $result = @stat($path); |
| 175 | + } else { |
| 176 | + $result = stat($path); |
| 177 | + } |
| 178 | + |
| 179 | + foreach (self::$registeredStreamWrapperProtocols as $protocol) { |
| 180 | + stream_wrapper_unregister($protocol); |
| 181 | + stream_wrapper_register($protocol, self::class); |
| 182 | + } |
| 183 | + |
| 184 | + return $result; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Simulates behavior of reading from an empty file. |
| 189 | + * |
| 190 | + */ |
| 191 | + public function stream_eof(): bool |
| 192 | + { |
| 193 | + return $this->readFromFile; |
| 194 | + } |
| 195 | + |
| 196 | + public function stream_flush(): bool |
| 197 | + { |
| 198 | + return true; |
| 199 | + } |
| 200 | + |
| 201 | + public function stream_tell(): int |
| 202 | + { |
| 203 | + return $this->seekPosition; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @param int $offset |
| 208 | + * @param int $whence |
| 209 | + */ |
| 210 | + public function stream_seek($offset, $whence): bool |
| 211 | + { |
| 212 | + switch ($whence) { |
| 213 | + // Behavior is the same for a zero-length file |
| 214 | + case SEEK_SET: |
| 215 | + case SEEK_END: |
| 216 | + if ($offset < 0) { |
| 217 | + return false; |
| 218 | + } |
| 219 | + $this->seekPosition = $offset; |
| 220 | + return true; |
| 221 | + |
| 222 | + case SEEK_CUR: |
| 223 | + if ($offset < 0) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + $this->seekPosition += $offset; |
| 227 | + return true; |
| 228 | + |
| 229 | + default: |
| 230 | + return false; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * @param int $option |
| 236 | + * @param int $arg1 |
| 237 | + * @param int $arg2 |
| 238 | + */ |
| 239 | + public function stream_set_option($option, $arg1, $arg2): bool |
| 240 | + { |
| 241 | + return false; |
| 242 | + } |
| 243 | + |
| 244 | +} |
0 commit comments