Version.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 Symfony\Component\Validator\Constraints as Assert;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Composer\Package\Version\VersionParser;
  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
  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")
  156. */
  157. private $updatedAt;
  158. /**
  159. * @ORM\Column(type="datetime", nullable=true)
  160. */
  161. private $releasedAt;
  162. public function __construct()
  163. {
  164. $this->tags = new ArrayCollection();
  165. $this->require = new ArrayCollection();
  166. $this->replace = new ArrayCollection();
  167. $this->conflict = new ArrayCollection();
  168. $this->provide = new ArrayCollection();
  169. $this->devRequire = new ArrayCollection();
  170. $this->suggest = new ArrayCollection();
  171. $this->authors = new ArrayCollection();
  172. $this->createdAt = new \DateTime;
  173. $this->updatedAt = new \DateTime;
  174. }
  175. public function toArray()
  176. {
  177. $tags = array();
  178. foreach ($this->getTags() as $tag) {
  179. /** @var $tag Tag */
  180. $tags[] = $tag->getName();
  181. }
  182. $authors = array();
  183. foreach ($this->getAuthors() as $author) {
  184. /** @var $author Author */
  185. $authors[] = $author->toArray();
  186. }
  187. $data = array(
  188. 'name' => $this->getName(),
  189. 'description' => (string) $this->getDescription(),
  190. 'keywords' => $tags,
  191. 'homepage' => (string) $this->getHomepage(),
  192. 'version' => $this->getVersion(),
  193. 'version_normalized' => $this->getNormalizedVersion(),
  194. 'license' => $this->getLicense(),
  195. 'authors' => $authors,
  196. 'source' => $this->getSource(),
  197. 'dist' => $this->getDist(),
  198. 'type' => $this->getType(),
  199. );
  200. if ($this->getReleasedAt()) {
  201. $data['time'] = $this->getReleasedAt()->format('Y-m-d\TH:i:sP');
  202. }
  203. if ($this->getAutoload()) {
  204. $data['autoload'] = $this->getAutoload();
  205. }
  206. if ($this->getExtra()) {
  207. $data['extra'] = $this->getExtra();
  208. }
  209. if ($this->getTargetDir()) {
  210. $data['target-dir'] = $this->getTargetDir();
  211. }
  212. if ($this->getIncludePaths()) {
  213. $data['include-path'] = $this->getIncludePaths();
  214. }
  215. if ($this->getBinaries()) {
  216. $data['bin'] = $this->getBinaries();
  217. }
  218. $supportedLinkTypes = array(
  219. 'require' => 'require',
  220. 'devRequire' => 'require-dev',
  221. 'suggest' => 'suggest',
  222. 'conflict' => 'conflict',
  223. 'provide' => 'provide',
  224. 'replace' => 'replace',
  225. );
  226. foreach ($supportedLinkTypes as $method => $linkType) {
  227. foreach ($this->{'get'.$method}() as $link) {
  228. $link = $link->toArray();
  229. $data[$linkType][key($link)] = current($link);
  230. }
  231. }
  232. return $data;
  233. }
  234. public function equals(Version $version)
  235. {
  236. return strtolower($version->getName()) === strtolower($this->getName())
  237. && strtolower($version->getNormalizedVersion()) === strtolower($this->getNormalizedVersion());
  238. }
  239. /**
  240. * Get id
  241. *
  242. * @return string $id
  243. */
  244. public function getId()
  245. {
  246. return $this->id;
  247. }
  248. /**
  249. * Set name
  250. *
  251. * @param string $name
  252. */
  253. public function setName($name)
  254. {
  255. $this->name = $name;
  256. }
  257. /**
  258. * Get name
  259. *
  260. * @return string $name
  261. */
  262. public function getName()
  263. {
  264. return $this->name;
  265. }
  266. public function getNames()
  267. {
  268. $names = array(
  269. strtolower($this->name) => true
  270. );
  271. foreach ($this->getReplace() as $link) {
  272. $names[strtolower($link->getPackageName())] = true;
  273. }
  274. foreach ($this->getProvide() as $link) {
  275. $names[strtolower($link->getPackageName())] = true;
  276. }
  277. return array_keys($names);
  278. }
  279. /**
  280. * Set description
  281. *
  282. * @param string $description
  283. */
  284. public function setDescription($description)
  285. {
  286. $this->description = $description;
  287. }
  288. /**
  289. * Get description
  290. *
  291. * @return string $description
  292. */
  293. public function getDescription()
  294. {
  295. return $this->description;
  296. }
  297. /**
  298. * Set homepage
  299. *
  300. * @param string $homepage
  301. */
  302. public function setHomepage($homepage)
  303. {
  304. $this->homepage = $homepage;
  305. }
  306. /**
  307. * Get homepage
  308. *
  309. * @return string $homepage
  310. */
  311. public function getHomepage()
  312. {
  313. return $this->homepage;
  314. }
  315. /**
  316. * Set version
  317. *
  318. * @param string $version
  319. */
  320. public function setVersion($version)
  321. {
  322. $this->version = $version;
  323. }
  324. /**
  325. * Get version
  326. *
  327. * @return string $version
  328. */
  329. public function getVersion()
  330. {
  331. return $this->version;
  332. }
  333. /**
  334. * Set normalizedVersion
  335. *
  336. * @param string $normalizedVersion
  337. */
  338. public function setNormalizedVersion($normalizedVersion)
  339. {
  340. $this->normalizedVersion = $normalizedVersion;
  341. }
  342. /**
  343. * Get normalizedVersion
  344. *
  345. * @return string $normalizedVersion
  346. */
  347. public function getNormalizedVersion()
  348. {
  349. return $this->normalizedVersion;
  350. }
  351. /**
  352. * Set license
  353. *
  354. * @param array $license
  355. */
  356. public function setLicense(array $license)
  357. {
  358. $this->license = json_encode($license);
  359. }
  360. /**
  361. * Get license
  362. *
  363. * @return array $license
  364. */
  365. public function getLicense()
  366. {
  367. return json_decode($this->license, true);
  368. }
  369. /**
  370. * Set source
  371. *
  372. * @param string $source
  373. */
  374. public function setSource($source)
  375. {
  376. $this->source = json_encode($source);
  377. }
  378. /**
  379. * Get source
  380. *
  381. * @return array $source
  382. */
  383. public function getSource()
  384. {
  385. return json_decode($this->source, true);
  386. }
  387. /**
  388. * Set dist
  389. *
  390. * @param string $dist
  391. */
  392. public function setDist($dist)
  393. {
  394. $this->dist = json_encode($dist);
  395. }
  396. /**
  397. * Get dist
  398. *
  399. * @return array
  400. */
  401. public function getDist()
  402. {
  403. return json_decode($this->dist, true);
  404. }
  405. /**
  406. * Set autoload
  407. *
  408. * @param string $autoload
  409. */
  410. public function setAutoload($autoload)
  411. {
  412. $this->autoload = json_encode($autoload);
  413. }
  414. /**
  415. * Get autoload
  416. *
  417. * @return array
  418. */
  419. public function getAutoload()
  420. {
  421. return json_decode($this->autoload, true);
  422. }
  423. /**
  424. * Set binaries
  425. *
  426. * @param string $binaries
  427. */
  428. public function setBinaries($binaries)
  429. {
  430. $this->binaries = json_encode($binaries);
  431. }
  432. /**
  433. * Get binaries
  434. *
  435. * @return array
  436. */
  437. public function getBinaries()
  438. {
  439. return json_decode($this->binaries, true);
  440. }
  441. /**
  442. * Set include paths.
  443. *
  444. * @param array $paths
  445. */
  446. public function setIncludePaths($paths)
  447. {
  448. $this->includePaths = $paths ? json_encode($paths) : null;
  449. }
  450. /**
  451. * Get include paths.
  452. *
  453. * @return array|null
  454. */
  455. public function getIncludePaths()
  456. {
  457. return json_decode($this->includePaths, true);
  458. }
  459. /**
  460. * Set support
  461. *
  462. * @param array $support
  463. */
  464. public function setSupport($support)
  465. {
  466. $this->support = $support ? json_encode($support) : null;
  467. }
  468. /**
  469. * Get support
  470. *
  471. * @return array|null
  472. */
  473. public function getSupport()
  474. {
  475. return json_decode($this->support, true);
  476. }
  477. /**
  478. * Set createdAt
  479. *
  480. * @param \DateTime $createdAt
  481. */
  482. public function setCreatedAt($createdAt)
  483. {
  484. $this->createdAt = $createdAt;
  485. }
  486. /**
  487. * Get createdAt
  488. *
  489. * @return \DateTime $createdAt
  490. */
  491. public function getCreatedAt()
  492. {
  493. return $this->createdAt;
  494. }
  495. /**
  496. * Set releasedAt
  497. *
  498. * @param \DateTime $releasedAt
  499. */
  500. public function setReleasedAt($releasedAt)
  501. {
  502. $this->releasedAt = $releasedAt;
  503. }
  504. /**
  505. * Get releasedAt
  506. *
  507. * @return \DateTime $releasedAt
  508. */
  509. public function getReleasedAt()
  510. {
  511. return $this->releasedAt;
  512. }
  513. /**
  514. * Set package
  515. *
  516. * @param \Packagist\WebBundle\Entity\Package $package
  517. */
  518. public function setPackage(Package $package)
  519. {
  520. $this->package = $package;
  521. }
  522. /**
  523. * Get package
  524. *
  525. * @return \Packagist\WebBundle\Entity\Package $package
  526. */
  527. public function getPackage()
  528. {
  529. return $this->package;
  530. }
  531. /**
  532. * Get tags
  533. *
  534. * @return \Doctrine\Common\Collections\Collection $tags
  535. */
  536. public function getTags()
  537. {
  538. return $this->tags;
  539. }
  540. /**
  541. * Set updatedAt
  542. *
  543. * @param \DateTime $updatedAt
  544. */
  545. public function setUpdatedAt($updatedAt)
  546. {
  547. $this->updatedAt = $updatedAt;
  548. }
  549. /**
  550. * Get updatedAt
  551. *
  552. * @return \DateTime $updatedAt
  553. */
  554. public function getUpdatedAt()
  555. {
  556. return $this->updatedAt;
  557. }
  558. /**
  559. * Get authors
  560. *
  561. * @return \Doctrine\Common\Collections\Collection
  562. */
  563. public function getAuthors()
  564. {
  565. return $this->authors;
  566. }
  567. /**
  568. * Set type
  569. *
  570. * @param string $type
  571. */
  572. public function setType($type)
  573. {
  574. $this->type = $type;
  575. }
  576. /**
  577. * Get type
  578. *
  579. * @return string
  580. */
  581. public function getType()
  582. {
  583. return $this->type;
  584. }
  585. /**
  586. * Set targetDir
  587. *
  588. * @param string $targetDir
  589. */
  590. public function setTargetDir($targetDir)
  591. {
  592. $this->targetDir = $targetDir;
  593. }
  594. /**
  595. * Get targetDir
  596. *
  597. * @return string
  598. */
  599. public function getTargetDir()
  600. {
  601. return $this->targetDir;
  602. }
  603. /**
  604. * Set extra
  605. *
  606. * @param array $extra
  607. */
  608. public function setExtra($extra)
  609. {
  610. $this->extra = $extra;
  611. }
  612. /**
  613. * Get extra
  614. *
  615. * @return array
  616. */
  617. public function getExtra()
  618. {
  619. return $this->extra;
  620. }
  621. /**
  622. * Set development
  623. *
  624. * @param Boolean $development
  625. */
  626. public function setDevelopment($development)
  627. {
  628. $this->development = $development;
  629. }
  630. /**
  631. * Get development
  632. *
  633. * @return Boolean
  634. */
  635. public function getDevelopment()
  636. {
  637. return $this->development;
  638. }
  639. /**
  640. * @return Boolean
  641. */
  642. public function isDevelopment()
  643. {
  644. return $this->getDevelopment();
  645. }
  646. /**
  647. * Add tag
  648. *
  649. * @param \Packagist\WebBundle\Entity\Tag $tag
  650. */
  651. public function addTag(Tag $tag)
  652. {
  653. $this->tags[] = $tag;
  654. }
  655. /**
  656. * Add authors
  657. *
  658. * @param \Packagist\WebBundle\Entity\Author $author
  659. */
  660. public function addAuthor(Author $author)
  661. {
  662. $this->authors[] = $author;
  663. }
  664. /**
  665. * Add require
  666. *
  667. * @param \Packagist\WebBundle\Entity\RequireLink $require
  668. */
  669. public function addRequireLink(RequireLink $require)
  670. {
  671. $this->require[] = $require;
  672. }
  673. /**
  674. * Get require
  675. *
  676. * @return \Doctrine\Common\Collections\Collection
  677. */
  678. public function getRequire()
  679. {
  680. return $this->require;
  681. }
  682. /**
  683. * Add replace
  684. *
  685. * @param \Packagist\WebBundle\Entity\ReplaceLink $replace
  686. */
  687. public function addReplaceLink(ReplaceLink $replace)
  688. {
  689. $this->replace[] = $replace;
  690. }
  691. /**
  692. * Get replace
  693. *
  694. * @return \Doctrine\Common\Collections\Collection
  695. */
  696. public function getReplace()
  697. {
  698. return $this->replace;
  699. }
  700. /**
  701. * Add conflict
  702. *
  703. * @param \Packagist\WebBundle\Entity\ConflictLink $conflict
  704. */
  705. public function addConflictLink(ConflictLink $conflict)
  706. {
  707. $this->conflict[] = $conflict;
  708. }
  709. /**
  710. * Get conflict
  711. *
  712. * @return \Doctrine\Common\Collections\Collection
  713. */
  714. public function getConflict()
  715. {
  716. return $this->conflict;
  717. }
  718. /**
  719. * Add provide
  720. *
  721. * @param \Packagist\WebBundle\Entity\ProvideLink $provide
  722. */
  723. public function addProvideLink(ProvideLink $provide)
  724. {
  725. $this->provide[] = $provide;
  726. }
  727. /**
  728. * Get provide
  729. *
  730. * @return \Doctrine\Common\Collections\Collection
  731. */
  732. public function getProvide()
  733. {
  734. return $this->provide;
  735. }
  736. /**
  737. * Add devRequire
  738. *
  739. * @param \Packagist\WebBundle\Entity\DevRequireLink $devRequire
  740. */
  741. public function addDevRequireLink(DevRequireLink $devRequire)
  742. {
  743. $this->devRequire[] = $devRequire;
  744. }
  745. /**
  746. * Get devRequire
  747. *
  748. * @return \Doctrine\Common\Collections\Collection
  749. */
  750. public function getDevRequire()
  751. {
  752. return $this->devRequire;
  753. }
  754. /**
  755. * Add suggest
  756. *
  757. * @param \Packagist\WebBundle\Entity\SuggestLink $suggest
  758. */
  759. public function addSuggestLink(SuggestLink $suggest)
  760. {
  761. $this->suggest[] = $suggest;
  762. }
  763. /**
  764. * Get suggest
  765. *
  766. * @return \Doctrine\Common\Collections\Collection
  767. */
  768. public function getSuggest()
  769. {
  770. return $this->suggest;
  771. }
  772. /**
  773. * @return Boolean
  774. */
  775. public function hasVersionAlias()
  776. {
  777. return $this->getDevelopment() && $this->getVersionAlias();
  778. }
  779. /**
  780. * @return string
  781. */
  782. public function getVersionAlias()
  783. {
  784. $extra = $this->getExtra();
  785. if (isset($extra['branch-alias'][$this->getVersion()])) {
  786. $parser = new VersionParser;
  787. $version = $parser->normalizeBranch(str_replace('-dev', '', $extra['branch-alias'][$this->getVersion()]));
  788. return preg_replace('{(\.9{7})+}', '.x', $version);
  789. }
  790. return '';
  791. }
  792. public function __toString()
  793. {
  794. return $this->name.' '.$this->version.' ('.$this->normalizedVersion.')';
  795. }
  796. }