PhpConfig.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of composer/xdebug-handler.
  5. *
  6. * (c) Composer <https://github.com/composer>
  7. *
  8. * For the full copyright and license information, please view
  9. * the LICENSE file that was distributed with this source code.
  10. */
  11. namespace Composer\XdebugHandler;
  12. /**
  13. * @author John Stevenson <john-stevenson@blueyonder.co.uk>
  14. *
  15. * @phpstan-type restartData array{tmpIni: string, scannedInis: bool, scanDir: false|string, phprc: false|string, inis: string[], skipped: string}
  16. */
  17. class PhpConfig
  18. {
  19. /**
  20. * Use the original PHP configuration
  21. *
  22. * @return string[] Empty array of PHP cli options
  23. */
  24. public function useOriginal(): array
  25. {
  26. $this->getDataAndReset();
  27. return [];
  28. }
  29. /**
  30. * Use standard restart settings
  31. *
  32. * @return string[] PHP cli options
  33. */
  34. public function useStandard(): array
  35. {
  36. $data = $this->getDataAndReset();
  37. if ($data !== null) {
  38. return ['-n', '-c', $data['tmpIni']];
  39. }
  40. return [];
  41. }
  42. /**
  43. * Use environment variables to persist settings
  44. *
  45. * @return string[] Empty array of PHP cli options
  46. */
  47. public function usePersistent(): array
  48. {
  49. $data = $this->getDataAndReset();
  50. if ($data !== null) {
  51. $this->updateEnv('PHPRC', $data['tmpIni']);
  52. $this->updateEnv('PHP_INI_SCAN_DIR', '');
  53. }
  54. return [];
  55. }
  56. /**
  57. * Returns restart data if available and resets the environment
  58. *
  59. * @phpstan-return restartData|null
  60. */
  61. private function getDataAndReset(): ?array
  62. {
  63. $data = XdebugHandler::getRestartSettings();
  64. if ($data !== null) {
  65. $this->updateEnv('PHPRC', $data['phprc']);
  66. $this->updateEnv('PHP_INI_SCAN_DIR', $data['scanDir']);
  67. }
  68. return $data;
  69. }
  70. /**
  71. * Updates a restart settings value in the environment
  72. *
  73. * @param string $name
  74. * @param string|false $value
  75. */
  76. private function updateEnv(string $name, $value): void
  77. {
  78. Process::setEnv($name, false !== $value ? $value : null);
  79. }
  80. }