Просмотр файла vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php

Размер файла: 2.72Kb
  1. <?php
  2.  
  3. namespace Illuminate\View\Engines;
  4.  
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\View\Compilers\CompilerInterface;
  7. use Illuminate\View\ViewException;
  8. use Throwable;
  9.  
  10. class CompilerEngine extends PhpEngine
  11. {
  12. /**
  13. * The Blade compiler instance.
  14. *
  15. * @var \Illuminate\View\Compilers\CompilerInterface
  16. */
  17. protected $compiler;
  18.  
  19. /**
  20. * A stack of the last compiled templates.
  21. *
  22. * @var array
  23. */
  24. protected $lastCompiled = [];
  25.  
  26. /**
  27. * Create a new compiler engine instance.
  28. *
  29. * @param \Illuminate\View\Compilers\CompilerInterface $compiler
  30. * @param \Illuminate\Filesystem\Filesystem|null $files
  31. * @return void
  32. */
  33. public function __construct(CompilerInterface $compiler, Filesystem $files = null)
  34. {
  35. parent::__construct($files ?: new Filesystem);
  36.  
  37. $this->compiler = $compiler;
  38. }
  39.  
  40. /**
  41. * Get the evaluated contents of the view.
  42. *
  43. * @param string $path
  44. * @param array $data
  45. * @return string
  46. */
  47. public function get($path, array $data = [])
  48. {
  49. $this->lastCompiled[] = $path;
  50.  
  51. // If this given view has expired, which means it has simply been edited since
  52. // it was last compiled, we will re-compile the views so we can evaluate a
  53. // fresh copy of the view. We'll pass the compiler the path of the view.
  54. if ($this->compiler->isExpired($path)) {
  55. $this->compiler->compile($path);
  56. }
  57.  
  58. // Once we have the path to the compiled file, we will evaluate the paths with
  59. // typical PHP just like any other templates. We also keep a stack of views
  60. // which have been rendered for right exception messages to be generated.
  61. $results = $this->evaluatePath($this->compiler->getCompiledPath($path), $data);
  62.  
  63. array_pop($this->lastCompiled);
  64.  
  65. return $results;
  66. }
  67.  
  68. /**
  69. * Handle a view exception.
  70. *
  71. * @param \Throwable $e
  72. * @param int $obLevel
  73. * @return void
  74. *
  75. * @throws \Throwable
  76. */
  77. protected function handleViewException(Throwable $e, $obLevel)
  78. {
  79. $e = new ViewException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);
  80.  
  81. parent::handleViewException($e, $obLevel);
  82. }
  83.  
  84. /**
  85. * Get the exception message for an exception.
  86. *
  87. * @param \Throwable $e
  88. * @return string
  89. */
  90. protected function getMessage(Throwable $e)
  91. {
  92. return $e->getMessage().' (View: '.realpath(last($this->lastCompiled)).')';
  93. }
  94.  
  95. /**
  96. * Get the compiler implementation.
  97. *
  98. * @return \Illuminate\View\Compilers\CompilerInterface
  99. */
  100. public function getCompiler()
  101. {
  102. return $this->compiler;
  103. }
  104. }