WebController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Controller;
  12. use Packagist\WebBundle\Form\Type\AddMaintainerRequestType;
  13. use Packagist\WebBundle\Form\Model\AddMaintainerRequest;
  14. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  15. use Packagist\WebBundle\Entity\Package;
  16. use Packagist\WebBundle\Entity\Version;
  17. use Packagist\WebBundle\Form\Type\PackageType;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  23. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  24. /**
  25. * @author Jordi Boggiano <j.boggiano@seld.be>
  26. */
  27. class WebController extends Controller
  28. {
  29. protected function getUser()
  30. {
  31. return $user = $this->get('security.context')->getToken()->getUser();
  32. }
  33. /**
  34. * @Template()
  35. * @Route("/", name="home")
  36. */
  37. public function indexAction()
  38. {
  39. $packages = $this->getDoctrine()
  40. ->getRepository('PackagistWebBundle:Package')
  41. ->findAll();
  42. return array('packages' => $packages, 'page' => 'home');
  43. }
  44. /**
  45. * @Template()
  46. * @Route("/submit", name="submit")
  47. */
  48. public function submitPackageAction()
  49. {
  50. $package = new Package;
  51. $package->setRepositoryProvider($this->get('packagist.repository_provider'));
  52. $form = $this->createForm(new PackageType, $package);
  53. $request = $this->getRequest();
  54. if ($request->getMethod() == 'POST') {
  55. $form->bindRequest($request);
  56. if ($form->isValid()) {
  57. try {
  58. $user = $this->getUser();
  59. $package->addMaintainers($user);
  60. $em = $this->getDoctrine()->getEntityManager();
  61. $em->persist($package);
  62. $em->flush();
  63. $this->get('session')->setFlash('success', $package->getName().' has been added to the package list, the repository will be parsed for releases in a bit.');
  64. return new RedirectResponse($this->generateUrl('home'));
  65. } catch (\Exception $e) {
  66. $this->get('logger')->crit($e->getMessage(), array('exception', $e));
  67. $this->get('session')->setFlash('error', $package->getName().' could not be saved.');
  68. }
  69. }
  70. }
  71. return array('form' => $form->createView(), 'page' => 'submit');
  72. }
  73. /**
  74. * @Route("/submit/fetch-info", name="submit.fetch_info", defaults={"_format"="json"})
  75. */
  76. public function fetchInfoAction()
  77. {
  78. $package = new Package;
  79. $package->setRepositoryProvider($this->get('packagist.repository_provider'));
  80. $form = $this->createForm(new PackageType, $package);
  81. $response = array('status' => 'error', 'reason' => 'No data posted.');
  82. $request = $this->getRequest();
  83. if ('POST' === $request->getMethod()) {
  84. $form->bindRequest($request);
  85. if ($form->isValid()) {
  86. $response = array('status' => 'success', 'name' => $package->getName());
  87. } else {
  88. $errors = array();
  89. foreach ($form->getChildren() as $child) {
  90. if ($child->hasErrors()) {
  91. foreach ($child->getErrors() as $error) {
  92. $errors[] = $error->getMessageTemplate();
  93. }
  94. }
  95. }
  96. $response = array('status' => 'error', 'reason' => $errors);
  97. }
  98. }
  99. return new Response(json_encode($response));
  100. }
  101. /**
  102. * View all packages with the specified tag.
  103. *
  104. * @Template()
  105. * @Route("/tag/{name}", name="tag")
  106. */
  107. public function tagAction($name)
  108. {
  109. $packages = $this->getDoctrine()
  110. ->getRepository('PackagistWebBundle:Package')
  111. ->findByTag($name);
  112. return array('packages' => $packages, 'tag' => $name);
  113. }
  114. /**
  115. * @Template()
  116. * @Route("/view/{name}", name="view")
  117. */
  118. public function viewAction($name)
  119. {
  120. $package = $this->getDoctrine()
  121. ->getRepository('PackagistWebBundle:Package')
  122. ->findOneByName($name);
  123. if (!$package) {
  124. throw new NotFoundHttpException('The requested package, '.$name.', was not found.');
  125. }
  126. if ($package->getMaintainers()->contains($this->getUser())) {
  127. $addMaintainerRequest = new AddMaintainerRequest;
  128. $form = $this->createForm(new AddMaintainerRequestType, $addMaintainerRequest);
  129. $request = $this->getRequest();
  130. if ('POST' === $request->getMethod()) {
  131. $form->bindRequest($request);
  132. if ($form->isValid()) {
  133. try {
  134. $em = $this->getDoctrine()->getEntityManager();
  135. $user = $addMaintainerRequest->getUser();
  136. if (empty($user)) {
  137. $this->get('session')->setFlash('error', 'The maintainer could not be found.');
  138. return array('package' => $package, 'form' => $form->createView());
  139. }
  140. $package->addMaintainers($user);
  141. $em->persist($package);
  142. $em->flush();
  143. $this->get('session')->setFlash('success', 'Maintainer added.');
  144. return new RedirectResponse($this->generateUrl('home'));
  145. } catch (\Exception $e) {
  146. $this->get('logger')->crit($e->getMessage(), array('exception', $e));
  147. $this->get('session')->setFlash('error', 'The maintainer could not be added.');
  148. }
  149. }
  150. }
  151. return array('package' => $package, 'form' => $form->createView());
  152. }
  153. return array('package' => $package);
  154. }
  155. /**
  156. * @Template()
  157. * @Route("/about", name="about")
  158. */
  159. public function aboutAction()
  160. {
  161. return array();
  162. }
  163. }