/src/TwigGenerator/Builder/BaseBuilder.php

https://github.com/cedriclombardot/TwigGenerator · PHP · 303 lines · 152 code · 42 blank · 109 comment · 7 complexity · 5a77d1c8379631821c853eb7c68b9ee2 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the TwigGenerator package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace TwigGenerator\Builder;
  10. /**
  11. * @author Cédric Lombardot
  12. */
  13. abstract class BaseBuilder implements BuilderInterface
  14. {
  15. /**
  16. * Default Twig file extension.
  17. */
  18. const TWIG_EXTENSION = '.php.twig';
  19. /**
  20. * @var \TwigGenerator\Builder\Generator The generator.
  21. */
  22. protected $generator;
  23. /**
  24. * @var array A list of template directories.
  25. */
  26. protected $templateDirectories = array();
  27. /**
  28. * @var string
  29. */
  30. protected $templateName;
  31. /**
  32. * @var string
  33. */
  34. protected $outputName;
  35. /**
  36. * @var Boolean
  37. */
  38. protected $mustOverwriteIfExists = false;
  39. /**
  40. * @var array
  41. */
  42. protected $twigFilters = array(
  43. 'addslashes',
  44. 'var_export',
  45. 'is_numeric',
  46. 'ucfirst',
  47. 'substr',
  48. );
  49. /**
  50. * @var array
  51. */
  52. protected $variables = array();
  53. /**
  54. * @var array
  55. */
  56. protected $twigExtensions = array(
  57. );
  58. /**
  59. * Constructor
  60. */
  61. public function __construct()
  62. {
  63. $this->templateDirectories = $this->getDefaultTemplateDirs();
  64. $this->templateName = $this->getDefaultTemplateName();
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function setGenerator(Generator $generator)
  70. {
  71. $this->generator = $generator;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function getGenerator()
  77. {
  78. return $this->generator;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function addTemplateDir($templateDir)
  84. {
  85. $this->templateDirectories[$templateDir] = $templateDir;
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function setTemplateDirs(array $templateDirs)
  91. {
  92. $this->templateDirectories = $templateDirs;
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getTemplateDirs()
  98. {
  99. return $this->templateDirectories;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function getDefaultTemplateDirs()
  105. {
  106. return array();
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function setTemplateName($templateName)
  112. {
  113. $this->templateName = $templateName;
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public function getTemplateName()
  119. {
  120. return $this->templateName;
  121. }
  122. /**
  123. * {@inheritDoc}
  124. */
  125. public function getDefaultTemplateName()
  126. {
  127. return $this->getSimpleClassName() . self::TWIG_EXTENSION;
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public function getSimpleClassName($class = null)
  133. {
  134. if (null === $class) {
  135. $class = get_class($this);
  136. }
  137. $classParts = explode('\\', $class);
  138. $simpleClassName = array_pop($classParts);
  139. return $simpleClassName;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function setOutputName($outputName)
  145. {
  146. $this->outputName = $outputName;
  147. }
  148. /**
  149. * {@inheritDoc}
  150. */
  151. public function getOutputName()
  152. {
  153. return $this->outputName;
  154. }
  155. /**
  156. * {@inheritDoc}
  157. */
  158. public function mustOverwriteIfExists()
  159. {
  160. return $this->mustOverwriteIfExists;
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public function setMustOverwriteIfExists($status = true)
  166. {
  167. $this->mustOverwriteIfExists = $status;
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. public function setVariables(array $variables)
  173. {
  174. $this->variables = $variables;
  175. }
  176. /**
  177. * {@inheritDoc}
  178. */
  179. public function setVariable($key, $value)
  180. {
  181. $this->variables[$key] = $value;
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function getVariables()
  187. {
  188. return $this->variables;
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. public function hasVariable($key)
  194. {
  195. return isset($this->variables[$key]);
  196. }
  197. /**
  198. * {@inheritDoc}
  199. */
  200. public function getVariable($key, $default = null)
  201. {
  202. return $this->hasVariable($key) ? $this->variables[$key] : $default;
  203. }
  204. /**
  205. * {@inheritDoc}
  206. */
  207. public function writeOnDisk($outputDirectory)
  208. {
  209. $path = $outputDirectory . DIRECTORY_SEPARATOR . $this->getOutputName();
  210. $dir = dirname($path);
  211. if (!is_dir($dir)) {
  212. mkdir($dir, 0777, true);
  213. }
  214. if (!file_exists($path) || (file_exists($path) && $this->mustOverwriteIfExists)) {
  215. file_put_contents($path, $this->getCode());
  216. }
  217. }
  218. /**
  219. * {@inheritDoc}
  220. */
  221. public function getCode()
  222. {
  223. $loader = new \Twig_Loader_Filesystem($this->getTemplateDirs());
  224. $twig = new \Twig_Environment($loader, array(
  225. 'autoescape' => false,
  226. 'strict_variables' => true,
  227. 'debug' => true,
  228. 'cache' => $this->getGenerator()->getTempDir(),
  229. ));
  230. $this->addTwigExtensions($twig, $loader);
  231. $this->addTwigFilters($twig);
  232. $template = $twig->loadTemplate($this->getTemplateName());
  233. $variables = $this->getVariables();
  234. $variables['builder'] = $this;
  235. return $template->render($variables);
  236. }
  237. /**
  238. * {@inheritDoc}
  239. */
  240. public function addTwigFilters(\Twig_Environment $twig)
  241. {
  242. foreach ($this->twigFilters as $twigFilter) {
  243. if (($pos = strpos($twigFilter, ':')) !== false) {
  244. $twigFilterName = substr($twigFilter, $pos + 2);
  245. } else {
  246. $twigFilterName = $twigFilter;
  247. }
  248. $twig->addFilter($twigFilterName, new \Twig_Filter_Function($twigFilter));
  249. }
  250. }
  251. /**
  252. * {@inheritDoc}
  253. */
  254. public function addTwigExtensions(\Twig_Environment $twig, \Twig_LoaderInterface $loader)
  255. {
  256. foreach ($this->twigExtensions as $twigExtensionName) {
  257. $twigExtension = new $twigExtensionName($loader);
  258. $twig->addExtension($twigExtension);
  259. }
  260. }
  261. }