Composer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Grav\Common;
  3. /**
  4. * Offers composer helper methods.
  5. *
  6. * @author eschmar
  7. * @license MIT
  8. */
  9. class Composer
  10. {
  11. /** @const Default composer location */
  12. const DEFAULT_PATH = "bin/composer.phar";
  13. /**
  14. * Returns the location of composer.
  15. *
  16. * @return string
  17. */
  18. public static function getComposerLocation()
  19. {
  20. if (!function_exists('shell_exec') || strtolower(substr(PHP_OS, 0, 3)) === 'win') {
  21. return self::DEFAULT_PATH;
  22. }
  23. // check for global composer install
  24. $path = trim(shell_exec("command -v composer"));
  25. // fall back to grav bundled composer
  26. if (!$path || !preg_match('/(composer|composer\.phar)$/', $path)) {
  27. $path = self::DEFAULT_PATH;
  28. }
  29. return $path;
  30. }
  31. public static function getComposerExecutor()
  32. {
  33. $executor = PHP_BINARY . ' ';
  34. $composer = static::getComposerLocation();
  35. if ($composer !== static::DEFAULT_PATH && is_executable($composer)) {
  36. $file = fopen($composer, 'r');
  37. $firstLine = fgets($file);
  38. fclose($file);
  39. if (!preg_match('/^#!.+php/i', $firstLine)) {
  40. $executor = '';
  41. }
  42. }
  43. return $executor . $composer;
  44. }
  45. }