ArrayDumper.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Dumper;
  12. use Composer\Package\BasePackage;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\CompletePackageInterface;
  15. use Composer\Package\RootPackageInterface;
  16. /**
  17. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class ArrayDumper
  21. {
  22. public function dump(PackageInterface $package)
  23. {
  24. $keys = array(
  25. 'binaries' => 'bin',
  26. 'type',
  27. 'extra',
  28. 'installationSource' => 'installation-source',
  29. 'autoload',
  30. 'devAutoload' => 'autoload-dev',
  31. 'notificationUrl' => 'notification-url',
  32. 'includePaths' => 'include-path',
  33. );
  34. $data = array();
  35. $data['name'] = $package->getPrettyName();
  36. $data['version'] = $package->getPrettyVersion();
  37. $data['version_normalized'] = $package->getVersion();
  38. if ($package->getTargetDir()) {
  39. $data['target-dir'] = $package->getTargetDir();
  40. }
  41. if ($package->getSourceType()) {
  42. $data['source']['type'] = $package->getSourceType();
  43. $data['source']['url'] = $package->getSourceUrl();
  44. if (null !== ($value = $package->getSourceReference())) {
  45. $data['source']['reference'] = $value;
  46. }
  47. if ($mirrors = $package->getSourceMirrors()) {
  48. $data['source']['mirrors'] = $mirrors;
  49. }
  50. }
  51. if ($package->getDistType()) {
  52. $data['dist']['type'] = $package->getDistType();
  53. $data['dist']['url'] = $package->getDistUrl();
  54. if (null !== ($value = $package->getDistReference())) {
  55. $data['dist']['reference'] = $value;
  56. }
  57. if (null !== ($value = $package->getDistSha1Checksum())) {
  58. $data['dist']['shasum'] = $value;
  59. }
  60. if ($mirrors = $package->getDistMirrors()) {
  61. $data['dist']['mirrors'] = $mirrors;
  62. }
  63. }
  64. if ($package->getArchiveExcludes()) {
  65. $data['archive']['exclude'] = $package->getArchiveExcludes();
  66. }
  67. foreach (BasePackage::$supportedLinkTypes as $type => $opts) {
  68. if ($links = $package->{'get'.ucfirst($opts['method'])}()) {
  69. foreach ($links as $link) {
  70. $data[$type][$link->getTarget()] = $link->getPrettyConstraint();
  71. }
  72. ksort($data[$type]);
  73. }
  74. }
  75. if ($packages = $package->getSuggests()) {
  76. ksort($packages);
  77. $data['suggest'] = $packages;
  78. }
  79. if ($package->getReleaseDate()) {
  80. $data['time'] = $package->getReleaseDate()->format(DATE_RFC3339);
  81. }
  82. $data = $this->dumpValues($package, $keys, $data);
  83. if ($package instanceof CompletePackageInterface) {
  84. $keys = array(
  85. 'scripts',
  86. 'license',
  87. 'authors',
  88. 'description',
  89. 'homepage',
  90. 'keywords',
  91. 'repositories',
  92. 'support',
  93. 'funding',
  94. );
  95. $data = $this->dumpValues($package, $keys, $data);
  96. if (isset($data['keywords']) && \is_array($data['keywords'])) {
  97. sort($data['keywords']);
  98. }
  99. if ($package->isAbandoned()) {
  100. $data['abandoned'] = $package->getReplacementPackage() ?: true;
  101. }
  102. }
  103. if ($package instanceof RootPackageInterface) {
  104. $minimumStability = $package->getMinimumStability();
  105. if ($minimumStability) {
  106. $data['minimum-stability'] = $minimumStability;
  107. }
  108. }
  109. if (\count($package->getTransportOptions()) > 0) {
  110. $data['transport-options'] = $package->getTransportOptions();
  111. }
  112. return $data;
  113. }
  114. private function dumpValues(PackageInterface $package, array $keys, array $data)
  115. {
  116. foreach ($keys as $method => $key) {
  117. if (is_numeric($method)) {
  118. $method = $key;
  119. }
  120. $getter = 'get'.ucfirst($method);
  121. $value = $package->$getter();
  122. if (null !== $value && !(\is_array($value) && 0 === \count($value))) {
  123. $data[$key] = $value;
  124. }
  125. }
  126. return $data;
  127. }
  128. }