Package.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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\Entity;
  12. use Composer\Factory;
  13. use Composer\IO\NullIO;
  14. use Composer\Repository\VcsRepository;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  19. use Composer\Repository\Vcs\GitHubDriver;
  20. /**
  21. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
  22. * @ORM\Table(
  23. * name="package",
  24. * uniqueConstraints={@ORM\UniqueConstraint(name="package_name_idx", columns={"name"})},
  25. * indexes={
  26. * @ORM\Index(name="indexed_idx",columns={"indexedAt"}),
  27. * @ORM\Index(name="crawled_idx",columns={"crawledAt"}),
  28. * @ORM\Index(name="dumped_idx",columns={"dumpedAt"})
  29. * }
  30. * )
  31. * @Assert\Callback(callback="isPackageUnique")
  32. * @Assert\Callback(callback="isVendorWritable")
  33. * @Assert\Callback(callback="isRepositoryValid", groups={"Update", "Default"})
  34. * @author Jordi Boggiano <j.boggiano@seld.be>
  35. */
  36. class Package
  37. {
  38. /**
  39. * @ORM\Id
  40. * @ORM\Column(type="integer")
  41. * @ORM\GeneratedValue(strategy="AUTO")
  42. */
  43. private $id;
  44. /**
  45. * Unique package name
  46. *
  47. * @ORM\Column(length=191)
  48. */
  49. private $name;
  50. /**
  51. * @ORM\Column(nullable=true)
  52. */
  53. private $type;
  54. /**
  55. * @ORM\Column(type="text", nullable=true)
  56. */
  57. private $description;
  58. /**
  59. * @ORM\Column(type="string", nullable=true)
  60. */
  61. private $language;
  62. /**
  63. * @ORM\Column(type="text", nullable=true)
  64. */
  65. private $readme;
  66. /**
  67. * @ORM\Column(type="integer", nullable=true, name="github_stars")
  68. */
  69. private $gitHubStars;
  70. /**
  71. * @ORM\Column(type="integer", nullable=true, name="github_watches")
  72. */
  73. private $gitHubWatches;
  74. /**
  75. * @ORM\Column(type="integer", nullable=true, name="github_forks")
  76. */
  77. private $gitHubForks;
  78. /**
  79. * @ORM\Column(type="integer", nullable=true, name="github_open_issues")
  80. */
  81. private $gitHubOpenIssues;
  82. /**
  83. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
  84. */
  85. private $versions;
  86. /**
  87. * @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
  88. * @ORM\JoinTable(name="maintainers_packages")
  89. */
  90. private $maintainers;
  91. /**
  92. * @ORM\Column()
  93. * @Assert\NotBlank(groups={"Update", "Default"})
  94. */
  95. private $repository;
  96. // dist-tags / rel or runtime?
  97. /**
  98. * @ORM\Column(type="datetime")
  99. */
  100. private $createdAt;
  101. /**
  102. * @ORM\Column(type="datetime", nullable=true)
  103. */
  104. private $updatedAt;
  105. /**
  106. * @ORM\Column(type="datetime", nullable=true)
  107. */
  108. private $crawledAt;
  109. /**
  110. * @ORM\Column(type="datetime", nullable=true)
  111. */
  112. private $indexedAt;
  113. /**
  114. * @ORM\Column(type="datetime", nullable=true)
  115. */
  116. private $dumpedAt;
  117. /**
  118. * @ORM\Column(type="boolean")
  119. */
  120. private $autoUpdated = false;
  121. /**
  122. * @var bool
  123. * @ORM\Column(type="boolean")
  124. */
  125. private $abandoned = false;
  126. /**
  127. * @var string
  128. * @ORM\Column(type="string", length=255, nullable=true)
  129. */
  130. private $replacementPackage;
  131. /**
  132. * @ORM\Column(type="boolean", options={"default"=false})
  133. */
  134. private $updateFailureNotified = false;
  135. private $entityRepository;
  136. private $router;
  137. /**
  138. * @var \Composer\Repository\Vcs\VcsDriverInterface
  139. */
  140. private $vcsDriver = true;
  141. private $vcsDriverError;
  142. /**
  143. * @var array lookup table for versions
  144. */
  145. private $cachedVersions;
  146. public function __construct()
  147. {
  148. $this->versions = new ArrayCollection();
  149. $this->createdAt = new \DateTime;
  150. }
  151. public function toArray(VersionRepository $versionRepo)
  152. {
  153. $versions = array();
  154. $versionIds = [];
  155. foreach ($this->getVersions() as $version) {
  156. $versionIds[] = $version->getId();
  157. }
  158. $versionData = $versionRepo->getVersionData($versionIds);
  159. foreach ($this->getVersions() as $version) {
  160. /** @var $version Version */
  161. $versions[$version->getVersion()] = $version->toArray($versionData);
  162. }
  163. $maintainers = array();
  164. foreach ($this->getMaintainers() as $maintainer) {
  165. /** @var $maintainer User */
  166. $maintainers[] = $maintainer->toArray();
  167. }
  168. $data = array(
  169. 'name' => $this->getName(),
  170. 'description' => $this->getDescription(),
  171. 'time' => $this->getCreatedAt()->format('c'),
  172. 'maintainers' => $maintainers,
  173. 'versions' => $versions,
  174. 'type' => $this->getType(),
  175. 'repository' => $this->getRepository(),
  176. 'github_stars' => $this->getGitHubStars(),
  177. 'github_watchers' => $this->getGitHubWatches(),
  178. 'github_forks' => $this->getGitHubForks(),
  179. 'github_open_issues' => $this->getGitHubOpenIssues(),
  180. 'language' => $this->getLanguage(),
  181. );
  182. if ($this->isAbandoned()) {
  183. $data['abandoned'] = $this->getReplacementPackage() ?: true;
  184. }
  185. return $data;
  186. }
  187. public function isRepositoryValid(ExecutionContextInterface $context)
  188. {
  189. // vcs driver was not nulled which means the repository was not set/modified and is still valid
  190. if (true === $this->vcsDriver && null !== $this->getName()) {
  191. return;
  192. }
  193. $property = 'repository';
  194. $driver = $this->vcsDriver;
  195. if (!is_object($driver)) {
  196. if (preg_match('{https?://.+@}', $this->repository)) {
  197. $context->buildViolation('URLs with user@host are not supported, use a read-only public URL')
  198. ->atPath($property)
  199. ->addViolation()
  200. ;
  201. } elseif (is_string($this->vcsDriverError)) {
  202. $context->buildViolation('Uncaught Exception: '.htmlentities($this->vcsDriverError, ENT_COMPAT, 'utf-8'))
  203. ->atPath($property)
  204. ->addViolation()
  205. ;
  206. } else {
  207. $context->buildViolation('No valid/supported repository was found at the given URL')
  208. ->atPath($property)
  209. ->addViolation()
  210. ;
  211. }
  212. return;
  213. }
  214. try {
  215. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  216. if (false === $information) {
  217. $context->buildViolation('No composer.json was found in the '.$driver->getRootIdentifier().' branch.')
  218. ->atPath($property)
  219. ->addViolation()
  220. ;
  221. return;
  222. }
  223. if (empty($information['name'])) {
  224. $context->buildViolation('The package name was not found in the composer.json, make sure there is a name present.')
  225. ->atPath($property)
  226. ->addViolation()
  227. ;
  228. return;
  229. }
  230. if (!preg_match('{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*$}i', $information['name'])) {
  231. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*".')
  232. ->atPath($property)
  233. ->addViolation()
  234. ;
  235. return;
  236. }
  237. $reservedNames = ['nul', 'con', 'prn', 'aux', 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9'];
  238. $bits = explode('/', strtolower($information['name']));
  239. if (in_array($bits[0], $reservedNames, true) || in_array($bits[1], $reservedNames, true)) {
  240. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is reserved, package and vendor names can not match any of: '.implode(', ', $reservedNames).'.')
  241. ->atPath($property)
  242. ->addViolation()
  243. ;
  244. return;
  245. }
  246. if (preg_match('{\.json$}', $information['name'])) {
  247. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.')
  248. ->atPath($property)
  249. ->addViolation()
  250. ;
  251. return;
  252. }
  253. if (preg_match('{[A-Z]}', $information['name'])) {
  254. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $information['name']);
  255. $suggestName = strtolower($suggestName);
  256. $context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.')
  257. ->atPath($property)
  258. ->addViolation()
  259. ;
  260. return;
  261. }
  262. } catch (\Exception $e) {
  263. $context->buildViolation('We had problems parsing your composer.json file, the parser reports: '.htmlentities($e->getMessage(), ENT_COMPAT, 'utf-8'))
  264. ->atPath($property)
  265. ->addViolation()
  266. ;
  267. }
  268. if (null === $this->getName()) {
  269. $context->buildViolation('An unexpected error has made our parser fail to find a package name in your repository, if you think this is incorrect please try again')
  270. ->atPath($property)
  271. ->addViolation()
  272. ;
  273. }
  274. }
  275. public function setEntityRepository($repository)
  276. {
  277. $this->entityRepository = $repository;
  278. }
  279. public function setRouter($router)
  280. {
  281. $this->router = $router;
  282. }
  283. public function isPackageUnique(ExecutionContextInterface $context)
  284. {
  285. try {
  286. if ($this->entityRepository->findOneByName($this->name)) {
  287. $context->buildViolation('A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.')
  288. ->atPath('repository')
  289. ->addViolation()
  290. ;
  291. }
  292. } catch (\Doctrine\ORM\NoResultException $e) {}
  293. }
  294. public function isVendorWritable(ExecutionContextInterface $context)
  295. {
  296. try {
  297. $vendor = $this->getVendor();
  298. if ($vendor && $this->entityRepository->isVendorTaken($vendor, reset($this->maintainers))) {
  299. $context->buildViolation('The vendor is already taken by someone else. '
  300. . 'You may ask them to add your package and give you maintainership access. '
  301. . 'If they add you as a maintainer on any package in that vendor namespace, '
  302. . 'you will then be able to add new packages in that namespace. '
  303. . 'The packages already in that vendor namespace can be found at '
  304. . '<a href="'.$this->router->generate('view_vendor', array('vendor' => $vendor)).'">'.$vendor.'</a>')
  305. ->atPath('repository')
  306. ->addViolation()
  307. ;
  308. }
  309. } catch (\Doctrine\ORM\NoResultException $e) {}
  310. }
  311. /**
  312. * Get id
  313. *
  314. * @return string
  315. */
  316. public function getId()
  317. {
  318. return $this->id;
  319. }
  320. /**
  321. * Set name
  322. *
  323. * @param string $name
  324. */
  325. public function setName($name)
  326. {
  327. $this->name = $name;
  328. }
  329. /**
  330. * Get name
  331. *
  332. * @return string
  333. */
  334. public function getName()
  335. {
  336. return $this->name;
  337. }
  338. /**
  339. * Get vendor prefix
  340. *
  341. * @return string
  342. */
  343. public function getVendor()
  344. {
  345. return preg_replace('{/.*$}', '', $this->name);
  346. }
  347. /**
  348. * Get package name without vendor
  349. *
  350. * @return string
  351. */
  352. public function getPackageName()
  353. {
  354. return preg_replace('{^[^/]*/}', '', $this->name);
  355. }
  356. /**
  357. * Set description
  358. *
  359. * @param string $description
  360. */
  361. public function setDescription($description)
  362. {
  363. $this->description = $description;
  364. }
  365. /**
  366. * Get description
  367. *
  368. * @return string
  369. */
  370. public function getDescription()
  371. {
  372. return $this->description;
  373. }
  374. /**
  375. * Set language
  376. *
  377. * @param string $language
  378. */
  379. public function setLanguage($language)
  380. {
  381. $this->language = $language;
  382. }
  383. /**
  384. * Get language
  385. *
  386. * @return string
  387. */
  388. public function getLanguage()
  389. {
  390. return $this->language;
  391. }
  392. /**
  393. * Set readme
  394. *
  395. * @param string $readme
  396. */
  397. public function setReadme($readme)
  398. {
  399. $this->readme = $readme;
  400. }
  401. /**
  402. * Get readme
  403. *
  404. * @return string
  405. */
  406. public function getReadme()
  407. {
  408. return $this->readme;
  409. }
  410. /**
  411. * @param int $val
  412. */
  413. public function setGitHubStars($val)
  414. {
  415. $this->gitHubStars = $val;
  416. }
  417. /**
  418. * @return int
  419. */
  420. public function getGitHubStars()
  421. {
  422. return $this->gitHubStars;
  423. }
  424. /**
  425. * @param int $val
  426. */
  427. public function setGitHubWatches($val)
  428. {
  429. $this->gitHubWatches = $val;
  430. }
  431. /**
  432. * @return int
  433. */
  434. public function getGitHubWatches()
  435. {
  436. return $this->gitHubWatches;
  437. }
  438. /**
  439. * @param int $val
  440. */
  441. public function setGitHubForks($val)
  442. {
  443. $this->gitHubForks = $val;
  444. }
  445. /**
  446. * @return int
  447. */
  448. public function getGitHubForks()
  449. {
  450. return $this->gitHubForks;
  451. }
  452. /**
  453. * @param int $val
  454. */
  455. public function setGitHubOpenIssues($val)
  456. {
  457. $this->gitHubOpenIssues = $val;
  458. }
  459. /**
  460. * @return int
  461. */
  462. public function getGitHubOpenIssues()
  463. {
  464. return $this->gitHubOpenIssues;
  465. }
  466. /**
  467. * Set createdAt
  468. *
  469. * @param \DateTime $createdAt
  470. */
  471. public function setCreatedAt($createdAt)
  472. {
  473. $this->createdAt = $createdAt;
  474. }
  475. /**
  476. * Get createdAt
  477. *
  478. * @return \DateTime
  479. */
  480. public function getCreatedAt()
  481. {
  482. return $this->createdAt;
  483. }
  484. /**
  485. * Set repository
  486. *
  487. * @param string $repository
  488. */
  489. public function setRepository($repoUrl)
  490. {
  491. $this->vcsDriver = null;
  492. // prevent local filesystem URLs
  493. if (preg_match('{^(\.|[a-z]:|/)}i', $repoUrl)) {
  494. return;
  495. }
  496. $repoUrl = preg_replace('{^git@github.com:}i', 'https://github.com/', $repoUrl);
  497. $repoUrl = preg_replace('{^git://github.com/}i', 'https://github.com/', $repoUrl);
  498. $repoUrl = preg_replace('{^(https://github.com/.*?)\.git$}i', '$1', $repoUrl);
  499. // normalize protocol case
  500. $repoUrl = preg_replace_callback('{^(https?|git|svn)://}i', function ($match) { return strtolower($match[1]) . '://'; }, $repoUrl);
  501. $this->repository = $repoUrl;
  502. // avoid user@host URLs
  503. if (preg_match('{https?://.+@}', $repoUrl)) {
  504. return;
  505. }
  506. try {
  507. $io = new NullIO();
  508. $config = Factory::createConfig();
  509. $io->loadConfiguration($config);
  510. $repository = new VcsRepository(array('url' => $this->repository), $io, $config);
  511. $driver = $this->vcsDriver = $repository->getDriver();
  512. if (!$driver) {
  513. return;
  514. }
  515. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  516. if (!isset($information['name'])) {
  517. return;
  518. }
  519. if (null === $this->getName()) {
  520. $this->setName($information['name']);
  521. }
  522. if ($driver instanceof GitHubDriver) {
  523. $this->repository = $driver->getRepositoryUrl();
  524. }
  525. } catch (\Exception $e) {
  526. $this->vcsDriverError = '['.get_class($e).'] '.$e->getMessage();
  527. }
  528. }
  529. /**
  530. * Get repository
  531. *
  532. * @return string $repository
  533. */
  534. public function getRepository()
  535. {
  536. return $this->repository;
  537. }
  538. /**
  539. * Add versions
  540. *
  541. * @param Version $versions
  542. */
  543. public function addVersions(Version $versions)
  544. {
  545. $this->versions[] = $versions;
  546. }
  547. /**
  548. * Get versions
  549. *
  550. * @return \Doctrine\Common\Collections\Collection
  551. */
  552. public function getVersions()
  553. {
  554. return $this->versions;
  555. }
  556. public function getVersion($normalizedVersion)
  557. {
  558. if (null === $this->cachedVersions) {
  559. $this->cachedVersions = array();
  560. foreach ($this->getVersions() as $version) {
  561. $this->cachedVersions[strtolower($version->getNormalizedVersion())] = $version;
  562. }
  563. }
  564. if (isset($this->cachedVersions[strtolower($normalizedVersion)])) {
  565. return $this->cachedVersions[strtolower($normalizedVersion)];
  566. }
  567. }
  568. /**
  569. * Set updatedAt
  570. *
  571. * @param \DateTime $updatedAt
  572. */
  573. public function setUpdatedAt($updatedAt)
  574. {
  575. $this->updatedAt = $updatedAt;
  576. $this->setUpdateFailureNotified(false);
  577. }
  578. /**
  579. * Get updatedAt
  580. *
  581. * @return \DateTime
  582. */
  583. public function getUpdatedAt()
  584. {
  585. return $this->updatedAt;
  586. }
  587. /**
  588. * Set crawledAt
  589. *
  590. * @param \DateTime|null $crawledAt
  591. */
  592. public function setCrawledAt($crawledAt)
  593. {
  594. $this->crawledAt = $crawledAt;
  595. }
  596. /**
  597. * Get crawledAt
  598. *
  599. * @return \DateTime
  600. */
  601. public function getCrawledAt()
  602. {
  603. return $this->crawledAt;
  604. }
  605. /**
  606. * Set indexedAt
  607. *
  608. * @param \DateTime $indexedAt
  609. */
  610. public function setIndexedAt($indexedAt)
  611. {
  612. $this->indexedAt = $indexedAt;
  613. }
  614. /**
  615. * Get indexedAt
  616. *
  617. * @return \DateTime
  618. */
  619. public function getIndexedAt()
  620. {
  621. return $this->indexedAt;
  622. }
  623. /**
  624. * Set dumpedAt
  625. *
  626. * @param \DateTime $dumpedAt
  627. */
  628. public function setDumpedAt($dumpedAt)
  629. {
  630. $this->dumpedAt = $dumpedAt;
  631. }
  632. /**
  633. * Get dumpedAt
  634. *
  635. * @return \DateTime
  636. */
  637. public function getDumpedAt()
  638. {
  639. return $this->dumpedAt;
  640. }
  641. /**
  642. * Add maintainers
  643. *
  644. * @param User $maintainer
  645. */
  646. public function addMaintainer(User $maintainer)
  647. {
  648. $this->maintainers[] = $maintainer;
  649. }
  650. /**
  651. * Get maintainers
  652. *
  653. * @return \Doctrine\Common\Collections\Collection
  654. */
  655. public function getMaintainers()
  656. {
  657. return $this->maintainers;
  658. }
  659. /**
  660. * Set type
  661. *
  662. * @param string $type
  663. */
  664. public function setType($type)
  665. {
  666. $this->type = $type;
  667. }
  668. /**
  669. * Get type
  670. *
  671. * @return string
  672. */
  673. public function getType()
  674. {
  675. return $this->type;
  676. }
  677. /**
  678. * Set autoUpdated
  679. *
  680. * @param Boolean $autoUpdated
  681. */
  682. public function setAutoUpdated($autoUpdated)
  683. {
  684. $this->autoUpdated = $autoUpdated;
  685. }
  686. /**
  687. * Get autoUpdated
  688. *
  689. * @return Boolean
  690. */
  691. public function isAutoUpdated()
  692. {
  693. return $this->autoUpdated;
  694. }
  695. /**
  696. * Set updateFailureNotified
  697. *
  698. * @param Boolean $updateFailureNotified
  699. */
  700. public function setUpdateFailureNotified($updateFailureNotified)
  701. {
  702. $this->updateFailureNotified = $updateFailureNotified;
  703. }
  704. /**
  705. * Get updateFailureNotified
  706. *
  707. * @return Boolean
  708. */
  709. public function isUpdateFailureNotified()
  710. {
  711. return $this->updateFailureNotified;
  712. }
  713. /**
  714. * @return boolean
  715. */
  716. public function isAbandoned()
  717. {
  718. return $this->abandoned;
  719. }
  720. /**
  721. * @param boolean $abandoned
  722. */
  723. public function setAbandoned($abandoned)
  724. {
  725. $this->abandoned = $abandoned;
  726. }
  727. /**
  728. * @return string
  729. */
  730. public function getReplacementPackage()
  731. {
  732. return $this->replacementPackage;
  733. }
  734. /**
  735. * @param string $replacementPackage
  736. */
  737. public function setReplacementPackage($replacementPackage)
  738. {
  739. $this->replacementPackage = $replacementPackage;
  740. }
  741. public static function sortVersions($a, $b)
  742. {
  743. $aVersion = $a->getNormalizedVersion();
  744. $bVersion = $b->getNormalizedVersion();
  745. $aVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $aVersion);
  746. $bVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $bVersion);
  747. // equal versions are sorted by date
  748. if ($aVersion === $bVersion) {
  749. return $b->getReleasedAt() > $a->getReleasedAt() ? 1 : -1;
  750. }
  751. // the rest is sorted by version
  752. return version_compare($bVersion, $aVersion);
  753. }
  754. }