No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

60 líneas
1.4KB

  1. <?php
  2. namespace Framework\View;
  3. use Closure;
  4. use Exception;
  5. use Framework\View\Engine\Engine;
  6. use Framework\View\View;
  7. class Manager
  8. {
  9. protected array $paths = [];
  10. protected array $engines = [];
  11. protected array $macros = [];
  12. public function addPath(string $path): static
  13. {
  14. array_push($this->paths, $path);
  15. return $this;
  16. }
  17. public function addEngine(string $extension, Engine $engine): static
  18. {
  19. $this->engines[$extension] = $engine;
  20. $this->engines[$extension]->setManager($this);
  21. return $this;
  22. }
  23. public function render(string $template, array $data = []): View
  24. {
  25. foreach ($this->engines as $extension => $engine) {
  26. foreach ($this->paths as $path) {
  27. $file = "{$path}/{$template}.{$extension}";
  28. if (is_file($file)) {
  29. return new View($engine, realpath($file), $data);
  30. }
  31. }
  32. }
  33. throw new Exception("Could not resolve '{$template}'");
  34. }
  35. public function addMacro(string $name, Closure $closure): static
  36. {
  37. $this->macros[$name] = $closure;
  38. return $this;
  39. }
  40. public function useMacro(string $name, ...$values)
  41. {
  42. if (isset($this->macros[$name])) {
  43. $bound = $this->macros[$name]->bindTo($this);
  44. return $bound(...$values);
  45. }
  46. throw new Exception("Macro isn't defined: '{$name}'");
  47. }
  48. }

Powered by TurnKey Linux.