Version.php 18 KB

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