CompilerEngine.php 2.6KB

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