Version.php 17 KB

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