TemplateService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace TemplateBundle\Services;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\EntityRepository;
  5. use TemplateBundle\Entity\Template;
  6. class TemplateService
  7. {
  8. const TEMPLATE_PATH = '/tmp/flowdat/templates';
  9. /**
  10. * @var EntityManagerInterface
  11. */
  12. private $entityManager;
  13. /**
  14. * @var EntityRepository
  15. */
  16. private $templateRepository;
  17. /**
  18. * @param EntityManagerInterface $entityManager
  19. */
  20. public function __construct(EntityManagerInterface $entityManager)
  21. {
  22. $this->entityManager = $entityManager;
  23. $this->templateRepository = $entityManager->getRepository('TemplateBundle:Template');
  24. }
  25. /**
  26. * @param string $name
  27. *
  28. * @return Template
  29. */
  30. public function getTemplateByName($name)
  31. {
  32. return $this->templateRepository->findOneByName($name);
  33. }
  34. /**
  35. * @param string $name
  36. * @param array $params
  37. * @param string $filename
  38. * @param string $engine
  39. *
  40. * @return string
  41. */
  42. public function renderTemplate($name, $params = array(), $filename = null, $engine = 'twig')
  43. {
  44. $content = null;
  45. $template = $this->getTemplateByName($name);
  46. if (!is_null($template)) {
  47. $content = $this->render($template, $params, $engine);
  48. var_dump($content);
  49. $filename = $this->createFile($content, $filename);
  50. } else {
  51. throw new \Exception("Template {$name} not found");
  52. }
  53. return $filename;
  54. }
  55. /**
  56. * @param Template $template
  57. * @param array $params
  58. * @param string $engine
  59. *
  60. * @return string
  61. */
  62. public function render($template, $params = array(), $engine = 'twig')
  63. {
  64. if ($engine == 'twig') {
  65. $loader = new \Twig_Loader_Array(array(
  66. $template->getName() => $template->getContent()
  67. ));
  68. $twig = new \Twig_Environment($loader, array(
  69. 'cache' => false,
  70. ));
  71. $content = $twig->render($template->getName(), $params);
  72. } elseif ($engine == 'dwoo') {
  73. $core = new \Dwoo\Core();
  74. $tpl = new \Dwoo\Template\Str($template->getContent());
  75. $data = new \Dwoo\Data();
  76. $data->setData($params);
  77. $content = $core->get($tpl, $data);
  78. } else {
  79. throw new \Exception("Invalid {$engine} engine value");
  80. }
  81. return $content;
  82. }
  83. /**
  84. * @param string $content
  85. * @param string $filename
  86. *
  87. * @return string
  88. */
  89. public function createFile($content, $filename = null)
  90. {
  91. if (is_null($filename)) {
  92. $filename = self::TEMPLATE_PATH . DIRECTORY_SEPARATOR . uniqid();
  93. }
  94. $pieces = explode(DIRECTORY_SEPARATOR, $filename);
  95. $file = array_pop($pieces);
  96. $dir = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $pieces);
  97. if (!file_exists($dir)) {
  98. mkdir($dir, 0777, true);
  99. }
  100. file_put_contents($dir . DIRECTORY_SEPARATOR . $file, $content);
  101. return $filename;
  102. }
  103. }