Package.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /**
  17. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
  18. * @ORM\Table(
  19. * name="package",
  20. * uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}
  21. * )
  22. * @Assert\Callback(methods={"isRepositoryValid", "isPackageUnique"})
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class Package
  26. {
  27. /**
  28. * @ORM\Id
  29. * @ORM\Column(type="integer")
  30. * @ORM\GeneratedValue(strategy="AUTO")
  31. */
  32. private $id;
  33. /**
  34. * Unique package name
  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\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
  46. */
  47. private $versions;
  48. /**
  49. * @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
  50. * @ORM\JoinTable(name="maintainers_packages")
  51. */
  52. private $maintainers;
  53. /**
  54. * @ORM\Column()
  55. * @Assert\NotBlank()
  56. */
  57. private $repository;
  58. // dist-tags / rel or runtime?
  59. /**
  60. * @ORM\Column(type="datetime")
  61. */
  62. private $createdAt;
  63. /**
  64. * @ORM\Column(type="datetime", nullable="true")
  65. */
  66. private $updatedAt;
  67. /**
  68. * @ORM\Column(type="datetime", nullable="true")
  69. */
  70. private $crawledAt;
  71. public function __construct()
  72. {
  73. $this->versions = new ArrayCollection();
  74. $this->createdAt = new \DateTime;
  75. }
  76. public function toJson()
  77. {
  78. $versions = array();
  79. foreach ($this->getVersions() as $version) {
  80. $versions[$version->getVersion()] = $version->toArray();
  81. }
  82. $maintainers = array();
  83. foreach ($this->getMaintainers() as $maintainer) {
  84. $maintainers[] = $maintainer->toArray();
  85. }
  86. $data = array(
  87. 'name' => $this->name,
  88. 'description' => $this->description,
  89. 'dist-tags' => array(),
  90. 'maintainers' => $maintainers,
  91. 'versions' => $versions,
  92. );
  93. return json_encode($data);
  94. }
  95. public function isRepositoryValid(ExecutionContext $context)
  96. {
  97. $propertyPath = $context->getPropertyPath() . '.repository';
  98. $context->setPropertyPath($propertyPath);
  99. if (!preg_match('#^git://.+|https?://github.com/[^/]+/[^/]+(?:\.git)?$#', $this->repository)) {
  100. $context->addViolation('This is not a valid git repository url', array(), null);
  101. return;
  102. }
  103. if (!preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $this->repository, $match)) {
  104. $context->addViolation('Only GitHub repositories are supported at the moment', array(), null);
  105. // TODO handle other types of git repos, and later svn/hg too
  106. return;
  107. }
  108. // handle github repositories
  109. $owner = $match[1];
  110. $repository = $match[2];
  111. $repoData = json_decode(file_get_contents('http://github.com/api/v2/json/repos/show/'.$owner.'/'.$repository), true);
  112. if (!$repoData) {
  113. $context->addViolation('Could not fetch information from this repository (if GitHub is down, please try again later)', array(), null);
  114. return;
  115. }
  116. $masterData = json_decode(file_get_contents('https://raw.github.com/'.$owner.'/'.$repository.'/master/composer.json'), true);
  117. if ($masterData['name'] !== $this->name) {
  118. $context->addViolation('The repository\'s composer.json information does not match the given package name, '.$masterData['name'].' found.', 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. }
  198. /**
  199. * Get repository
  200. *
  201. * @return string $repository
  202. */
  203. public function getRepository()
  204. {
  205. return $this->repository;
  206. }
  207. /**
  208. * Add versions
  209. *
  210. * @param Packagist\WebBundle\Entity\Version $versions
  211. */
  212. public function addVersions(Version $versions)
  213. {
  214. $this->versions[] = $versions;
  215. }
  216. /**
  217. * Get versions
  218. *
  219. * @return string $versions
  220. */
  221. public function getVersions()
  222. {
  223. return $this->versions;
  224. }
  225. /**
  226. * Set updatedAt
  227. *
  228. * @param datetime $updatedAt
  229. */
  230. public function setUpdatedAt($updatedAt)
  231. {
  232. $this->updatedAt = $updatedAt;
  233. }
  234. /**
  235. * Get updatedAt
  236. *
  237. * @return datetime $updatedAt
  238. */
  239. public function getUpdatedAt()
  240. {
  241. return $this->updatedAt;
  242. }
  243. /**
  244. * Set crawledAt
  245. *
  246. * @param datetime $crawledAt
  247. */
  248. public function setCrawledAt($crawledAt)
  249. {
  250. $this->crawledAt = $crawledAt;
  251. }
  252. /**
  253. * Get crawledAt
  254. *
  255. * @return datetime $crawledAt
  256. */
  257. public function getCrawledAt()
  258. {
  259. return $this->crawledAt;
  260. }
  261. /**
  262. * Add maintainers
  263. *
  264. * @param Packagist\WebBundle\Entity\User $maintainers
  265. */
  266. public function addMaintainers(User $maintainers)
  267. {
  268. $this->maintainers[] = $maintainers;
  269. }
  270. /**
  271. * Get maintainers
  272. *
  273. * @return Doctrine\Common\Collections\Collection $maintainers
  274. */
  275. public function getMaintainers()
  276. {
  277. return $this->maintainers;
  278. }
  279. }