Composer.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use function function_exists;
  10. /**
  11. * Class Composer
  12. * @package Grav\Common
  13. */
  14. class Composer
  15. {
  16. /** @const Default composer location */
  17. const DEFAULT_PATH = 'bin/composer.phar';
  18. /**
  19. * Returns the location of composer.
  20. *
  21. * @return string
  22. */
  23. public static function getComposerLocation()
  24. {
  25. if (!function_exists('shell_exec') || stripos(PHP_OS, 'win') === 0) {
  26. return self::DEFAULT_PATH;
  27. }
  28. // check for global composer install
  29. $path = trim((string)shell_exec('command -v composer'));
  30. // fall back to grav bundled composer
  31. if (!$path || !preg_match('/(composer|composer\.phar)$/', $path)) {
  32. $path = self::DEFAULT_PATH;
  33. }
  34. return $path;
  35. }
  36. /**
  37. * Return the composer executable file path
  38. *
  39. * @return string
  40. */
  41. public static function getComposerExecutor()
  42. {
  43. $executor = PHP_BINARY . ' ';
  44. $composer = static::getComposerLocation();
  45. if ($composer !== static::DEFAULT_PATH && is_executable($composer)) {
  46. $file = fopen($composer, 'rb');
  47. $firstLine = fgets($file);
  48. fclose($file);
  49. if (!preg_match('/^#!.+php/i', $firstLine)) {
  50. $executor = '';
  51. }
  52. }
  53. return $executor . $composer;
  54. }
  55. }