Version.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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\Package\Version\VersionParser;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\VersionRepository")
  18. * @ORM\Table(
  19. * name="package_version",
  20. * uniqueConstraints={@ORM\UniqueConstraint(name="pkg_ver_idx",columns={"package_id","normalizedVersion"})},
  21. * indexes={
  22. * @ORM\Index(name="release_idx",columns={"releasedAt"}),
  23. * @ORM\Index(name="is_devel_idx",columns={"development"})
  24. * }
  25. * )
  26. * @author Jordi Boggiano <j.boggiano@seld.be>
  27. */
  28. class Version
  29. {
  30. /**
  31. * @ORM\Id
  32. * @ORM\Column(type="integer")
  33. * @ORM\GeneratedValue(strategy="AUTO")
  34. */
  35. private $id;
  36. /**
  37. * @ORM\Column
  38. * @Assert\NotBlank()
  39. */
  40. private $name;
  41. /**
  42. * @ORM\Column(type="text", nullable=true)
  43. */
  44. private $description;
  45. /**
  46. * @ORM\Column(nullable=true)
  47. */
  48. private $type;
  49. /**
  50. * @ORM\Column(nullable=true)
  51. */
  52. private $targetDir;
  53. /**
  54. * @ORM\Column(type="array", nullable=true)
  55. */
  56. private $extra = array();
  57. /**
  58. * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Tag", inversedBy="versions")
  59. * @ORM\JoinTable(name="version_tag",
  60. * joinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")},
  61. * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
  62. * )
  63. */
  64. private $tags;
  65. /**
  66. * @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Package", fetch="EAGER", inversedBy="versions")
  67. * @Assert\Type(type="Packagist\WebBundle\Entity\Package")
  68. */
  69. private $package;
  70. /**
  71. * @ORM\Column(nullable=true)
  72. * @Assert\Url()
  73. */
  74. private $homepage;
  75. /**
  76. * @ORM\Column
  77. * @Assert\NotBlank()
  78. */
  79. private $version;
  80. /**
  81. * @ORM\Column(length=191)
  82. * @Assert\NotBlank()
  83. */
  84. private $normalizedVersion;
  85. /**
  86. * @ORM\Column(type="boolean")
  87. * @Assert\NotBlank()
  88. */
  89. private $development;
  90. /**
  91. * @ORM\Column(type="text", nullable=true)
  92. */
  93. private $license;
  94. /**
  95. * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Author", inversedBy="versions")
  96. * @ORM\JoinTable(name="version_author",
  97. * joinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")},
  98. * inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
  99. * )
  100. */
  101. private $authors;
  102. /**
  103. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\RequireLink", mappedBy="version")
  104. */
  105. private $require;
  106. /**
  107. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\ReplaceLink", mappedBy="version")
  108. */
  109. private $replace;
  110. /**
  111. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\ConflictLink", mappedBy="version")
  112. */
  113. private $conflict;
  114. /**
  115. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\ProvideLink", mappedBy="version")
  116. */
  117. private $provide;
  118. /**
  119. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\DevRequireLink", mappedBy="version")
  120. */
  121. private $devRequire;
  122. /**
  123. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\SuggestLink", mappedBy="version")
  124. */
  125. private $suggest;
  126. /**
  127. * @ORM\Column(type="text", nullable=true)
  128. */
  129. private $source;
  130. /**
  131. * @ORM\Column(type="text", nullable=true)
  132. */
  133. private $dist;
  134. /**
  135. * @ORM\Column(type="text", nullable=true)
  136. */
  137. private $autoload;
  138. /**
  139. * @ORM\Column(type="text", nullable=true)
  140. */
  141. private $binaries;
  142. /**
  143. * @ORM\Column(type="text", nullable=true)
  144. */
  145. private $includePaths;
  146. /**
  147. * @ORM\Column(type="text", nullable=true)
  148. */
  149. private $support;
  150. /**
  151. * @ORM\Column(type="datetime")
  152. */
  153. private $createdAt;
  154. /**
  155. * @ORM\Column(type="datetime", nullable=true)
  156. */
  157. private $softDeletedAt;
  158. /**
  159. * @ORM\Column(type="datetime")
  160. */
  161. private $updatedAt;
  162. /**
  163. * @ORM\Column(type="datetime", nullable=true)
  164. */
  165. private $releasedAt;
  166. public function __construct()
  167. {
  168. $this->tags = new ArrayCollection();
  169. $this->require = new ArrayCollection();
  170. $this->replace = new ArrayCollection();
  171. $this->conflict = new ArrayCollection();
  172. $this->provide = new ArrayCollection();
  173. $this->devRequire = new ArrayCollection();
  174. $this->suggest = new ArrayCollection();
  175. $this->authors = new ArrayCollection();
  176. $this->createdAt = new \DateTime;
  177. $this->updatedAt = new \DateTime;
  178. }
  179. public function toArray(array $versionData)
  180. {
  181. $tags = array();
  182. foreach ($this->getTags() as $tag) {
  183. /** @var $tag Tag */
  184. $tags[] = $tag->getName();
  185. }
  186. if (isset($versionData[$this->id]['authors'])) {
  187. $authors = $versionData[$this->id]['authors'];
  188. } else {
  189. $authors = array();
  190. foreach ($this->getAuthors() as $author) {
  191. /** @var $author Author */
  192. $authors[] = $author->toArray();
  193. }
  194. }
  195. $data = array(
  196. 'name' => $this->getName(),
  197. 'description' => (string) $this->getDescription(),
  198. 'keywords' => $tags,
  199. 'homepage' => (string) $this->getHomepage(),
  200. 'version' => $this->getVersion(),
  201. 'version_normalized' => $this->getNormalizedVersion(),
  202. 'license' => $this->getLicense(),
  203. 'authors' => $authors,
  204. 'source' => $this->getSource(),
  205. 'dist' => $this->getDist(),
  206. 'type' => $this->getType(),
  207. );
  208. if ($this->getReleasedAt()) {
  209. $data['time'] = $this->getReleasedAt()->format('Y-m-d\TH:i:sP');
  210. }
  211. if ($this->getAutoload()) {
  212. $data['autoload'] = $this->getAutoload();
  213. }
  214. if ($this->getExtra()) {
  215. $data['extra'] = $this->getExtra();
  216. }
  217. if ($this->getTargetDir()) {
  218. $data['target-dir'] = $this->getTargetDir();
  219. }
  220. if ($this->getIncludePaths()) {
  221. $data['include-path'] = $this->getIncludePaths();
  222. }
  223. if ($this->getBinaries()) {
  224. $data['bin'] = $this->getBinaries();
  225. }
  226. $supportedLinkTypes = array(
  227. 'require' => 'require',
  228. 'devRequire' => 'require-dev',
  229. 'suggest' => 'suggest',
  230. 'conflict' => 'conflict',
  231. 'provide' => 'provide',
  232. 'replace' => 'replace',
  233. );
  234. foreach ($supportedLinkTypes as $method => $linkType) {
  235. if (isset($versionData[$this->id][$method])) {
  236. foreach ($versionData[$this->id][$method] as $link) {
  237. $data[$linkType][$link['name']] = $link['version'];
  238. }
  239. continue;
  240. }
  241. foreach ($this->{'get'.$method}() as $link) {
  242. $link = $link->toArray();
  243. $data[$linkType][key($link)] = current($link);
  244. }
  245. }
  246. if ($this->getPackage()->isAbandoned()) {
  247. $data['abandoned'] = $this->getPackage()->getReplacementPackage() ?: true;
  248. }
  249. return $data;
  250. }
  251. public function equals(Version $version)
  252. {
  253. return strtolower($version->getName()) === strtolower($this->getName())
  254. && strtolower($version->getNormalizedVersion()) === strtolower($this->getNormalizedVersion());
  255. }
  256. /**
  257. * Get id
  258. *
  259. * @return string $id
  260. */
  261. public function getId()
  262. {
  263. return $this->id;
  264. }
  265. /**
  266. * Set name
  267. *
  268. * @param string $name
  269. */
  270. public function setName($name)
  271. {
  272. $this->name = $name;
  273. }
  274. /**
  275. * Get name
  276. *
  277. * @return string $name
  278. */
  279. public function getName()
  280. {
  281. return $this->name;
  282. }
  283. public function getNames()
  284. {
  285. $names = array(
  286. strtolower($this->name) => true
  287. );
  288. foreach ($this->getReplace() as $link) {
  289. $names[strtolower($link->getPackageName())] = true;
  290. }
  291. foreach ($this->getProvide() as $link) {
  292. $names[strtolower($link->getPackageName())] = true;
  293. }
  294. return array_keys($names);
  295. }
  296. /**
  297. * Set description
  298. *
  299. * @param string $description
  300. */
  301. public function setDescription($description)
  302. {
  303. $this->description = $description;
  304. }
  305. /**
  306. * Get description
  307. *
  308. * @return string $description
  309. */
  310. public function getDescription()
  311. {
  312. return $this->description;
  313. }
  314. /**
  315. * Set homepage
  316. *
  317. * @param string $homepage
  318. */
  319. public function setHomepage($homepage)
  320. {
  321. $this->homepage = $homepage;
  322. }
  323. /**
  324. * Get homepage
  325. *
  326. * @return string $homepage
  327. */
  328. public function getHomepage()
  329. {
  330. return $this->homepage;
  331. }
  332. /**
  333. * Set version
  334. *
  335. * @param string $version
  336. */
  337. public function setVersion($version)
  338. {
  339. $this->version = $version;
  340. }
  341. /**
  342. * Get version
  343. *
  344. * @return string $version
  345. */
  346. public function getVersion()
  347. {
  348. return $this->version;
  349. }
  350. /**
  351. * @return string
  352. */
  353. public function getRequireVersion()
  354. {
  355. return preg_replace('{^v(\d)}', '$1', str_replace('.x-dev', '.*@dev', $this->getVersion()));
  356. }
  357. /**
  358. * Set normalizedVersion
  359. *
  360. * @param string $normalizedVersion
  361. */
  362. public function setNormalizedVersion($normalizedVersion)
  363. {
  364. $this->normalizedVersion = $normalizedVersion;
  365. }
  366. /**
  367. * Get normalizedVersion
  368. *
  369. * @return string $normalizedVersion
  370. */
  371. public function getNormalizedVersion()
  372. {
  373. return $this->normalizedVersion;
  374. }
  375. /**
  376. * Set license
  377. *
  378. * @param array $license
  379. */
  380. public function setLicense(array $license)
  381. {
  382. $this->license = json_encode($license);
  383. }
  384. /**
  385. * Get license
  386. *
  387. * @return array $license
  388. */
  389. public function getLicense()
  390. {
  391. return json_decode($this->license, true);
  392. }
  393. /**
  394. * Set source
  395. *
  396. * @param array $source
  397. */
  398. public function setSource($source)
  399. {
  400. $this->source = null === $source ? $source : json_encode($source);
  401. }
  402. /**
  403. * Get source
  404. *
  405. * @return array|null
  406. */
  407. public function getSource()
  408. {
  409. return json_decode($this->source, true);
  410. }
  411. /**
  412. * Set dist
  413. *
  414. * @param array $dist
  415. */
  416. public function setDist($dist)
  417. {
  418. $this->dist = null === $dist ? $dist : json_encode($dist);
  419. }
  420. /**
  421. * Get dist
  422. *
  423. * @return array|null
  424. */
  425. public function getDist()
  426. {
  427. return json_decode($this->dist, true);
  428. }
  429. /**
  430. * Set autoload
  431. *
  432. * @param array $autoload
  433. */
  434. public function setAutoload($autoload)
  435. {
  436. $this->autoload = json_encode($autoload);
  437. }
  438. /**
  439. * Get autoload
  440. *
  441. * @return array|null
  442. */
  443. public function getAutoload()
  444. {
  445. return json_decode($this->autoload, true);
  446. }
  447. /**
  448. * Set binaries
  449. *
  450. * @param array $binaries
  451. */
  452. public function setBinaries($binaries)
  453. {
  454. $this->binaries = null === $binaries ? $binaries : json_encode($binaries);
  455. }
  456. /**
  457. * Get binaries
  458. *
  459. * @return array|null
  460. */
  461. public function getBinaries()
  462. {
  463. return json_decode($this->binaries, true);
  464. }
  465. /**
  466. * Set include paths.
  467. *
  468. * @param array $paths
  469. */
  470. public function setIncludePaths($paths)
  471. {
  472. $this->includePaths = $paths ? json_encode($paths) : null;
  473. }
  474. /**
  475. * Get include paths.
  476. *
  477. * @return array|null
  478. */
  479. public function getIncludePaths()
  480. {
  481. return json_decode($this->includePaths, true);
  482. }
  483. /**
  484. * Set support
  485. *
  486. * @param array $support
  487. */
  488. public function setSupport($support)
  489. {
  490. $this->support = $support ? json_encode($support) : null;
  491. }
  492. /**
  493. * Get support
  494. *
  495. * @return array|null
  496. */
  497. public function getSupport()
  498. {
  499. return json_decode($this->support, true);
  500. }
  501. /**
  502. * Set createdAt
  503. *
  504. * @param \DateTime $createdAt
  505. */
  506. public function setCreatedAt($createdAt)
  507. {
  508. $this->createdAt = $createdAt;
  509. }
  510. /**
  511. * Get createdAt
  512. *
  513. * @return \DateTime
  514. */
  515. public function getCreatedAt()
  516. {
  517. return $this->createdAt;
  518. }
  519. /**
  520. * Set releasedAt
  521. *
  522. * @param \DateTime $releasedAt
  523. */
  524. public function setReleasedAt($releasedAt)
  525. {
  526. $this->releasedAt = $releasedAt;
  527. }
  528. /**
  529. * Get releasedAt
  530. *
  531. * @return \DateTime
  532. */
  533. public function getReleasedAt()
  534. {
  535. return $this->releasedAt;
  536. }
  537. /**
  538. * Set package
  539. *
  540. * @param Package $package
  541. */
  542. public function setPackage(Package $package)
  543. {
  544. $this->package = $package;
  545. }
  546. /**
  547. * Get package
  548. *
  549. * @return Package
  550. */
  551. public function getPackage()
  552. {
  553. return $this->package;
  554. }
  555. /**
  556. * Get tags
  557. *
  558. * @return Tag[]
  559. */
  560. public function getTags()
  561. {
  562. return $this->tags;
  563. }
  564. /**
  565. * Set updatedAt
  566. *
  567. * @param \DateTime $updatedAt
  568. */
  569. public function setUpdatedAt($updatedAt)
  570. {
  571. $this->updatedAt = $updatedAt;
  572. }
  573. /**
  574. * Get updatedAt
  575. *
  576. * @return \DateTime $updatedAt
  577. */
  578. public function getUpdatedAt()
  579. {
  580. return $this->updatedAt;
  581. }
  582. /**
  583. * Set softDeletedAt
  584. *
  585. * @param \DateTime|null $softDeletedAt
  586. */
  587. public function setSoftDeletedAt($softDeletedAt)
  588. {
  589. $this->softDeletedAt = $softDeletedAt;
  590. }
  591. /**
  592. * Get softDeletedAt
  593. *
  594. * @return \DateTime|null $softDeletedAt
  595. */
  596. public function getSoftDeletedAt()
  597. {
  598. return $this->softDeletedAt;
  599. }
  600. /**
  601. * Get authors
  602. *
  603. * @return Author[]
  604. */
  605. public function getAuthors()
  606. {
  607. return $this->authors;
  608. }
  609. /**
  610. * Set type
  611. *
  612. * @param string $type
  613. */
  614. public function setType($type)
  615. {
  616. $this->type = $type;
  617. }
  618. /**
  619. * Get type
  620. *
  621. * @return string
  622. */
  623. public function getType()
  624. {
  625. return $this->type;
  626. }
  627. /**
  628. * Set targetDir
  629. *
  630. * @param string $targetDir
  631. */
  632. public function setTargetDir($targetDir)
  633. {
  634. $this->targetDir = $targetDir;
  635. }
  636. /**
  637. * Get targetDir
  638. *
  639. * @return string
  640. */
  641. public function getTargetDir()
  642. {
  643. return $this->targetDir;
  644. }
  645. /**
  646. * Set extra
  647. *
  648. * @param array $extra
  649. */
  650. public function setExtra($extra)
  651. {
  652. $this->extra = $extra;
  653. }
  654. /**
  655. * Get extra
  656. *
  657. * @return array
  658. */
  659. public function getExtra()
  660. {
  661. return $this->extra;
  662. }
  663. /**
  664. * Set development
  665. *
  666. * @param Boolean $development
  667. */
  668. public function setDevelopment($development)
  669. {
  670. $this->development = $development;
  671. }
  672. /**
  673. * Get development
  674. *
  675. * @return Boolean
  676. */
  677. public function getDevelopment()
  678. {
  679. return $this->development;
  680. }
  681. /**
  682. * @return Boolean
  683. */
  684. public function isDevelopment()
  685. {
  686. return $this->getDevelopment();
  687. }
  688. /**
  689. * Add tag
  690. *
  691. * @param Tag $tag
  692. */
  693. public function addTag(Tag $tag)
  694. {
  695. $this->tags[] = $tag;
  696. }
  697. /**
  698. * Add authors
  699. *
  700. * @param Author $author
  701. */
  702. public function addAuthor(Author $author)
  703. {
  704. $this->authors[] = $author;
  705. }
  706. /**
  707. * Add require
  708. *
  709. * @param RequireLink $require
  710. */
  711. public function addRequireLink(RequireLink $require)
  712. {
  713. $this->require[] = $require;
  714. }
  715. /**
  716. * Get require
  717. *
  718. * @return RequireLink[]
  719. */
  720. public function getRequire()
  721. {
  722. return $this->require;
  723. }
  724. /**
  725. * Add replace
  726. *
  727. * @param ReplaceLink $replace
  728. */
  729. public function addReplaceLink(ReplaceLink $replace)
  730. {
  731. $this->replace[] = $replace;
  732. }
  733. /**
  734. * Get replace
  735. *
  736. * @return ReplaceLink[]
  737. */
  738. public function getReplace()
  739. {
  740. return $this->replace;
  741. }
  742. /**
  743. * Add conflict
  744. *
  745. * @param ConflictLink $conflict
  746. */
  747. public function addConflictLink(ConflictLink $conflict)
  748. {
  749. $this->conflict[] = $conflict;
  750. }
  751. /**
  752. * Get conflict
  753. *
  754. * @return ConflictLink[]
  755. */
  756. public function getConflict()
  757. {
  758. return $this->conflict;
  759. }
  760. /**
  761. * Add provide
  762. *
  763. * @param ProvideLink $provide
  764. */
  765. public function addProvideLink(ProvideLink $provide)
  766. {
  767. $this->provide[] = $provide;
  768. }
  769. /**
  770. * Get provide
  771. *
  772. * @return ProvideLink[]
  773. */
  774. public function getProvide()
  775. {
  776. return $this->provide;
  777. }
  778. /**
  779. * Add devRequire
  780. *
  781. * @param DevRequireLink $devRequire
  782. */
  783. public function addDevRequireLink(DevRequireLink $devRequire)
  784. {
  785. $this->devRequire[] = $devRequire;
  786. }
  787. /**
  788. * Get devRequire
  789. *
  790. * @return DevRequireLink[]
  791. */
  792. public function getDevRequire()
  793. {
  794. return $this->devRequire;
  795. }
  796. /**
  797. * Add suggest
  798. *
  799. * @param SuggestLink $suggest
  800. */
  801. public function addSuggestLink(SuggestLink $suggest)
  802. {
  803. $this->suggest[] = $suggest;
  804. }
  805. /**
  806. * Get suggest
  807. *
  808. * @return SuggestLink[]
  809. */
  810. public function getSuggest()
  811. {
  812. return $this->suggest;
  813. }
  814. /**
  815. * @return Boolean
  816. */
  817. public function hasVersionAlias()
  818. {
  819. return $this->getDevelopment() && $this->getVersionAlias();
  820. }
  821. /**
  822. * @return string
  823. */
  824. public function getVersionAlias()
  825. {
  826. $extra = $this->getExtra();
  827. if (isset($extra['branch-alias'][$this->getVersion()])) {
  828. $parser = new VersionParser;
  829. $version = $parser->normalizeBranch(str_replace('-dev', '', $extra['branch-alias'][$this->getVersion()]));
  830. return preg_replace('{(\.9{7})+}', '.x', $version);
  831. }
  832. return '';
  833. }
  834. /**
  835. * @return string
  836. */
  837. public function getRequireVersionAlias()
  838. {
  839. return str_replace('.x-dev', '.*@dev', $this->getVersionAlias());
  840. }
  841. public function __toString()
  842. {
  843. return $this->name.' '.$this->version.' ('.$this->normalizedVersion.')';
  844. }
  845. }