RenderTemplateCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace TemplateBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use TemplateBundle\Services\TemplateService;
  8. class RenderTemplateCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('template:render')
  14. ->setDescription('Render template content')
  15. ->setHelp('This command allows you to render the content of a template')
  16. ->addOption('name', null, InputOption::VALUE_REQUIRED, 'Template name')
  17. ->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'Template output file')
  18. ->addOption(
  19. 'parameter',
  20. null,
  21. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  22. 'Array with template parameters. e.g., --parameter=key1:value1 --parameter=key2:value2'
  23. )
  24. ->addOption(
  25. 'dump',
  26. 'd',
  27. InputOption::VALUE_NONE,
  28. 'Dump template content'
  29. )
  30. ->addOption('engine', null, InputOption::VALUE_OPTIONAL, 'Render engine. e.g. twig|dwoo', 'twig')
  31. ;
  32. }
  33. /**
  34. * @param InputInterface $input
  35. * @param OutputInterface $output
  36. */
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $name = $input->getOption('name');
  40. if (is_null($name)) {
  41. $output->writeln('<error>Name option is required</error>');
  42. return;
  43. }
  44. $parameters = $this->getParameters($input, $output);
  45. try {
  46. /* @var $templateService TemplateService */
  47. $templateService = $this->getContainer()->get('template.template_service');
  48. $filename = $templateService->renderTemplate($name, $parameters, $input->getOption('filename'), $input->getOption('engine'));
  49. if (!is_null($filename)) {
  50. $output->writeln("Template successfully generated! File: <info>{$filename}</info>");
  51. if ($input->getOption('dump')) {
  52. $output->writeln(file_get_contents($filename));
  53. }
  54. } else {
  55. $output->writeln('<error>Template not found</error>');
  56. }
  57. } catch (\Exception $ex) {
  58. $output->writeln(sprintf('<error>%s</error>', $ex->getMessage()));
  59. }
  60. }
  61. /**
  62. * @param InputInterface $input
  63. * @param OutputInterface $output
  64. *
  65. * @return array
  66. */
  67. public function getParameters(InputInterface $input, OutputInterface $output)
  68. {
  69. $parameters = array();
  70. $inputParameters = $input->getOption('parameter');
  71. if (empty($inputParameters)) {
  72. $output->writeln('Enter the template parameters:');
  73. $output->writeln('Enter the name and value of parameter. e.g., <info>\'name1:value1\'</info>. Press Enter to finish');
  74. while ($param = fgets(STDIN)) {
  75. if ($param === PHP_EOL) {
  76. break;
  77. }
  78. $inputParameters[] = $param;
  79. $output->writeln('Enter the name and value of parameter. e.g., <info>\'name1:value1\'</info>. Press Enter to finish');
  80. }
  81. }
  82. foreach ($inputParameters as $param) {
  83. $pieces = explode(':', $param, 2);
  84. if (isset($pieces[0]) && isset($pieces[1])) {
  85. $parameters[trim($pieces[0])] = trim($pieces[1]);
  86. }
  87. }
  88. return $parameters;
  89. }
  90. }