Template.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold PhpMyAdmin\Template class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Twig\CharsetsExtension;
  10. use PhpMyAdmin\Twig\CoreExtension;
  11. use PhpMyAdmin\Twig\I18nExtension;
  12. use PhpMyAdmin\Twig\IndexExtension;
  13. use PhpMyAdmin\Twig\MessageExtension;
  14. use PhpMyAdmin\Twig\PartitionExtension;
  15. use PhpMyAdmin\Twig\PhpFunctionsExtension;
  16. use PhpMyAdmin\Twig\PluginsExtension;
  17. use PhpMyAdmin\Twig\RelationExtension;
  18. use PhpMyAdmin\Twig\SanitizeExtension;
  19. use PhpMyAdmin\Twig\ServerPrivilegesExtension;
  20. use PhpMyAdmin\Twig\StorageEngineExtension;
  21. use PhpMyAdmin\Twig\TableExtension;
  22. use PhpMyAdmin\Twig\TrackerExtension;
  23. use PhpMyAdmin\Twig\TransformationsExtension;
  24. use PhpMyAdmin\Twig\UrlExtension;
  25. use PhpMyAdmin\Twig\UtilExtension;
  26. use Twig\Environment;
  27. use Twig\Loader\FilesystemLoader;
  28. /**
  29. * Class Template
  30. *
  31. * Handle front end templating
  32. *
  33. * @package PhpMyAdmin
  34. */
  35. class Template
  36. {
  37. /**
  38. * Name of the template
  39. */
  40. protected $name = null;
  41. /**
  42. * Twig environment
  43. */
  44. static protected $twig;
  45. const BASE_PATH = 'templates/';
  46. /**
  47. * Template constructor
  48. *
  49. * @param string $name Template name
  50. */
  51. protected function __construct($name)
  52. {
  53. $this->name = $name;
  54. if (is_null($this::$twig)) {
  55. $loader = new FilesystemLoader(static::BASE_PATH);
  56. $cache_dir = $GLOBALS['PMA_Config']->getTempDir('twig');
  57. /* Twig expects false when cache is not configured */
  58. if (is_null($cache_dir)) {
  59. $cache_dir = false;
  60. }
  61. $twig = new Environment($loader, array(
  62. 'auto_reload' => true,
  63. 'cache' => $cache_dir,
  64. 'debug' => false,
  65. ));
  66. $twig->addExtension(new CharsetsExtension());
  67. $twig->addExtension(new CoreExtension());
  68. $twig->addExtension(new I18nExtension());
  69. $twig->addExtension(new IndexExtension());
  70. $twig->addExtension(new MessageExtension());
  71. $twig->addExtension(new PartitionExtension());
  72. $twig->addExtension(new PhpFunctionsExtension());
  73. $twig->addExtension(new PluginsExtension());
  74. $twig->addExtension(new RelationExtension());
  75. $twig->addExtension(new SanitizeExtension());
  76. $twig->addExtension(new ServerPrivilegesExtension());
  77. $twig->addExtension(new StorageEngineExtension());
  78. $twig->addExtension(new TrackerExtension());
  79. $twig->addExtension(new TableExtension());
  80. $twig->addExtension(new TransformationsExtension());
  81. $twig->addExtension(new UrlExtension());
  82. $twig->addExtension(new UtilExtension());
  83. $this::$twig = $twig;
  84. }
  85. }
  86. /**
  87. * Template getter
  88. *
  89. * @param string $name Template name
  90. *
  91. * @return Template
  92. */
  93. public static function get($name)
  94. {
  95. return new Template($name);
  96. }
  97. /**
  98. * Render template
  99. *
  100. * @param array $data Variables to be provided to the template
  101. *
  102. * @return string
  103. */
  104. public function render(array $data = array())
  105. {
  106. try {
  107. $template = $this::$twig->load($this->name . '.twig');
  108. } catch (\RuntimeException $e) {
  109. /* Retry with disabled cache */
  110. $this::$twig->setCache(false);
  111. $template = $this::$twig->load($this->name . '.twig');
  112. /*
  113. * The trigger error is intentionally after second load
  114. * to avoid triggering error when disabling cache does not
  115. * solve it.
  116. */
  117. trigger_error(
  118. sprintf(
  119. __('Error while working with template cache: %s'),
  120. $e->getMessage()
  121. ),
  122. E_USER_WARNING
  123. );
  124. }
  125. return $template->render($data);
  126. }
  127. }