You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.2KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Repository\Vcs;
  11. use Composer\Cache;
  12. use Composer\Json\JsonFile;
  13. use Composer\Util\ProcessExecutor;
  14. /**
  15. * Helper for process VCS driver.
  16. *
  17. * @author François Pluchino <francois.pluchino@gmail.com>
  18. */
  19. class ProcessUtil
  20. {
  21. /**
  22. * Get composer information.
  23. *
  24. * @param Cache $cache
  25. * @param array $infoCache
  26. * @param string $assetType
  27. * @param ProcessExecutor $process
  28. * @param string $identifier
  29. * @param string $resource
  30. * @param string $cmdGet
  31. * @param string $cmdLog
  32. * @param string $repoDir
  33. * @param string $datetimePrefix
  34. *
  35. * @return array The composer
  36. */
  37. public static function getComposerInformation(Cache $cache, array &$infoCache,
  38. $assetType, ProcessExecutor $process, $identifier, $resource, $cmdGet,
  39. $cmdLog, $repoDir, $datetimePrefix = '')
  40. {
  41. $infoCache[$identifier] = Util::readCache($infoCache, $cache, $assetType, $identifier);
  42. if (!isset($infoCache[$identifier])) {
  43. $composer = static::doGetComposerInformation($resource, $process, $cmdGet, $cmdLog, $repoDir, $datetimePrefix);
  44. Util::writeCache($cache, $assetType, $identifier, $composer);
  45. $infoCache[$identifier] = $composer;
  46. }
  47. return $infoCache[$identifier];
  48. }
  49. /**
  50. * Get composer information.
  51. *
  52. * @param string $resource
  53. * @param ProcessExecutor $process
  54. * @param string $cmdGet
  55. * @param string $cmdLog
  56. * @param string $repoDir
  57. * @param string $datetimePrefix
  58. *
  59. * @return array The composer
  60. */
  61. protected static function doGetComposerInformation($resource, ProcessExecutor $process, $cmdGet, $cmdLog, $repoDir, $datetimePrefix = '')
  62. {
  63. $process->execute($cmdGet, $composer, $repoDir);
  64. if (!trim($composer)) {
  65. return array('_nonexistent_package' => true);
  66. }
  67. $composer = JsonFile::parseJson($composer, $resource);
  68. return static::addComposerTime($composer, $process, $cmdLog, $repoDir, $datetimePrefix);
  69. }
  70. /**
  71. * Add time in composer.
  72. *
  73. * @param array $composer
  74. * @param ProcessExecutor $process
  75. * @param string $cmd
  76. * @param string $repoDir
  77. * @param string $datetimePrefix
  78. *
  79. * @return array The composer
  80. */
  81. protected static function addComposerTime(array $composer, ProcessExecutor $process, $cmd, $repoDir, $datetimePrefix = '')
  82. {
  83. if (!isset($composer['time'])) {
  84. $process->execute($cmd, $output, $repoDir);
  85. $date = new \DateTime($datetimePrefix.trim($output), new \DateTimeZone('UTC'));
  86. $composer['time'] = $date->format('Y-m-d H:i:s');
  87. }
  88. return $composer;
  89. }
  90. }