EngineResolver.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Illuminate\View\Engines;
  3. use Closure;
  4. use InvalidArgumentException;
  5. class EngineResolver
  6. {
  7. /**
  8. * The array of engine resolvers.
  9. *
  10. * @var array
  11. */
  12. protected $resolvers = [];
  13. /**
  14. * The resolved engine instances.
  15. *
  16. * @var array
  17. */
  18. protected $resolved = [];
  19. /**
  20. * Register a new engine resolver.
  21. *
  22. * The engine string typically corresponds to a file extension.
  23. *
  24. * @param string $engine
  25. * @param \Closure $resolver
  26. * @return void
  27. */
  28. public function register($engine, Closure $resolver)
  29. {
  30. unset($this->resolved[$engine]);
  31. $this->resolvers[$engine] = $resolver;
  32. }
  33. /**
  34. * Resolve an engine instance by name.
  35. *
  36. * @param string $engine
  37. * @return \Illuminate\Contracts\View\Engine
  38. * @throws \InvalidArgumentException
  39. */
  40. public function resolve($engine)
  41. {
  42. if (isset($this->resolved[$engine])) {
  43. return $this->resolved[$engine];
  44. }
  45. if (isset($this->resolvers[$engine])) {
  46. return $this->resolved[$engine] = call_user_func($this->resolvers[$engine]);
  47. }
  48. throw new InvalidArgumentException("Engine [{$engine}] not found.");
  49. }
  50. }