view.php 884 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace la_restaurant_theme;
  3. require_once('renderer.php');
  4. class view {
  5. public function __construct(private string $path) {
  6. if (!$this->exists()) {
  7. throw new ErrorException('View "'.$path.'" not exists.');
  8. }
  9. }
  10. public function exists(): bool {
  11. return is_file($this->path);
  12. }
  13. private function read(): string {
  14. $file = fopen($this->path, 'r');
  15. $size = filesize($this->path);
  16. if ($file === false) {
  17. throw new ErrorException('View "'.$path.'" is not readable.');
  18. }
  19. $content = fread($file, $size);
  20. fclose($file);
  21. return $content;
  22. }
  23. public function rendering(): renderer {
  24. static $content = null;
  25. if ($content === null) {
  26. $content = $this->read();
  27. }
  28. return new renderer($content);
  29. }
  30. }