Package.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. $this->versions = $versionRepo->refreshVersions($this->getVersions());
  156. foreach ($this->getVersions() as $version) {
  157. $versionIds[] = $version->getId();
  158. }
  159. $versionData = $versionRepo->getVersionData($versionIds);
  160. foreach ($this->getVersions() as $version) {
  161. /** @var $version Version */
  162. $versions[$version->getVersion()] = $version->toArray($versionData);
  163. }
  164. $maintainers = array();
  165. foreach ($this->getMaintainers() as $maintainer) {
  166. /** @var $maintainer User */
  167. $maintainers[] = $maintainer->toArray();
  168. }
  169. $data = array(
  170. 'name' => $this->getName(),
  171. 'description' => $this->getDescription(),
  172. 'time' => $this->getCreatedAt()->format('c'),
  173. 'maintainers' => $maintainers,
  174. 'versions' => $versions,
  175. 'type' => $this->getType(),
  176. 'repository' => $this->getRepository(),
  177. 'github_stars' => $this->getGitHubStars(),
  178. 'github_watchers' => $this->getGitHubWatches(),
  179. 'github_forks' => $this->getGitHubForks(),
  180. 'github_open_issues' => $this->getGitHubOpenIssues(),
  181. 'language' => $this->getLanguage(),
  182. );
  183. if ($this->isAbandoned()) {
  184. $data['abandoned'] = $this->getReplacementPackage() ?: true;
  185. }
  186. return $data;
  187. }
  188. public function isRepositoryValid(ExecutionContextInterface $context)
  189. {
  190. // vcs driver was not nulled which means the repository was not set/modified and is still valid
  191. if (true === $this->vcsDriver && null !== $this->getName()) {
  192. return;
  193. }
  194. $property = 'repository';
  195. $driver = $this->vcsDriver;
  196. if (!is_object($driver)) {
  197. if (preg_match('{https?://.+@}', $this->repository)) {
  198. $context->buildViolation('URLs with user@host are not supported, use a read-only public URL')
  199. ->atPath($property)
  200. ->addViolation()
  201. ;
  202. } elseif (is_string($this->vcsDriverError)) {
  203. $context->buildViolation('Uncaught Exception: '.htmlentities($this->vcsDriverError, ENT_COMPAT, 'utf-8'))
  204. ->atPath($property)
  205. ->addViolation()
  206. ;
  207. } else {
  208. $context->buildViolation('No valid/supported repository was found at the given URL')
  209. ->atPath($property)
  210. ->addViolation()
  211. ;
  212. }
  213. return;
  214. }
  215. try {
  216. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  217. if (false === $information) {
  218. $context->buildViolation('No composer.json was found in the '.$driver->getRootIdentifier().' branch.')
  219. ->atPath($property)
  220. ->addViolation()
  221. ;
  222. return;
  223. }
  224. if (empty($information['name'])) {
  225. $context->buildViolation('The package name was not found in the composer.json, make sure there is a name present.')
  226. ->atPath($property)
  227. ->addViolation()
  228. ;
  229. return;
  230. }
  231. if (!preg_match('{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*$}i', $information['name'])) {
  232. $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]+)*".')
  233. ->atPath($property)
  234. ->addViolation()
  235. ;
  236. return;
  237. }
  238. $reservedNames = ['nul', 'con', 'prn', 'aux', 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9'];
  239. $bits = explode('/', strtolower($information['name']));
  240. if (in_array($bits[0], $reservedNames, true) || in_array($bits[1], $reservedNames, true)) {
  241. $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).'.')
  242. ->atPath($property)
  243. ->addViolation()
  244. ;
  245. return;
  246. }
  247. if (preg_match('{\.json$}', $information['name'])) {
  248. $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.')
  249. ->atPath($property)
  250. ->addViolation()
  251. ;
  252. return;
  253. }
  254. if (preg_match('{[A-Z]}', $information['name'])) {
  255. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $information['name']);
  256. $suggestName = strtolower($suggestName);
  257. $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.')
  258. ->atPath($property)
  259. ->addViolation()
  260. ;
  261. return;
  262. }
  263. } catch (\Exception $e) {
  264. $context->buildViolation('We had problems parsing your composer.json file, the parser reports: '.htmlentities($e->getMessage(), ENT_COMPAT, 'utf-8'))
  265. ->atPath($property)
  266. ->addViolation()
  267. ;
  268. }
  269. if (null === $this->getName()) {
  270. $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')
  271. ->atPath($property)
  272. ->addViolation()
  273. ;
  274. }
  275. }
  276. public function setEntityRepository($repository)
  277. {
  278. $this->entityRepository = $repository;
  279. }
  280. public function setRouter($router)
  281. {
  282. $this->router = $router;
  283. }
  284. public function isPackageUnique(ExecutionContextInterface $context)
  285. {
  286. try {
  287. if ($this->entityRepository->findOneByName($this->name)) {
  288. $context->buildViolation('A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.')
  289. ->atPath('repository')
  290. ->addViolation()
  291. ;
  292. }
  293. } catch (\Doctrine\ORM\NoResultException $e) {}
  294. }
  295. public function isVendorWritable(ExecutionContextInterface $context)
  296. {
  297. try {
  298. $vendor = $this->getVendor();
  299. if ($vendor && $this->entityRepository->isVendorTaken($vendor, reset($this->maintainers))) {
  300. $context->buildViolation('The vendor is already taken by someone else. '
  301. . 'You may ask them to add your package and give you maintainership access. '
  302. . 'If they add you as a maintainer on any package in that vendor namespace, '
  303. . 'you will then be able to add new packages in that namespace. '
  304. . 'The packages already in that vendor namespace can be found at '
  305. . '<a href="'.$this->router->generate('view_vendor', array('vendor' => $vendor)).'">'.$vendor.'</a>')
  306. ->atPath('repository')
  307. ->addViolation()
  308. ;
  309. }
  310. } catch (\Doctrine\ORM\NoResultException $e) {}
  311. }
  312. /**
  313. * Get id
  314. *
  315. * @return string
  316. */
  317. public function getId()
  318. {
  319. return $this->id;
  320. }
  321. /**
  322. * Set name
  323. *
  324. * @param string $name
  325. */
  326. public function setName($name)
  327. {
  328. $this->name = $name;
  329. }
  330. /**
  331. * Get name
  332. *
  333. * @return string
  334. */
  335. public function getName()
  336. {
  337. return $this->name;
  338. }
  339. /**
  340. * Get vendor prefix
  341. *
  342. * @return string
  343. */
  344. public function getVendor()
  345. {
  346. return preg_replace('{/.*$}', '', $this->name);
  347. }
  348. /**
  349. * Get package name without vendor
  350. *
  351. * @return string
  352. */
  353. public function getPackageName()
  354. {
  355. return preg_replace('{^[^/]*/}', '', $this->name);
  356. }
  357. /**
  358. * Set description
  359. *
  360. * @param string $description
  361. */
  362. public function setDescription($description)
  363. {
  364. $this->description = $description;
  365. }
  366. /**
  367. * Get description
  368. *
  369. * @return string
  370. */
  371. public function getDescription()
  372. {
  373. return $this->description;
  374. }
  375. /**
  376. * Set language
  377. *
  378. * @param string $language
  379. */
  380. public function setLanguage($language)
  381. {
  382. $this->language = $language;
  383. }
  384. /**
  385. * Get language
  386. *
  387. * @return string
  388. */
  389. public function getLanguage()
  390. {
  391. return $this->language;
  392. }
  393. /**
  394. * Set readme
  395. *
  396. * @param string $readme
  397. */
  398. public function setReadme($readme)
  399. {
  400. $this->readme = $readme;
  401. }
  402. /**
  403. * Get readme
  404. *
  405. * @return string
  406. */
  407. public function getReadme()
  408. {
  409. return $this->readme;
  410. }
  411. /**
  412. * @param int $val
  413. */
  414. public function setGitHubStars($val)
  415. {
  416. $this->gitHubStars = $val;
  417. }
  418. /**
  419. * @return int
  420. */
  421. public function getGitHubStars()
  422. {
  423. return $this->gitHubStars;
  424. }
  425. /**
  426. * @param int $val
  427. */
  428. public function setGitHubWatches($val)
  429. {
  430. $this->gitHubWatches = $val;
  431. }
  432. /**
  433. * @return int
  434. */
  435. public function getGitHubWatches()
  436. {
  437. return $this->gitHubWatches;
  438. }
  439. /**
  440. * @param int $val
  441. */
  442. public function setGitHubForks($val)
  443. {
  444. $this->gitHubForks = $val;
  445. }
  446. /**
  447. * @return int
  448. */
  449. public function getGitHubForks()
  450. {
  451. return $this->gitHubForks;
  452. }
  453. /**
  454. * @param int $val
  455. */
  456. public function setGitHubOpenIssues($val)
  457. {
  458. $this->gitHubOpenIssues = $val;
  459. }
  460. /**
  461. * @return int
  462. */
  463. public function getGitHubOpenIssues()
  464. {
  465. return $this->gitHubOpenIssues;
  466. }
  467. /**
  468. * Set createdAt
  469. *
  470. * @param \DateTime $createdAt
  471. */
  472. public function setCreatedAt($createdAt)
  473. {
  474. $this->createdAt = $createdAt;
  475. }
  476. /**
  477. * Get createdAt
  478. *
  479. * @return \DateTime
  480. */
  481. public function getCreatedAt()
  482. {
  483. return $this->createdAt;
  484. }
  485. /**
  486. * Set repository
  487. *
  488. * @param string $repository
  489. */
  490. public function setRepository($repoUrl)
  491. {
  492. $this->vcsDriver = null;
  493. // prevent local filesystem URLs
  494. if (preg_match('{^(\.|[a-z]:|/)}i', $repoUrl)) {
  495. return;
  496. }
  497. $repoUrl = preg_replace('{^git@github.com:}i', 'https://github.com/', $repoUrl);
  498. $repoUrl = preg_replace('{^git://github.com/}i', 'https://github.com/', $repoUrl);
  499. $repoUrl = preg_replace('{^(https://github.com/.*?)\.git$}i', '$1', $repoUrl);
  500. // normalize protocol case
  501. $repoUrl = preg_replace_callback('{^(https?|git|svn)://}i', function ($match) { return strtolower($match[1]) . '://'; }, $repoUrl);
  502. $this->repository = $repoUrl;
  503. // avoid user@host URLs
  504. if (preg_match('{https?://.+@}', $repoUrl)) {
  505. return;
  506. }
  507. try {
  508. $io = new NullIO();
  509. $config = Factory::createConfig();
  510. $io->loadConfiguration($config);
  511. $repository = new VcsRepository(array('url' => $this->repository), $io, $config);
  512. $driver = $this->vcsDriver = $repository->getDriver();
  513. if (!$driver) {
  514. return;
  515. }
  516. $information = $driver->getComposerInformation($driver->getRootIdentifier());
  517. if (!isset($information['name'])) {
  518. return;
  519. }
  520. if (null === $this->getName()) {
  521. $this->setName($information['name']);
  522. }
  523. if ($driver instanceof GitHubDriver) {
  524. $this->repository = $driver->getRepositoryUrl();
  525. }
  526. } catch (\Exception $e) {
  527. $this->vcsDriverError = '['.get_class($e).'] '.$e->getMessage();
  528. }
  529. }
  530. /**
  531. * Get repository
  532. *
  533. * @return string $repository
  534. */
  535. public function getRepository()
  536. {
  537. return $this->repository;
  538. }
  539. /**
  540. * Add versions
  541. *
  542. * @param Version $versions
  543. */
  544. public function addVersions(Version $versions)
  545. {
  546. $this->versions[] = $versions;
  547. }
  548. /**
  549. * Get versions
  550. *
  551. * @return \Doctrine\Common\Collections\Collection
  552. */
  553. public function getVersions()
  554. {
  555. return $this->versions;
  556. }
  557. public function getVersion($normalizedVersion)
  558. {
  559. if (null === $this->cachedVersions) {
  560. $this->cachedVersions = array();
  561. foreach ($this->getVersions() as $version) {
  562. $this->cachedVersions[strtolower($version->getNormalizedVersion())] = $version;
  563. }
  564. }
  565. if (isset($this->cachedVersions[strtolower($normalizedVersion)])) {
  566. return $this->cachedVersions[strtolower($normalizedVersion)];
  567. }
  568. }
  569. /**
  570. * Set updatedAt
  571. *
  572. * @param \DateTime $updatedAt
  573. */
  574. public function setUpdatedAt($updatedAt)
  575. {
  576. $this->updatedAt = $updatedAt;
  577. $this->setUpdateFailureNotified(false);
  578. }
  579. /**
  580. * Get updatedAt
  581. *
  582. * @return \DateTime
  583. */
  584. public function getUpdatedAt()
  585. {
  586. return $this->updatedAt;
  587. }
  588. /**
  589. * Set crawledAt
  590. *
  591. * @param \DateTime|null $crawledAt
  592. */
  593. public function setCrawledAt($crawledAt)
  594. {
  595. $this->crawledAt = $crawledAt;
  596. }
  597. /**
  598. * Get crawledAt
  599. *
  600. * @return \DateTime
  601. */
  602. public function getCrawledAt()
  603. {
  604. return $this->crawledAt;
  605. }
  606. /**
  607. * Set indexedAt
  608. *
  609. * @param \DateTime $indexedAt
  610. */
  611. public function setIndexedAt($indexedAt)
  612. {
  613. $this->indexedAt = $indexedAt;
  614. }
  615. /**
  616. * Get indexedAt
  617. *
  618. * @return \DateTime
  619. */
  620. public function getIndexedAt()
  621. {
  622. return $this->indexedAt;
  623. }
  624. /**
  625. * Set dumpedAt
  626. *
  627. * @param \DateTime $dumpedAt
  628. */
  629. public function setDumpedAt($dumpedAt)
  630. {
  631. $this->dumpedAt = $dumpedAt;
  632. }
  633. /**
  634. * Get dumpedAt
  635. *
  636. * @return \DateTime
  637. */
  638. public function getDumpedAt()
  639. {
  640. return $this->dumpedAt;
  641. }
  642. /**
  643. * Add maintainers
  644. *
  645. * @param User $maintainer
  646. */
  647. public function addMaintainer(User $maintainer)
  648. {
  649. $this->maintainers[] = $maintainer;
  650. }
  651. /**
  652. * Get maintainers
  653. *
  654. * @return \Doctrine\Common\Collections\Collection
  655. */
  656. public function getMaintainers()
  657. {
  658. return $this->maintainers;
  659. }
  660. /**
  661. * Set type
  662. *
  663. * @param string $type
  664. */
  665. public function setType($type)
  666. {
  667. $this->type = $type;
  668. }
  669. /**
  670. * Get type
  671. *
  672. * @return string
  673. */
  674. public function getType()
  675. {
  676. return $this->type;
  677. }
  678. /**
  679. * Set autoUpdated
  680. *
  681. * @param Boolean $autoUpdated
  682. */
  683. public function setAutoUpdated($autoUpdated)
  684. {
  685. $this->autoUpdated = $autoUpdated;
  686. }
  687. /**
  688. * Get autoUpdated
  689. *
  690. * @return Boolean
  691. */
  692. public function isAutoUpdated()
  693. {
  694. return $this->autoUpdated;
  695. }
  696. /**
  697. * Set updateFailureNotified
  698. *
  699. * @param Boolean $updateFailureNotified
  700. */
  701. public function setUpdateFailureNotified($updateFailureNotified)
  702. {
  703. $this->updateFailureNotified = $updateFailureNotified;
  704. }
  705. /**
  706. * Get updateFailureNotified
  707. *
  708. * @return Boolean
  709. */
  710. public function isUpdateFailureNotified()
  711. {
  712. return $this->updateFailureNotified;
  713. }
  714. /**
  715. * @return boolean
  716. */
  717. public function isAbandoned()
  718. {
  719. return $this->abandoned;
  720. }
  721. /**
  722. * @param boolean $abandoned
  723. */
  724. public function setAbandoned($abandoned)
  725. {
  726. $this->abandoned = $abandoned;
  727. }
  728. /**
  729. * @return string
  730. */
  731. public function getReplacementPackage()
  732. {
  733. return $this->replacementPackage;
  734. }
  735. /**
  736. * @param string $replacementPackage
  737. */
  738. public function setReplacementPackage($replacementPackage)
  739. {
  740. $this->replacementPackage = $replacementPackage;
  741. }
  742. public static function sortVersions($a, $b)
  743. {
  744. $aVersion = $a->getNormalizedVersion();
  745. $bVersion = $b->getNormalizedVersion();
  746. $aVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $aVersion);
  747. $bVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $bVersion);
  748. // equal versions are sorted by date
  749. if ($aVersion === $bVersion) {
  750. return $b->getReleasedAt() > $a->getReleasedAt() ? 1 : -1;
  751. }
  752. // the rest is sorted by version
  753. return version_compare($bVersion, $aVersion);
  754. }
  755. }