Composer.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  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') || stripos(PHP_OS, 'win') === 0) {
  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. /**
  32. * Return the composer executable file path
  33. *
  34. * @return string
  35. */
  36. public static function getComposerExecutor()
  37. {
  38. $executor = PHP_BINARY . ' ';
  39. $composer = static::getComposerLocation();
  40. if ($composer !== static::DEFAULT_PATH && is_executable($composer)) {
  41. $file = fopen($composer, 'rb');
  42. $firstLine = fgets($file);
  43. fclose($file);
  44. if (!preg_match('/^#!.+php/i', $firstLine)) {
  45. $executor = '';
  46. }
  47. }
  48. return $executor . $composer;
  49. }
  50. }