ArrayDumper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. use Composer\Package\Link;
  17. /**
  18. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class ArrayDumper
  22. {
  23. public function dump(PackageInterface $package)
  24. {
  25. $keys = array(
  26. 'binaries' => 'bin',
  27. 'type',
  28. 'extra',
  29. 'installationSource' => 'installation-source',
  30. 'autoload',
  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. $data['source']['reference'] = $package->getSourceReference();
  45. }
  46. if ($package->getDistType()) {
  47. $data['dist']['type'] = $package->getDistType();
  48. $data['dist']['url'] = $package->getDistUrl();
  49. $data['dist']['reference'] = $package->getDistReference();
  50. $data['dist']['shasum'] = $package->getDistSha1Checksum();
  51. }
  52. if ($package->getArchiveExcludes()) {
  53. $data['archive']['exclude'] = $package->getArchiveExcludes();
  54. }
  55. foreach (BasePackage::$supportedLinkTypes as $type => $opts) {
  56. if ($links = $package->{'get'.ucfirst($opts['method'])}()) {
  57. foreach ($links as $link) {
  58. $data[$type][$link->getTarget()] = $link->getPrettyConstraint();
  59. }
  60. ksort($data[$type]);
  61. }
  62. }
  63. if ($packages = $package->getSuggests()) {
  64. ksort($packages);
  65. $data['suggest'] = $packages;
  66. }
  67. if ($package->getReleaseDate()) {
  68. $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s');
  69. }
  70. $data = $this->dumpValues($package, $keys, $data);
  71. if ($package instanceof CompletePackageInterface) {
  72. $keys = array(
  73. 'scripts',
  74. 'license',
  75. 'authors',
  76. 'description',
  77. 'homepage',
  78. 'keywords',
  79. 'repositories',
  80. 'support',
  81. );
  82. $data = $this->dumpValues($package, $keys, $data);
  83. if (isset($data['keywords']) && is_array($data['keywords'])) {
  84. sort($data['keywords']);
  85. }
  86. }
  87. if ($package instanceof RootPackageInterface) {
  88. $minimumStability = $package->getMinimumStability();
  89. if ($minimumStability) {
  90. $data['minimum-stability'] = $minimumStability;
  91. }
  92. }
  93. return $data;
  94. }
  95. private function dumpValues(PackageInterface $package, array $keys, array $data)
  96. {
  97. foreach ($keys as $method => $key) {
  98. if (is_numeric($method)) {
  99. $method = $key;
  100. }
  101. $getter = 'get'.ucfirst($method);
  102. $value = $package->$getter();
  103. if (null !== $value && !(is_array($value) && 0 === count($value))) {
  104. $data[$key] = $value;
  105. }
  106. }
  107. return $data;
  108. }
  109. }