PhpEngine.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Illuminate\View\Engines;
  3. use Exception;
  4. use Throwable;
  5. use Illuminate\Contracts\View\Engine;
  6. use Symfony\Component\Debug\Exception\FatalThrowableError;
  7. class PhpEngine implements Engine
  8. {
  9. /**
  10. * Get the evaluated contents of the view.
  11. *
  12. * @param string $path
  13. * @param array $data
  14. * @return string
  15. */
  16. public function get($path, array $data = [])
  17. {
  18. return $this->evaluatePath($path, $data);
  19. }
  20. /**
  21. * Get the evaluated contents of the view at the given path.
  22. *
  23. * @param string $__path
  24. * @param array $__data
  25. * @return string
  26. */
  27. protected function evaluatePath($__path, $__data)
  28. {
  29. $obLevel = ob_get_level();
  30. ob_start();
  31. extract($__data, EXTR_SKIP);
  32. // We'll evaluate the contents of the view inside a try/catch block so we can
  33. // flush out any stray output that might get out before an error occurs or
  34. // an exception is thrown. This prevents any partial views from leaking.
  35. try {
  36. include $__path;
  37. } catch (Exception $e) {
  38. $this->handleViewException($e, $obLevel);
  39. } catch (Throwable $e) {
  40. $this->handleViewException(new FatalThrowableError($e), $obLevel);
  41. }
  42. return ltrim(ob_get_clean());
  43. }
  44. /**
  45. * Handle a view exception.
  46. *
  47. * @param \Exception $e
  48. * @param int $obLevel
  49. * @return void
  50. *
  51. * @throws \Exception
  52. */
  53. protected function handleViewException(Exception $e, $obLevel)
  54. {
  55. while (ob_get_level() > $obLevel) {
  56. ob_end_clean();
  57. }
  58. throw $e;
  59. }
  60. }