Updater.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  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 Packagist\WebBundle\Package;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Repository\VcsRepository;
  14. use Composer\IO\NullIO;
  15. use Packagist\WebBundle\Entity\Author;
  16. use Packagist\WebBundle\Entity\Package;
  17. use Packagist\WebBundle\Entity\Tag;
  18. use Packagist\WebBundle\Entity\Version;
  19. use Symfony\Bridge\Doctrine\RegistryInterface;
  20. /**
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class Updater
  24. {
  25. /**
  26. * Doctrine
  27. * @var RegistryInterface
  28. */
  29. protected $doctrine;
  30. /**
  31. * Start
  32. * @var DateTime
  33. */
  34. protected $start;
  35. /**
  36. * Supported link types
  37. * @var array
  38. */
  39. protected $supportedLinkTypes = array(
  40. 'require' => 'RequireLink',
  41. 'conflict' => 'ConflictLink',
  42. 'provide' => 'ProvideLink',
  43. 'replace' => 'ReplaceLink',
  44. 'recommend' => 'RecommendLink',
  45. 'suggest' => 'SuggestLink',
  46. );
  47. /**
  48. * Constructor
  49. *
  50. * @param RegistryInterface $doctrine
  51. * @param \DateTime $start
  52. */
  53. public function __construct(RegistryInterface $doctrine, \DateTime $start = null)
  54. {
  55. $this->doctrine = $doctrine;
  56. $this->start = null !== $start ? $start : new \DateTime();
  57. }
  58. /**
  59. * Update a project
  60. *
  61. * @param PackageInterface $package
  62. * @param boolean $clearExistingVersions
  63. */
  64. public function update(Package $package, $clearExistingVersions = false)
  65. {
  66. $repository = new VcsRepository(array('url' => $package->getRepository()), new NullIO());
  67. $versions = $repository->getPackages();
  68. $em = $this->doctrine->getEntityManager();
  69. usort($versions, function ($a, $b) {
  70. return version_compare($a->getVersion(), $b->getVersion());
  71. });
  72. $versionRepository = $this->doctrine->getRepository('PackagistWebBundle:Version');
  73. if ($clearExistingVersions) {
  74. foreach ($package->getVersions() as $version) {
  75. $versionRepo->remove($version);
  76. }
  77. $em->flush();
  78. $em->refresh($package);
  79. }
  80. foreach ($versions as $version) {
  81. $this->updateInformation($package, $version);
  82. $em->flush();
  83. }
  84. // remove outdated -dev versions
  85. foreach ($package->getVersions() as $version) {
  86. if ($version->getDevelopment() && $version->getUpdatedAt() < $this->start) {
  87. $versionRepository->remove($version);
  88. }
  89. }
  90. $package->setUpdatedAt(new \DateTime);
  91. $package->setCrawledAt(new \DateTime);
  92. $em->flush();
  93. }
  94. private function updateInformation(Package $package, PackageInterface $data)
  95. {
  96. $em = $this->doctrine->getEntityManager();
  97. $version = new Version();
  98. $version->setName($package->getName());
  99. $version->setNormalizedVersion(preg_replace('{-dev$}i', '', $data->getVersion()));
  100. // check if we have that version yet
  101. foreach ($package->getVersions() as $existingVersion) {
  102. if ($existingVersion->equals($version)) {
  103. // avoid updating newer versions, in case two branches have the same version in their composer.json
  104. if ($existingVersion->getReleasedAt() > $data->getReleaseDate()) {
  105. return;
  106. }
  107. if ($existingVersion->getDevelopment()) {
  108. $version = $existingVersion;
  109. break;
  110. }
  111. return;
  112. }
  113. }
  114. $version->setVersion($data->getPrettyVersion());
  115. $version->setDevelopment(substr($data->getVersion(), -4) === '-dev');
  116. $em->persist($version);
  117. $version->setDescription($data->getDescription());
  118. $package->setDescription($data->getDescription());
  119. $version->setHomepage($data->getHomepage());
  120. $version->setLicense($data->getLicense() ?: array());
  121. $version->setPackage($package);
  122. $version->setUpdatedAt(new \DateTime);
  123. $version->setReleasedAt($data->getReleaseDate());
  124. if ($data->getSourceType()) {
  125. $source['type'] = $data->getSourceType();
  126. $source['url'] = $data->getSourceUrl();
  127. $source['reference'] = $data->getSourceReference();
  128. $version->setSource($source);
  129. }
  130. if ($data->getDistType()) {
  131. $dist['type'] = $data->getDistType();
  132. $dist['url'] = $data->getDistUrl();
  133. $dist['reference'] = $data->getDistReference();
  134. $dist['shasum'] = $data->getDistSha1Checksum();
  135. $version->setDist($dist);
  136. }
  137. if ($data->getType()) {
  138. $version->setType($data->getType());
  139. if ($data->getType() && $data->getType() !== $package->getType()) {
  140. $package->setType($data->getType());
  141. }
  142. }
  143. $version->setTargetDir($data->getTargetDir());
  144. $version->setAutoload($data->getAutoload());
  145. $version->setExtra($data->getExtra());
  146. $version->setBinaries($data->getBinaries());
  147. $version->getTags()->clear();
  148. if ($data->getKeywords()) {
  149. foreach ($data->getKeywords() as $keyword) {
  150. $version->addTag(Tag::getByName($em, $keyword, true));
  151. }
  152. }
  153. $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
  154. $version->getAuthors()->clear();
  155. if ($data->getAuthors()) {
  156. foreach ($data->getAuthors() as $authorData) {
  157. $author = null;
  158. // skip authors with no information
  159. if (empty($authorData['email']) && empty($authorData['name'])) {
  160. continue;
  161. }
  162. if (!empty($authorData['email'])) {
  163. $author = $authorRepository->findOneByEmail($authorData['email']);
  164. }
  165. if (!$author && !empty($authorData['homepage'])) {
  166. $author = $authorRepository->findOneBy(array(
  167. 'name' => $authorData['name'],
  168. 'homepage' => $authorData['homepage']
  169. ));
  170. }
  171. if (!$author && !empty($authorData['name'])) {
  172. $author = $authorRepository->findOneByNameAndPackage($authorData['name'], $package);
  173. }
  174. if (!$author) {
  175. $author = new Author();
  176. $em->persist($author);
  177. }
  178. foreach (array('email', 'name', 'homepage') as $field) {
  179. if (isset($authorData[$field])) {
  180. $author->{'set'.$field}($authorData[$field]);
  181. }
  182. }
  183. $author->setUpdatedAt(new \DateTime);
  184. if (!$version->getAuthors()->contains($author)) {
  185. $version->addAuthor($author);
  186. }
  187. if (!$author->getVersions()->contains($version)) {
  188. $author->addVersion($version);
  189. }
  190. }
  191. }
  192. foreach ($this->supportedLinkTypes as $linkType => $linkEntity) {
  193. $links = array();
  194. foreach ($data->{'get'.$linkType.'s'}() as $link) {
  195. $links[$link->getTarget()] = $link->getPrettyConstraint();
  196. }
  197. foreach ($version->{'get'.$linkType}() as $link) {
  198. // clear links that have changed/disappeared (for updates)
  199. if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
  200. $version->{'get'.$linkType}()->removeElement($link);
  201. $em->remove($link);
  202. } else {
  203. // clear those that are already set
  204. unset($links[$link->getPackageName()]);
  205. }
  206. }
  207. foreach ($links as $linkPackageName => $linkPackageVersion) {
  208. $class = 'Packagist\WebBundle\Entity\\'.$linkEntity;
  209. $link = new $class;
  210. $link->setPackageName($linkPackageName);
  211. $link->setPackageVersion($linkPackageVersion);
  212. $version->{'add'.$linkType.'Link'}($link);
  213. $link->setVersion($version);
  214. $em->persist($link);
  215. }
  216. }
  217. if (!$package->getVersions()->contains($version)) {
  218. $package->addVersions($version);
  219. }
  220. }
  221. }