ConfiguratorController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\DistributionBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. /**
  14. * ConfiguratorController.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ConfiguratorController extends ContainerAware
  19. {
  20. /**
  21. * @return Response A Response instance
  22. */
  23. public function stepAction($index = 0)
  24. {
  25. $configurator = $this->container->get('sensio.distribution.webconfigurator');
  26. $step = $configurator->getStep($index);
  27. $form = $this->container->get('form.factory')->create($step->getFormType(), $step);
  28. $request = $this->container->get('request');
  29. if ('POST' === $request->getMethod()) {
  30. $form->bindRequest($request);
  31. if ($form->isValid()) {
  32. $configurator->mergeParameters($step->update($form->getData()));
  33. $configurator->write();
  34. $index++;
  35. if ($index < $configurator->getStepCount()) {
  36. return new RedirectResponse($this->container->get('router')->generate('_configurator_step', array('index' => $index)));
  37. }
  38. return new RedirectResponse($this->container->get('router')->generate('_configurator_final'));
  39. }
  40. }
  41. return $this->container->get('templating')->renderResponse($step->getTemplate(), array(
  42. 'form' => $form->createView(),
  43. 'index' => $index,
  44. 'count' => $configurator->getStepCount(),
  45. 'version' => $this->getVersion(),
  46. ));
  47. }
  48. public function checkAction()
  49. {
  50. $configurator = $this->container->get('sensio.distribution.webconfigurator');
  51. $steps = $configurator->getSteps();
  52. $majors = array();
  53. $minors = array();
  54. // Trying to get as much requirements as possible
  55. foreach ($steps as $step) {
  56. foreach ($step->checkRequirements() as $major) {
  57. $majors[] = $major;
  58. }
  59. foreach ($step->checkOptionalSettings() as $minor) {
  60. $minors[] = $minor;
  61. }
  62. }
  63. $url = $this->container->get('router')->generate('_configurator_step', array('index' => 0));
  64. if (empty($majors) && empty($minors)) {
  65. return new RedirectResponse($url);
  66. }
  67. return $this->container->get('templating')->renderResponse('SensioDistributionBundle::Configurator/check.html.twig', array(
  68. 'majors' => $majors,
  69. 'minors' => $minors,
  70. 'url' => $url,
  71. 'version' => $this->getVersion(),
  72. ));
  73. }
  74. public function finalAction()
  75. {
  76. $configurator = $this->container->get('sensio.distribution.webconfigurator');
  77. $configurator->clean();
  78. try {
  79. $welcomeUrl = $this->container->get('router')->generate('_welcome');
  80. } catch (\Exception $e) {
  81. $welcomeUrl = null;
  82. }
  83. return $this->container->get('templating')->renderResponse('SensioDistributionBundle::Configurator/final.html.twig', array(
  84. 'welcome_url' => $welcomeUrl,
  85. 'parameters' => $configurator->render(),
  86. 'ini_path' => $this->container->getParameter('kernel.root_dir').'/config/parameters.ini',
  87. 'is_writable' => $configurator->isFileWritable(),
  88. 'version' => $this->getVersion(),
  89. ));
  90. }
  91. public function getVersion()
  92. {
  93. $kernel = $this->container->get('kernel');
  94. return $kernel::VERSION;
  95. }
  96. }