Package.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 Symfony\Component\Validator\ExecutionContext;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Composer\IO\NullIO;
  17. use Composer\Repository\VcsRepository;
  18. use Composer\Repository\RepositoryManager;
  19. /**
  20. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
  21. * @ORM\Table(
  22. * name="package",
  23. * uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})},
  24. * indexes={
  25. * @ORM\Index(name="indexed_idx",columns={"indexedAt"}),
  26. * @ORM\Index(name="crawled_idx",columns={"crawledAt"}),
  27. * @ORM\Index(name="dumped_idx",columns={"dumpedAt"})
  28. * }
  29. * )
  30. * @Assert\Callback(methods={"isPackageUnique","isRepositoryValid"})
  31. * @author Jordi Boggiano <j.boggiano@seld.be>
  32. */
  33. class Package
  34. {
  35. /**
  36. * @ORM\Id
  37. * @ORM\Column(type="integer")
  38. * @ORM\GeneratedValue(strategy="AUTO")
  39. */
  40. private $id;
  41. /**
  42. * Unique package name
  43. *
  44. * @ORM\Column()
  45. */
  46. private $name;
  47. /**
  48. * @ORM\Column(nullable=true)
  49. */
  50. private $type;
  51. /**
  52. * @ORM\Column(type="text", nullable=true)
  53. */
  54. private $description;
  55. /**
  56. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
  57. */
  58. private $versions;
  59. /**
  60. * @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
  61. * @ORM\JoinTable(name="maintainers_packages")
  62. */
  63. private $maintainers;
  64. /**
  65. * @ORM\Column()
  66. * @Assert\NotBlank()
  67. */
  68. private $repository;
  69. // dist-tags / rel or runtime?
  70. /**
  71. * @ORM\Column(type="datetime")
  72. */
  73. private $createdAt;
  74. /**
  75. * @ORM\Column(type="datetime", nullable=true)
  76. */
  77. private $updatedAt;
  78. /**
  79. * @ORM\Column(type="datetime", nullable=true)
  80. */
  81. private $crawledAt;
  82. /**
  83. * @ORM\Column(type="datetime", nullable=true)
  84. */
  85. private $indexedAt;
  86. /**
  87. * @ORM\Column(type="datetime", nullable=true)
  88. */
  89. private $dumpedAt;
  90. /**
  91. * @ORM\Column(type="boolean")
  92. */
  93. private $autoUpdated = false;
  94. private $entityRepository;
  95. private $repositoryClass;
  96. public function __construct()
  97. {
  98. $this->versions = new ArrayCollection();
  99. $this->createdAt = new \DateTime;
  100. }
  101. public function toArray()
  102. {
  103. $versions = array();
  104. foreach ($this->getVersions() as $version) {
  105. $versions[$version->getVersion()] = $version->toArray();
  106. }
  107. $maintainers = array();
  108. foreach ($this->getMaintainers() as $maintainer) {
  109. $maintainers[] = $maintainer->toArray();
  110. }
  111. $data = array(
  112. 'name' => $this->getName(),
  113. 'description' => $this->getDescription(),
  114. 'maintainers' => $maintainers,
  115. 'versions' => $versions,
  116. 'type' => $this->getType(),
  117. 'repository' => $this->getRepository()
  118. );
  119. return $data;
  120. }
  121. public function isRepositoryValid(ExecutionContext $context)
  122. {
  123. $propertyPath = $context->getPropertyPath() . '.repository';
  124. $context->setPropertyPath($propertyPath);
  125. $repo = $this->repositoryClass;
  126. if (!$repo) {
  127. if (preg_match('{//.+@}', $this->repository)) {
  128. $context->addViolation('URLs with user@host are not supported, use a read-only public URL', array(), null);
  129. } else {
  130. $context->addViolation('No valid/supported repository was found at the given URL', array(), null);
  131. }
  132. return;
  133. }
  134. try {
  135. $information = $repo->getComposerInformation($repo->getRootIdentifier());
  136. if (!isset($information['name']) || !$information['name']) {
  137. $context->addViolation('The package name was not found in the composer.json, make sure there is a name present.', array(), null);
  138. return;
  139. }
  140. if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}i', $information['name'])) {
  141. $context->addViolation('The package name '.$information['name'].' is invalid, it should have a vendor name, a forward slash, and a package name, matching <em>[a-z0-9_.-]+/[a-z0-9_.-]+</em>.', array(), null);
  142. return;
  143. }
  144. } catch (\Exception $e) {
  145. $context->addViolation('We had problems parsing your composer.json file, the parser reports: '.$e->getMessage(), array(), null);
  146. }
  147. }
  148. public function setEntityRepository($repository)
  149. {
  150. $this->entityRepository = $repository;
  151. }
  152. public function isPackageUnique(ExecutionContext $context)
  153. {
  154. try {
  155. if ($this->entityRepository->findOneByName($this->name)) {
  156. $propertyPath = $context->getPropertyPath() . '.repository';
  157. $context->setPropertyPath($propertyPath);
  158. $context->addViolation('A package with the name '.$this->name.' already exists.', array(), null);
  159. }
  160. } catch (\Doctrine\ORM\NoResultException $e) {}
  161. }
  162. /**
  163. * Get id
  164. *
  165. * @return string
  166. */
  167. public function getId()
  168. {
  169. return $this->id;
  170. }
  171. /**
  172. * Set name
  173. *
  174. * @param string $name
  175. */
  176. public function setName($name)
  177. {
  178. $this->name = $name;
  179. }
  180. /**
  181. * Get name
  182. *
  183. * @return string
  184. */
  185. public function getName()
  186. {
  187. return $this->name;
  188. }
  189. /**
  190. * Get vendor prefix
  191. *
  192. * @return string
  193. */
  194. public function getVendor()
  195. {
  196. return preg_replace('{/.*$}', '', $this->name);
  197. }
  198. /**
  199. * Get package name without vendor
  200. *
  201. * @return string
  202. */
  203. public function getPackageName()
  204. {
  205. return preg_replace('{^[^/]*/}', '', $this->name);
  206. }
  207. /**
  208. * Set description
  209. *
  210. * @param string $description
  211. */
  212. public function setDescription($description)
  213. {
  214. $this->description = $description;
  215. }
  216. /**
  217. * Get description
  218. *
  219. * @return string
  220. */
  221. public function getDescription()
  222. {
  223. return $this->description;
  224. }
  225. /**
  226. * Set createdAt
  227. *
  228. * @param \DateTime $createdAt
  229. */
  230. public function setCreatedAt($createdAt)
  231. {
  232. $this->createdAt = $createdAt;
  233. }
  234. /**
  235. * Get createdAt
  236. *
  237. * @return \DateTime
  238. */
  239. public function getCreatedAt()
  240. {
  241. return $this->createdAt;
  242. }
  243. /**
  244. * Set repository
  245. *
  246. * @param string $repository
  247. */
  248. public function setRepository($repository)
  249. {
  250. $this->repository = $repository;
  251. // avoid user@host URLs
  252. if (preg_match('{//.+@}', $repository)) {
  253. return;
  254. }
  255. try {
  256. $repository = new VcsRepository(array('url' => $repository), new NullIO());
  257. $repo = $this->repositoryClass = $repository->getDriver();
  258. if (!$repo) {
  259. return;
  260. }
  261. $information = $repo->getComposerInformation($repo->getRootIdentifier());
  262. $this->setName($information['name']);
  263. } catch (\Exception $e) {
  264. }
  265. }
  266. /**
  267. * Get repository
  268. *
  269. * @return string $repository
  270. */
  271. public function getRepository()
  272. {
  273. return $this->repository;
  274. }
  275. /**
  276. * Add versions
  277. *
  278. * @param \Packagist\WebBundle\Entity\Version $versions
  279. */
  280. public function addVersions(Version $versions)
  281. {
  282. $this->versions[] = $versions;
  283. }
  284. /**
  285. * Get versions
  286. *
  287. * @return \Doctrine\Common\Collections\Collection
  288. */
  289. public function getVersions()
  290. {
  291. return $this->versions;
  292. }
  293. /**
  294. * Set updatedAt
  295. *
  296. * @param \DateTime $updatedAt
  297. */
  298. public function setUpdatedAt($updatedAt)
  299. {
  300. $this->updatedAt = $updatedAt;
  301. }
  302. /**
  303. * Get updatedAt
  304. *
  305. * @return \DateTime
  306. */
  307. public function getUpdatedAt()
  308. {
  309. return $this->updatedAt;
  310. }
  311. /**
  312. * Set crawledAt
  313. *
  314. * @param \DateTime $crawledAt
  315. */
  316. public function setCrawledAt($crawledAt)
  317. {
  318. $this->crawledAt = $crawledAt;
  319. }
  320. /**
  321. * Get crawledAt
  322. *
  323. * @return \DateTime
  324. */
  325. public function getCrawledAt()
  326. {
  327. return $this->crawledAt;
  328. }
  329. /**
  330. * Set indexedAt
  331. *
  332. * @param \DateTime $indexedAt
  333. */
  334. public function setIndexedAt($indexedAt)
  335. {
  336. $this->indexedAt = $indexedAt;
  337. }
  338. /**
  339. * Get indexedAt
  340. *
  341. * @return \DateTime
  342. */
  343. public function getIndexedAt()
  344. {
  345. return $this->indexedAt;
  346. }
  347. /**
  348. * Set dumpedAt
  349. *
  350. * @param \DateTime $dumpedAt
  351. */
  352. public function setDumpedAt($dumpedAt)
  353. {
  354. $this->dumpedAt = $dumpedAt;
  355. }
  356. /**
  357. * Get dumpedAt
  358. *
  359. * @return \DateTime
  360. */
  361. public function getDumpedAt()
  362. {
  363. return $this->dumpedAt;
  364. }
  365. /**
  366. * Add maintainers
  367. *
  368. * @param \Packagist\WebBundle\Entity\User $maintainer
  369. */
  370. public function addMaintainer(User $maintainer)
  371. {
  372. $this->maintainers[] = $maintainer;
  373. }
  374. /**
  375. * Get maintainers
  376. *
  377. * @return \Doctrine\Common\Collections\Collection
  378. */
  379. public function getMaintainers()
  380. {
  381. return $this->maintainers;
  382. }
  383. /**
  384. * Set type
  385. *
  386. * @param string $type
  387. */
  388. public function setType($type)
  389. {
  390. $this->type = $type;
  391. }
  392. /**
  393. * Get type
  394. *
  395. * @return string
  396. */
  397. public function getType()
  398. {
  399. return $this->type;
  400. }
  401. /**
  402. * Set autoUpdated
  403. *
  404. * @param Boolean $autoUpdated
  405. */
  406. public function setAutoUpdated($autoUpdated)
  407. {
  408. $this->autoUpdated = $autoUpdated;
  409. }
  410. /**
  411. * Get autoUpdated
  412. *
  413. * @return Boolean
  414. */
  415. public function isAutoUpdated()
  416. {
  417. return $this->autoUpdated;
  418. }
  419. }