* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net) */ class Views { use ComponentPathsTrait; /** * Get a view. * * Supply the name and the view will be loaded from the supplied directory, * or from one of the viewPaths (which you can add). * * @param string $viewName Name of the view * @param Controller $controller * @param string $viewType The type of view to be loaded. Defaults to 'Standard' * @param array $viewPaths Alternative paths to use to load the view * @param string $namespace Alternative namespace for the view. Defaults to \Application\View * @param mixed %arguments,... Arguments to be provided to the constructor [...] * @return View * @throws NotFoundException * @throws ViewException */ public function get(string $viewName, Controller $controller, string $viewType = 'Standard', array $viewPaths = [], string $namespace = '\Application\View\\'): View { if (empty($viewName)) throw new ViewException("Could not load view. No name provided", 1); // First get the directories where the view can be located $viewPaths = (empty($viewPaths) ? $this->componentPaths : [3 => $viewPaths]); // Get arguments for constructor if (func_num_args() > 5) $arguments = array_slice(func_get_args(), 5); else $arguments = []; // Fire a viewGetEvent /** @var ViewGetEvent $event */ try { $event = Events::fireEvent('viewGetEvent', $viewName, $viewType, $viewPaths, $namespace, $controller, $arguments); } catch (Exception\EventException $e) { throw new ViewException("Could not load view. viewGetEvent threw exception: '".$e->getMessage()."''"); } // If the event is cancelled, stop loading if ($event->isCancelled()) throw new ViewException("Could not load view. View cancelled by viewGetEvent."); // And attempt to load the view return $this->loadView($event->viewName, $event->controller, $event->viewType, $event->viewPaths, $event->namespace, $event->arguments); } /** * Load and return a view. * * Supply the name and the view will be loaded from one of the supplied directories * * @param string $viewName Name of the view * @param Controller $controller * @param string $viewType Type of the view * @param array $viewPaths * @param string $namespace * @param array $arguments * @return View The View object * @throws NotFoundException * @throws ViewException */ protected function loadView(string $viewName, Controller $controller, string $viewType, array $viewPaths, string $namespace, array $arguments): View { // Now figure out the className and subdir $class = trim($viewName, '/'); if (($last_slash = strrpos($class, '/')) !== FALSE) { // Extract the path $subdir = substr($class, 0, ++$last_slash); // Get the filename from the path $class = substr($class, $last_slash); } else { $subdir = ''; } // If the class already exists, return a new instance directly $class = ucfirst($class); $className = $namespace . $class . $viewType . 'View'; if (class_exists($className, false)) { /** @var View $view */ $view = new $className(...$arguments); if (!$view instanceof View) throw new ViewException("Could not load view. Provided viewName is not instance of \FuzeWorks\View"); // Load and return $view->setController($controller); return $view; } // Search for the view file for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++) { if (!isset($viewPaths[$i])) continue; foreach ($viewPaths[$i] as $directory) { // Determine the file $file = $directory . DS . $subdir . "view." . strtolower($viewType) . "." . strtolower($class) . '.php'; // If it doesn't, try and load the file if (file_exists($file)) { include_once($file); /** @var View $view */ $view = new $className(...$arguments); if (!$view instanceof View) throw new ViewException("Could not load view. Provided viewName is not instance of \FuzeWorks\View"); // Load and return $view->setController($controller); return $view; } } } // Maybe it's in a subdirectory with the same name as the class if ($subdir === '') { return $this->loadView($class . "/" . $class, $controller, $viewType, $viewPaths, $namespace, $arguments); } throw new NotFoundException("Could not load view. View was not found", 1); } }