scoper.inc.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Symfony\Component\Finder\Finder;
  11. $polyfillsBootstraps = \array_map(
  12. function (SplFileInfo $fileInfo) {
  13. return $fileInfo->getPathname();
  14. },
  15. \iterator_to_array(
  16. Finder::create()
  17. ->files()
  18. ->in(__DIR__.'/vendor/symfony/polyfill-*')
  19. ->name('bootstrap*.php'),
  20. false
  21. )
  22. );
  23. $polyfillsStubs = [];
  24. try {
  25. $polyfillsStubs = \array_map(
  26. function (SplFileInfo $fileInfo) {
  27. return $fileInfo->getPathname();
  28. },
  29. \iterator_to_array(
  30. Finder::create()
  31. ->files()
  32. ->in(__DIR__.'/vendor/symfony/polyfill-*/Resources/stubs')
  33. ->name('*.php'),
  34. false
  35. )
  36. );
  37. } catch (Throwable $e) {
  38. // There may not be any stubs?
  39. }
  40. return [
  41. 'exclude-namespaces' => [
  42. 'Psy',
  43. 'Symfony\Polyfill',
  44. ],
  45. 'exclude-constants' => [
  46. // Symfony global constants
  47. '/^SYMFONY\_[\p{L}_]+$/',
  48. ],
  49. 'exclude-files' => \array_merge($polyfillsBootstraps, $polyfillsStubs),
  50. 'patchers' => [
  51. // https://github.com/humbug/php-scoper/issues/294
  52. // https://github.com/humbug/php-scoper/issues/286
  53. static function (string $filePath, string $prefix, string $contents): string {
  54. if (!\in_array($filePath, ['src/Formatter/DocblockFormatter.php', 'src/Output/ShellOutput.php'], true)) {
  55. return $contents;
  56. }
  57. return \str_replace(
  58. '\'Symfony\\\\Component\\\\Console\\\\Formatter\\\\OutputFormatter\'',
  59. \sprintf(
  60. '\'%s\\%s\'',
  61. $prefix,
  62. 'Symfony\\Component\\Console\\Formatter\\OutputFormatter'
  63. ),
  64. $contents
  65. );
  66. },
  67. // Symfony patches
  68. static function (string $filePath, string $prefix, string $contents): string {
  69. if ('vendor/symfony/debug/DebugClassLoader.php' !== $filePath) {
  70. return $contents;
  71. }
  72. return \preg_replace(
  73. '/case \'(Symfony\\\\.+\\\\)\':/',
  74. \sprintf(
  75. 'case \'%s\\\\\\\$1\':',
  76. $prefix
  77. ),
  78. $contents
  79. );
  80. },
  81. // https://github.com/humbug/php-scoper/issues/286
  82. static function (string $filePath, string $prefix, string $contents): string {
  83. if ('vendor/symfony/var-dumper/Cloner/AbstractCloner.php' !== $filePath) {
  84. return $contents;
  85. }
  86. return \preg_replace(
  87. '/\'(Symfony\\\\.+?)\'/',
  88. \sprintf(
  89. '\'%s\\\\\\\$1\'',
  90. $prefix
  91. ),
  92. $contents
  93. );
  94. },
  95. // https://github.com/humbug/php-scoper/issues/286
  96. static function (string $filePath, string $prefix, string $contents): string {
  97. if ('vendor/symfony/debug/Exception/FlattenException.php' !== $filePath) {
  98. return $contents;
  99. }
  100. return \preg_replace(
  101. '/\'(Symfony\\\\.+?)\'/',
  102. \sprintf(
  103. '\'%s\\\\\\\$1\'',
  104. $prefix
  105. ),
  106. $contents
  107. );
  108. },
  109. // PHP-Parser patches
  110. static function (string $filePath, string $prefix, string $contents): string {
  111. if ('vendor/nikic/php-parser/lib/PhpParser/JsonDecoder.php' !== $filePath) {
  112. return $contents;
  113. }
  114. return \str_replace(
  115. '\'PhpParser\\\\Node\\\\\'',
  116. \sprintf(
  117. '\'%s\\\\PhpParser\\\\Node\\\\\'',
  118. $prefix
  119. ),
  120. $contents
  121. );
  122. },
  123. static function (string $filePath, string $prefix, string $contents): string {
  124. if ('vendor/nikic/php-parser/lib/PhpParser/Unserializer/XML.php' !== $filePath) {
  125. return $contents;
  126. }
  127. $contents = \preg_replace(
  128. '/\'(PhpParser\\\\.+(?:\\\\)?)\'/',
  129. \sprintf(
  130. '\'%s\\\\\\\$1\'',
  131. $prefix
  132. ),
  133. $contents
  134. );
  135. $contents = \preg_replace(
  136. '/\'(PhpParser\\\\\\\\\p{L}+)(?:\\\\\\\\)?\'/u',
  137. \sprintf(
  138. '\'%s\\\\\\\$1\'',
  139. $prefix
  140. ),
  141. $contents
  142. );
  143. return $contents;
  144. },
  145. static function (string $filePath, string $prefix, string $contents): string {
  146. if ('vendor/nikic/php-parser/lib/PhpParser/Lexer.php' !== $filePath) {
  147. return $contents;
  148. }
  149. return \str_replace(
  150. '\'PhpParser\\\\Parser\\\\Tokens::\'',
  151. \sprintf(
  152. '\'%s\\\\PhpParser\\\\Parser\\\\Tokens::\'',
  153. $prefix
  154. ),
  155. $contents
  156. );
  157. },
  158. ],
  159. ];