Нет описания

Renderer.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\DI\ContainerWrapper;
  5. use MailPoet\Twig;
  6. use MailPoet\Util\CdnAssetUrl;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. use MailPoetVendor\Twig\Environment as TwigEnv;
  9. use MailPoetVendor\Twig\Extension\DebugExtension;
  10. use MailPoetVendor\Twig\Lexer as TwigLexer;
  11. use MailPoetVendor\Twig\Loader\FilesystemLoader as TwigFileSystem;
  12. class Renderer {
  13. protected $cachePath;
  14. protected $cachingEnabled;
  15. protected $debuggingEnabled;
  16. protected $renderer;
  17. public $assetsManifestJs;
  18. public $assetsManifestCss;
  19. public function __construct(
  20. $cachingEnabled = false,
  21. $debuggingEnabled = false
  22. ) {
  23. $this->cachingEnabled = $cachingEnabled;
  24. $this->debuggingEnabled = $debuggingEnabled;
  25. $this->cachePath = Env::$cachePath;
  26. $fileSystem = new TwigFileSystem(Env::$viewsPath);
  27. $this->renderer = new TwigEnv(
  28. $fileSystem,
  29. [
  30. 'cache' => $this->detectCache(),
  31. 'debug' => $this->debuggingEnabled,
  32. 'auto_reload' => true,
  33. ]
  34. );
  35. $this->assetsManifestJs = $this->getAssetManifest(Env::$assetsPath . '/dist/js/manifest.json');
  36. $this->assetsManifestCss = $this->getAssetManifest(Env::$assetsPath . '/dist/css/manifest.json');
  37. $this->setupDebug();
  38. $this->setupTranslations();
  39. $this->setupFunctions();
  40. $this->setupFilters();
  41. $this->setupHandlebars();
  42. $this->setupHelpscout();
  43. $this->setupAnalytics();
  44. $this->setupGlobalVariables();
  45. $this->setupSyntax();
  46. }
  47. public function setupTranslations() {
  48. $this->renderer->addExtension(new Twig\I18n(Env::$pluginName));
  49. }
  50. public function setupFunctions() {
  51. $this->renderer->addExtension(new Twig\Functions());
  52. }
  53. public function setupFilters() {
  54. $this->renderer->addExtension(new Twig\Filters());
  55. }
  56. public function setupHandlebars() {
  57. $this->renderer->addExtension(new Twig\Handlebars());
  58. }
  59. public function setupHelpscout() {
  60. $this->renderer->addExtension(new Twig\Helpscout());
  61. }
  62. public function setupAnalytics() {
  63. $this->renderer->addExtension(new Twig\Analytics());
  64. }
  65. public function setupGlobalVariables() {
  66. $this->renderer->addExtension(new Twig\Assets([
  67. 'version' => Env::$version,
  68. 'assets_url' => Env::$assetsUrl,
  69. 'assets_manifest_js' => $this->assetsManifestJs,
  70. 'assets_manifest_css' => $this->assetsManifestCss,
  71. ], ContainerWrapper::getInstance()->get(CdnAssetUrl::class)));
  72. }
  73. public function setupSyntax() {
  74. $lexer = new TwigLexer($this->renderer, [
  75. 'tag_comment' => ['<#', '#>'],
  76. 'tag_block' => ['<%', '%>'],
  77. 'tag_variable' => ['<%=', '%>'],
  78. 'interpolation' => ['%{', '}'],
  79. ]);
  80. $this->renderer->setLexer($lexer);
  81. }
  82. public function detectCache() {
  83. return $this->cachingEnabled ? $this->cachePath : false;
  84. }
  85. public function setupDebug() {
  86. if ($this->debuggingEnabled) {
  87. $this->renderer->addExtension(new DebugExtension());
  88. }
  89. }
  90. public function render($template, $context = []) {
  91. try {
  92. return $this->renderer->render($template, $context);
  93. } catch (\RuntimeException $e) {
  94. throw new \Exception(sprintf(
  95. WPFunctions::get()->__('Failed to render template "%s". Please ensure the template cache folder "%s" exists and has write permissions. Terminated with error: "%s"'),
  96. $template,
  97. $this->cachePath,
  98. $e->getMessage()
  99. ));
  100. }
  101. }
  102. public function getAssetManifest($manifestFile) {
  103. if (is_readable($manifestFile)) {
  104. $contents = file_get_contents($manifestFile);
  105. if (is_string($contents)) {
  106. return json_decode($contents, true);
  107. }
  108. }
  109. return false;
  110. }
  111. public function getJsAsset($asset) {
  112. return (!empty($this->assetsManifestJs[$asset])) ?
  113. $this->assetsManifestJs[$asset] :
  114. $asset;
  115. }
  116. public function getCssAsset($asset) {
  117. return (!empty($this->assetsManifestCss[$asset])) ?
  118. $this->assetsManifestCss[$asset] :
  119. $asset;
  120. }
  121. }