RenderTemplateCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ;
  31. }
  32. /**
  33. * @param InputInterface $input
  34. * @param OutputInterface $output
  35. */
  36. protected function execute(InputInterface $input, OutputInterface $output)
  37. {
  38. $name = $input->getOption('name');
  39. if (is_null($name)) {
  40. $output->writeln('<error>Name option is required</error>');
  41. return;
  42. }
  43. $parameters = $this->getParameters($input, $output);
  44. try {
  45. /* @var $templateService TemplateService */
  46. $templateService = $this->getContainer()->get('template.template_service');
  47. $filename = $templateService->renderTemplate($name, $parameters, $input->getOption('filename'));
  48. if (!is_null($filename)) {
  49. $output->writeln("Template successfully generated! File: <info>{$filename}</info>");
  50. if ($input->getOption('dump')) {
  51. $output->writeln(file_get_contents($filename));
  52. }
  53. } else {
  54. $output->writeln('<error>Template not found</error>');
  55. }
  56. } catch (\Exception $ex) {
  57. $output->writeln(sprintf('<error>%s</error>', $ex->getMessage()));
  58. }
  59. }
  60. /**
  61. * @param InputInterface $input
  62. * @param OutputInterface $output
  63. *
  64. * @return array
  65. */
  66. public function getParameters(InputInterface $input, OutputInterface $output)
  67. {
  68. $parameters = array();
  69. $inputParameters = $input->getOption('parameter');
  70. if (empty($inputParameters)) {
  71. $output->writeln('Enter the template parameters:');
  72. $output->writeln('Enter the name and value of parameter. e.g., <info>\'name1:value1\'</info>. Press Enter to finish');
  73. while ($param = fgets(STDIN)) {
  74. if ($param === PHP_EOL) {
  75. break;
  76. }
  77. $inputParameters[] = $param;
  78. $output->writeln('Enter the name and value of parameter. e.g., <info>\'name1:value1\'</info>. Press Enter to finish');
  79. }
  80. }
  81. foreach ($inputParameters as $param) {
  82. $pieces = explode(':', $param);
  83. if (isset($pieces[0]) && isset($pieces[1])) {
  84. $parameters[trim($pieces[0])] = trim($pieces[1]);
  85. }
  86. }
  87. return $parameters;
  88. }
  89. }