IniHelper.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. /**
  13. * Provides ini file location functions that work with and without a restart.
  14. * When the process has restarted it uses a tmp ini and stores the original
  15. * ini locations in an environment variable.
  16. *
  17. * @author John Stevenson <john-stevenson@blueyonder.co.uk>
  18. */
  19. class IniHelper
  20. {
  21. const ENV_ORIGINAL = 'COMPOSER_ORIGINAL_INIS';
  22. /**
  23. * Returns an array of php.ini locations with at least one entry
  24. *
  25. * The equivalent of calling php_ini_loaded_file then php_ini_scanned_files.
  26. * The loaded ini location is the first entry and may be empty.
  27. *
  28. * @return array
  29. */
  30. public static function getAll()
  31. {
  32. $env = getenv(self::ENV_ORIGINAL);
  33. if (false !== $env) {
  34. return explode(PATH_SEPARATOR, $env);
  35. }
  36. $paths = array(strval(php_ini_loaded_file()));
  37. if ($scanned = php_ini_scanned_files()) {
  38. $paths = array_merge($paths, array_map('trim', explode(',', $scanned)));
  39. }
  40. return $paths;
  41. }
  42. /**
  43. * Describes the location of the loaded php.ini file(s)
  44. *
  45. * @return string
  46. */
  47. public static function getMessage()
  48. {
  49. $paths = self::getAll();
  50. if (empty($paths[0])) {
  51. array_shift($paths);
  52. }
  53. $ini = array_shift($paths);
  54. if (empty($ini)) {
  55. return 'A php.ini file does not exist. You will have to create one.';
  56. }
  57. if (!empty($paths)) {
  58. return 'Your command-line PHP is using multiple ini files. Run `php --ini` to show them.';
  59. }
  60. return 'The php.ini used by your command-line PHP is: '.$ini;
  61. }
  62. }