MergeBox.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Fgm\ComposerCheck;
  3. /**
  4. * Class MergeBox merges requirements with locked versions and platform.
  5. */
  6. class MergeBox {
  7. /**
  8. * The composer.lock loader.
  9. *
  10. * @var \Fgm\ComposerCheck\LockLoader
  11. */
  12. protected $lock;
  13. /**
  14. * The merged packages.
  15. *
  16. * @var array
  17. */
  18. protected $packages;
  19. /**
  20. * The composer.json loader.
  21. *
  22. * @var \Fgm\ComposerCheck\RequirementsLoader
  23. */
  24. protected $requirements;
  25. /**
  26. * MergeBox constructor.
  27. *
  28. * @param \Fgm\ComposerCheck\RequirementsLoader $requirementsLoader
  29. * The composer.json loader.
  30. * @param \Fgm\ComposerCheck\LockLoader $lockLoader
  31. * The composer.lock loader.
  32. * @param bool $all
  33. * Include all packages or only those required ?
  34. */
  35. public function __construct(RequirementsLoader $requirementsLoader,
  36. LockLoader $lockLoader,
  37. bool $all) {
  38. $this->requirements = $requirementsLoader;
  39. $this->lock = $lockLoader;
  40. $this->all = $all;
  41. }
  42. /**
  43. * Perform reconciliation of requirements and locked packages/platform.
  44. */
  45. public function merge() {
  46. $packages = ['dev' => [], 'run' => []];
  47. foreach ($this->requirements->getRun() as $package => $requirement) {
  48. if ($this->all || !empty($requirement)) {
  49. $package = strtolower($package);
  50. $packages['run'][$package]['requirement'] = $requirement;
  51. }
  52. }
  53. foreach ($this->requirements->getDev() as $package => $requirement) {
  54. if ($this->all || !empty($requirement)) {
  55. $package = strtolower($package);
  56. $packages['dev'][$package]['requirement'] = $requirement;
  57. }
  58. }
  59. foreach ($this->lock->getRun() as $packageInfo) {
  60. $package = strtolower($packageInfo['name']);
  61. if ($this->all || !empty($packages['run'][$package])) {
  62. $version = $packageInfo['version'];
  63. $packages['run'][$package]['version'] = $version;
  64. }
  65. }
  66. foreach ($this->lock->getDev() as $packageInfo) {
  67. $package = strtolower($packageInfo['name']);
  68. if ($this->all || !empty($packages['dev'][$package])) {
  69. $version = $packageInfo['version'];
  70. $packages['dev'][$package]['version'] = $version;
  71. }
  72. }
  73. ksort($packages['dev']);
  74. ksort($packages['run']);
  75. $this->packages = $packages;
  76. }
  77. /**
  78. * Return the merge results.
  79. *
  80. * @return array
  81. * An array of requirements and matching locked versions.
  82. */
  83. public function getRows() {
  84. return $this->packages;
  85. }
  86. }