Updater.php 8.7 KB

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