Package.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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\Repository\VcsRepository;
  17. use Composer\Repository\RepositoryManager;
  18. /**
  19. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
  20. * @ORM\Table(
  21. * name="package",
  22. * uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}
  23. * )
  24. * @Assert\Callback(methods={"isPackageUnique","isRepositoryValid"})
  25. * @author Jordi Boggiano <j.boggiano@seld.be>
  26. */
  27. class Package
  28. {
  29. /**
  30. * @ORM\Id
  31. * @ORM\Column(type="integer")
  32. * @ORM\GeneratedValue(strategy="AUTO")
  33. */
  34. private $id;
  35. /**
  36. * Unique package name
  37. *
  38. * @ORM\Column()
  39. */
  40. private $name;
  41. /**
  42. * @ORM\Column(nullable=true)
  43. */
  44. private $type;
  45. /**
  46. * @ORM\Column(type="text", nullable=true)
  47. */
  48. private $description;
  49. /**
  50. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
  51. */
  52. private $versions;
  53. /**
  54. * @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
  55. * @ORM\JoinTable(name="maintainers_packages")
  56. */
  57. private $maintainers;
  58. /**
  59. * @ORM\Column()
  60. * @Assert\NotBlank()
  61. */
  62. private $repository;
  63. // dist-tags / rel or runtime?
  64. /**
  65. * @ORM\Column(type="datetime")
  66. */
  67. private $createdAt;
  68. /**
  69. * @ORM\Column(type="datetime", nullable=true)
  70. */
  71. private $updatedAt;
  72. /**
  73. * @ORM\Column(type="datetime", nullable=true)
  74. */
  75. private $crawledAt;
  76. private $entityRepository;
  77. public function __construct()
  78. {
  79. $this->versions = new ArrayCollection();
  80. $this->createdAt = new \DateTime;
  81. }
  82. public function toArray()
  83. {
  84. $versions = array();
  85. foreach ($this->getVersions() as $version) {
  86. $versions[$version->getVersion()] = $version->toArray();
  87. }
  88. $maintainers = array();
  89. foreach ($this->getMaintainers() as $maintainer) {
  90. $maintainers[] = $maintainer->toArray();
  91. }
  92. $data = array(
  93. 'name' => $this->getName(),
  94. 'description' => $this->getDescription(),
  95. 'maintainers' => $maintainers,
  96. 'versions' => $versions,
  97. 'type' => $this->getType(),
  98. );
  99. return $data;
  100. }
  101. public function isRepositoryValid(ExecutionContext $context)
  102. {
  103. $propertyPath = $context->getPropertyPath() . '.repository';
  104. $context->setPropertyPath($propertyPath);
  105. $repo = $this->repositoryClass;
  106. if (!$repo) {
  107. $context->addViolation('No valid/supported repository was found at the given URL', array(), null);
  108. return;
  109. }
  110. try {
  111. $information = $repo->getComposerInformation($repo->getRootIdentifier());
  112. if (!isset($information['name']) || !$information['name']) {
  113. $context->addViolation('The package name was not found in the composer.json, make sure there is a name present.', array(), null);
  114. return;
  115. }
  116. if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}i', $information['name'])) {
  117. $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);
  118. return;
  119. }
  120. } catch (\UnexpectedValueException $e) {
  121. $context->addViolation('We had problems parsing your composer.json file, the parser reports: '.$e->getMessage(), array(), null);
  122. }
  123. }
  124. public function setEntityRepository($repository)
  125. {
  126. $this->entityRepository = $repository;
  127. }
  128. public function isPackageUnique(ExecutionContext $context)
  129. {
  130. if ($this->entityRepository->findOneByName($this->name)) {
  131. $propertyPath = $context->getPropertyPath() . '.repository';
  132. $context->setPropertyPath($propertyPath);
  133. $context->addViolation('A package with the name '.$this->name.' already exists.', array(), null);
  134. }
  135. }
  136. /**
  137. * Get id
  138. *
  139. * @return string $id
  140. */
  141. public function getId()
  142. {
  143. return $this->id;
  144. }
  145. /**
  146. * Set name
  147. *
  148. * @param string $name
  149. */
  150. public function setName($name)
  151. {
  152. $this->name = $name;
  153. }
  154. /**
  155. * Get name
  156. *
  157. * @return string $name
  158. */
  159. public function getName()
  160. {
  161. return $this->name;
  162. }
  163. /**
  164. * Get vendor
  165. *
  166. * @return string $vendor
  167. */
  168. public function getVendor()
  169. {
  170. return preg_replace('{/.*$}', '', $this->name);
  171. }
  172. /**
  173. * Set description
  174. *
  175. * @param string $description
  176. */
  177. public function setDescription($description)
  178. {
  179. $this->description = $description;
  180. }
  181. /**
  182. * Get description
  183. *
  184. * @return text $description
  185. */
  186. public function getDescription()
  187. {
  188. return $this->description;
  189. }
  190. /**
  191. * Set createdAt
  192. *
  193. * @param datetime $createdAt
  194. */
  195. public function setCreatedAt($createdAt)
  196. {
  197. $this->createdAt = $createdAt;
  198. }
  199. /**
  200. * Get createdAt
  201. *
  202. * @return datetime $createdAt
  203. */
  204. public function getCreatedAt()
  205. {
  206. return $this->createdAt;
  207. }
  208. /**
  209. * Set repository
  210. *
  211. * @param string $repository
  212. */
  213. public function setRepository($repository)
  214. {
  215. $this->repository = $repository;
  216. $repositoryManager = new RepositoryManager;
  217. $repositoryManager->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  218. $repositoryManager->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  219. $repositoryManager->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  220. $repositoryManager->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  221. try {
  222. $repository = new VcsRepository(array('url' => $repository));
  223. $repository->setRepositoryManager($repositoryManager);
  224. $repo = $this->repositoryClass = $repository->getDriver();
  225. if (!$repo) {
  226. return;
  227. }
  228. $information = $repo->getComposerInformation($repo->getRootIdentifier());
  229. $this->setName($information['name']);
  230. } catch (\UnexpectedValueException $e) {}
  231. }
  232. /**
  233. * Get repository
  234. *
  235. * @return string $repository
  236. */
  237. public function getRepository()
  238. {
  239. return $this->repository;
  240. }
  241. /**
  242. * Add versions
  243. *
  244. * @param Packagist\WebBundle\Entity\Version $versions
  245. */
  246. public function addVersions(Version $versions)
  247. {
  248. $this->versions[] = $versions;
  249. }
  250. /**
  251. * Get versions
  252. *
  253. * @return string $versions
  254. */
  255. public function getVersions()
  256. {
  257. return $this->versions;
  258. }
  259. /**
  260. * Set updatedAt
  261. *
  262. * @param datetime $updatedAt
  263. */
  264. public function setUpdatedAt($updatedAt)
  265. {
  266. $this->updatedAt = $updatedAt;
  267. }
  268. /**
  269. * Get updatedAt
  270. *
  271. * @return datetime $updatedAt
  272. */
  273. public function getUpdatedAt()
  274. {
  275. return $this->updatedAt;
  276. }
  277. /**
  278. * Set crawledAt
  279. *
  280. * @param datetime $crawledAt
  281. */
  282. public function setCrawledAt($crawledAt)
  283. {
  284. $this->crawledAt = $crawledAt;
  285. }
  286. /**
  287. * Get crawledAt
  288. *
  289. * @return datetime $crawledAt
  290. */
  291. public function getCrawledAt()
  292. {
  293. return $this->crawledAt;
  294. }
  295. /**
  296. * Add maintainers
  297. *
  298. * @param Packagist\WebBundle\Entity\User $maintainer
  299. */
  300. public function addMaintainer(User $maintainer)
  301. {
  302. $this->maintainers[] = $maintainer;
  303. }
  304. /**
  305. * Get maintainers
  306. *
  307. * @return Doctrine\Common\Collections\Collection $maintainers
  308. */
  309. public function getMaintainers()
  310. {
  311. return $this->maintainers;
  312. }
  313. /**
  314. * Set type
  315. *
  316. * @param string $type
  317. */
  318. public function setType($type)
  319. {
  320. $this->type = $type;
  321. }
  322. /**
  323. * Get type
  324. *
  325. * @return string
  326. */
  327. public function getType()
  328. {
  329. return $this->type;
  330. }
  331. }