views.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace la_restaurant_theme;
  3. require_once('view.php');
  4. class views {
  5. private string $path;
  6. public function __construct(string $path) {
  7. $this->path = $this->parse_path($path);
  8. if (!$this->exists()) {
  9. throw new ErrorException(
  10. 'Views directory "'.$this->path.'" not exists.'
  11. );
  12. }
  13. }
  14. private function parse_path($path): string {
  15. if (strlen($path) === 0) {
  16. $path = './';
  17. }
  18. $size = strlen($path);
  19. $last = $path[$size - 1];
  20. if ($last !== '/' || $last !== '\\') {
  21. return $path.'/';
  22. }
  23. return $path;
  24. }
  25. public function exists(): bool {
  26. return is_dir($this->path);
  27. }
  28. private function get_file_for(string $name): string {
  29. $unix = strpos($name, '/');
  30. $dos = strpos($name, '\\');
  31. if ($unix !== false || $dos !== false) {
  32. throw new ErrorException('View "'.$name.'" not in views dir.');
  33. }
  34. return $this->path.$name;
  35. }
  36. public function get(string $name): view {
  37. return new view($this->get_file_for($name));
  38. }
  39. }