ArrayDumper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. foreach (BasePackage::$supportedLinkTypes as $type => $opts) {
  53. if ($links = $package->{'get'.ucfirst($opts['method'])}()) {
  54. foreach ($links as $link) {
  55. $data[$type][$link->getTarget()] = $link->getPrettyConstraint();
  56. }
  57. ksort($data[$type]);
  58. }
  59. }
  60. if ($packages = $package->getSuggests()) {
  61. ksort($packages);
  62. $data['suggest'] = $packages;
  63. }
  64. if ($package->getReleaseDate()) {
  65. $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s');
  66. }
  67. $data = $this->dumpValues($package, $keys, $data);
  68. if ($package instanceof CompletePackageInterface) {
  69. $keys = array(
  70. 'scripts',
  71. 'license',
  72. 'authors',
  73. 'description',
  74. 'homepage',
  75. 'keywords',
  76. 'repositories',
  77. 'support',
  78. );
  79. $data = $this->dumpValues($package, $keys, $data);
  80. if (isset($data['keywords']) && is_array($data['keywords'])) {
  81. sort($data['keywords']);
  82. }
  83. }
  84. if ($package instanceof RootPackageInterface) {
  85. $minimumStability = $package->getMinimumStability();
  86. if ($minimumStability) {
  87. $data['minimum-stability'] = $minimumStability;
  88. }
  89. }
  90. return $data;
  91. }
  92. private function dumpValues(PackageInterface $package, array $keys, array $data)
  93. {
  94. foreach ($keys as $method => $key) {
  95. if (is_numeric($method)) {
  96. $method = $key;
  97. }
  98. $getter = 'get'.ucfirst($method);
  99. $value = $package->$getter();
  100. if (null !== $value && !(is_array($value) && 0 === count($value))) {
  101. $data[$key] = $value;
  102. }
  103. }
  104. return $data;
  105. }
  106. }