Package.php 7.6 KB

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