Package.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 toJson()
  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->name,
  92. 'description' => $this->description,
  93. 'dist-tags' => array(),
  94. 'maintainers' => $maintainers,
  95. 'versions' => $versions,
  96. 'type' => $this->type,
  97. );
  98. return json_encode($data);
  99. }
  100. public function setRepositoryProvider(RepositoryProviderInterface $provider)
  101. {
  102. $this->repositoryProvider = $provider;
  103. }
  104. public function isRepositoryValid(ExecutionContext $context)
  105. {
  106. $propertyPath = $context->getPropertyPath() . '.repository';
  107. $context->setPropertyPath($propertyPath);
  108. $repo = $this->repositoryClass;
  109. if (!$repo) {
  110. $context->addViolation('No valid/supported repository was found at the given URL', array(), null);
  111. return;
  112. }
  113. try {
  114. $information = $repo->getComposerInformation($repo->getRootIdentifier());
  115. } catch (\UnexpectedValueException $e) {}
  116. // TODO use more specialized exception for repos
  117. if (!isset($information['name']) || !$information['name']) {
  118. $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);
  119. return;
  120. }
  121. }
  122. public function isPackageUnique(ExecutionContext $context)
  123. {
  124. // TODO check for uniqueness of package name
  125. }
  126. /**
  127. * Get id
  128. *
  129. * @return string $id
  130. */
  131. public function getId()
  132. {
  133. return $this->id;
  134. }
  135. /**
  136. * Set name
  137. *
  138. * @param string $name
  139. */
  140. public function setName($name)
  141. {
  142. $this->name = $name;
  143. }
  144. /**
  145. * Get name
  146. *
  147. * @return string $name
  148. */
  149. public function getName()
  150. {
  151. return $this->name;
  152. }
  153. /**
  154. * Set description
  155. *
  156. * @param text $description
  157. */
  158. public function setDescription($description)
  159. {
  160. $this->description = $description;
  161. }
  162. /**
  163. * Get description
  164. *
  165. * @return text $description
  166. */
  167. public function getDescription()
  168. {
  169. return $this->description;
  170. }
  171. /**
  172. * Set createdAt
  173. *
  174. * @param datetime $createdAt
  175. */
  176. public function setCreatedAt($createdAt)
  177. {
  178. $this->createdAt = $createdAt;
  179. }
  180. /**
  181. * Get createdAt
  182. *
  183. * @return datetime $createdAt
  184. */
  185. public function getCreatedAt()
  186. {
  187. return $this->createdAt;
  188. }
  189. /**
  190. * Set repository
  191. *
  192. * @param string $repository
  193. */
  194. public function setRepository($repository)
  195. {
  196. $this->repository = $repository;
  197. try {
  198. $this->repositoryClass = $repo = $this->repositoryProvider->getRepository($this->repository);
  199. if (!$repo) {
  200. return;
  201. }
  202. $information = $repo->getComposerInformation($repo->getRootIdentifier());
  203. $this->setName($information['name']);
  204. } catch (\UnexpectedValueException $e) {}
  205. // TODO use more specialized exception for repos
  206. }
  207. /**
  208. * Get repository
  209. *
  210. * @return string $repository
  211. */
  212. public function getRepository()
  213. {
  214. return $this->repository;
  215. }
  216. /**
  217. * Add versions
  218. *
  219. * @param Packagist\WebBundle\Entity\Version $versions
  220. */
  221. public function addVersions(Version $versions)
  222. {
  223. $this->versions[] = $versions;
  224. }
  225. /**
  226. * Get versions
  227. *
  228. * @return string $versions
  229. */
  230. public function getVersions()
  231. {
  232. return $this->versions;
  233. }
  234. /**
  235. * Set updatedAt
  236. *
  237. * @param datetime $updatedAt
  238. */
  239. public function setUpdatedAt($updatedAt)
  240. {
  241. $this->updatedAt = $updatedAt;
  242. }
  243. /**
  244. * Get updatedAt
  245. *
  246. * @return datetime $updatedAt
  247. */
  248. public function getUpdatedAt()
  249. {
  250. return $this->updatedAt;
  251. }
  252. /**
  253. * Set crawledAt
  254. *
  255. * @param datetime $crawledAt
  256. */
  257. public function setCrawledAt($crawledAt)
  258. {
  259. $this->crawledAt = $crawledAt;
  260. }
  261. /**
  262. * Get crawledAt
  263. *
  264. * @return datetime $crawledAt
  265. */
  266. public function getCrawledAt()
  267. {
  268. return $this->crawledAt;
  269. }
  270. /**
  271. * Add maintainers
  272. *
  273. * @param Packagist\WebBundle\Entity\User $maintainers
  274. */
  275. public function addMaintainers(User $maintainers)
  276. {
  277. $this->maintainers[] = $maintainers;
  278. }
  279. /**
  280. * Get maintainers
  281. *
  282. * @return Doctrine\Common\Collections\Collection $maintainers
  283. */
  284. public function getMaintainers()
  285. {
  286. return $this->maintainers;
  287. }
  288. /**
  289. * Set type
  290. *
  291. * @param text $type
  292. */
  293. public function setType($type)
  294. {
  295. $this->type = $type;
  296. }
  297. /**
  298. * Get type
  299. *
  300. * @return string
  301. */
  302. public function getType()
  303. {
  304. return $this->type;
  305. }
  306. }