PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Mail/Markdown.php

https://bitbucket.org/rogersantosferreira/bigfeel_web
PHP | 160 lines | 70 code | 20 blank | 70 comment | 0 complexity | 904f8ae4a77392fe6dd777f1ef347a55 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. namespace Illuminate\Mail;
  3. use Parsedown;
  4. use Illuminate\Support\HtmlString;
  5. use Illuminate\Contracts\View\Factory as ViewFactory;
  6. use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
  7. class Markdown
  8. {
  9. /**
  10. * The view factory implementation.
  11. *
  12. * @var \Illuminate\Contracts\View\Factory
  13. */
  14. protected $view;
  15. /**
  16. * The current theme being used when generating emails.
  17. *
  18. * @var string
  19. */
  20. protected $theme = 'default';
  21. /**
  22. * The registered component paths.
  23. *
  24. * @var array
  25. */
  26. protected $componentPaths = [];
  27. /**
  28. * Create a new Markdown renderer instance.
  29. *
  30. * @param \Illuminate\Contracts\View\Factory $view
  31. * @param array $options
  32. * @return void
  33. */
  34. public function __construct(ViewFactory $view, array $options = [])
  35. {
  36. $this->view = $view;
  37. $this->theme = $options['theme'] ?? 'default';
  38. $this->loadComponentsFrom($options['paths'] ?? []);
  39. }
  40. /**
  41. * Render the Markdown template into HTML.
  42. *
  43. * @param string $view
  44. * @param array $data
  45. * @param \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null $inliner
  46. * @return \Illuminate\Support\HtmlString
  47. */
  48. public function render($view, array $data = [], $inliner = null)
  49. {
  50. $this->view->flushFinderCache();
  51. $contents = $this->view->replaceNamespace(
  52. 'mail', $this->htmlComponentPaths()
  53. )->make($view, $data)->render();
  54. return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
  55. $contents, $this->view->make('mail::themes.'.$this->theme)->render()
  56. ));
  57. }
  58. /**
  59. * Render the Markdown template into HTML.
  60. *
  61. * @param string $view
  62. * @param array $data
  63. * @return \Illuminate\Support\HtmlString
  64. */
  65. public function renderText($view, array $data = [])
  66. {
  67. $this->view->flushFinderCache();
  68. $contents = $this->view->replaceNamespace(
  69. 'mail', $this->markdownComponentPaths()
  70. )->make($view, $data)->render();
  71. return new HtmlString(
  72. html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $contents), ENT_QUOTES, 'UTF-8')
  73. );
  74. }
  75. /**
  76. * Parse the given Markdown text into HTML.
  77. *
  78. * @param string $text
  79. * @return \Illuminate\Support\HtmlString
  80. */
  81. public static function parse($text)
  82. {
  83. $parsedown = new Parsedown;
  84. return new HtmlString($parsedown->text($text));
  85. }
  86. /**
  87. * Get the HTML component paths.
  88. *
  89. * @return array
  90. */
  91. public function htmlComponentPaths()
  92. {
  93. return array_map(function ($path) {
  94. return $path.'/html';
  95. }, $this->componentPaths());
  96. }
  97. /**
  98. * Get the Markdown component paths.
  99. *
  100. * @return array
  101. */
  102. public function markdownComponentPaths()
  103. {
  104. return array_map(function ($path) {
  105. return $path.'/markdown';
  106. }, $this->componentPaths());
  107. }
  108. /**
  109. * Get the component paths.
  110. *
  111. * @return array
  112. */
  113. protected function componentPaths()
  114. {
  115. return array_unique(array_merge($this->componentPaths, [
  116. __DIR__.'/resources/views',
  117. ]));
  118. }
  119. /**
  120. * Register new mail component paths.
  121. *
  122. * @param array $paths
  123. * @return void
  124. */
  125. public function loadComponentsFrom(array $paths = [])
  126. {
  127. $this->componentPaths = $paths;
  128. }
  129. /**
  130. * Set the default theme to be used.
  131. *
  132. * @param string $theme
  133. * @return $this
  134. */
  135. public function theme($theme)
  136. {
  137. $this->theme = $theme;
  138. return $this;
  139. }
  140. }