Composer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 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') || 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. /**
  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, 'r');
  42. $firstLine = fgets($file);
  43. fclose($file);
  44. if (!preg_match('/^#!.+php/i', $firstLine)) {
  45. $executor = '';
  46. }
  47. }
  48. return $executor . $composer;
  49. }
  50. }