TemplateService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $params = array('em' => $this->entityManager) + $params;
  47. if (!is_null($template)) {
  48. $content = $this->render($template, $params, $engine);
  49. var_dump($content);
  50. $filename = $this->createFile($content, $filename);
  51. } else {
  52. throw new \Exception("Template {$name} not found");
  53. }
  54. return $filename;
  55. }
  56. /**
  57. * @param Template $template
  58. * @param array $params
  59. * @param string $engine
  60. *
  61. * @return string
  62. */
  63. public function render($template, $params = array(), $engine = 'twig')
  64. {
  65. if ($engine == 'twig') {
  66. $loader = new \Twig_Loader_Array(array(
  67. $template->getName() => $template->getContent()
  68. ));
  69. $twig = new \Twig_Environment($loader, array(
  70. 'cache' => false,
  71. ));
  72. $content = $twig->render($template->getName(), $params);
  73. } elseif ($engine == 'dwoo') {
  74. $core = new \Dwoo\Core();
  75. $tpl = new \Dwoo\Template\Str($template->getContent());
  76. $data = new \Dwoo\Data();
  77. $data->setData($params);
  78. $content = $core->get($tpl, $data);
  79. } else {
  80. throw new \Exception("Invalid {$engine} engine value");
  81. }
  82. return $content;
  83. }
  84. /**
  85. * @param string $content
  86. * @param string $filename
  87. *
  88. * @return string
  89. */
  90. public function createFile($content, $filename = null)
  91. {
  92. if (is_null($filename)) {
  93. $filename = self::TEMPLATE_PATH . DIRECTORY_SEPARATOR . uniqid();
  94. }
  95. $pieces = explode(DIRECTORY_SEPARATOR, $filename);
  96. $file = array_pop($pieces);
  97. $dir = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $pieces);
  98. if (!file_exists($dir)) {
  99. mkdir($dir, 0777, true);
  100. }
  101. file_put_contents($dir . DIRECTORY_SEPARATOR . $file, $content);
  102. return $filename;
  103. }
  104. }