* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net) */ class Helpers { /** * Array of loadedHelpers, so that they won't be reloaded * * @var array Array of loaded helperNames */ protected $helpers = []; /** * Paths where Helpers can be found. * * Libraries will only be loaded if either a directory is supplied or it is in one of the helperPaths * * @var array Array of paths where helpers can be found */ protected $helperPaths = []; public function __construct() { $this->helperPaths = Core::$appDirs; } /** * Load a helper. * * Supply the name and the helper will be loaded from the supplied directory, * or from one of the helperPaths (which you can add). * * @param string $helperName Name of the helper * @param array $helperDirectories * @return bool Whether the helper was successfully loaded (true if yes) * @throws HelperException */ public function load(string $helperName, array $helperDirectories = []): bool { // Determine what directories should be checked $helperPaths = (empty($helperDirectories) ? $this->helperPaths : $helperDirectories); // Check it is already loaded if (isset($this->helpers[$helperName])) { Logger::log("Helper '".$helperName."' is already loaded. Skipping"); return false; } /** @var HelperLoadEvent $event */ try { $event = Events::fireEvent('helperLoadEvent', $helperName, $helperPaths); // @codeCoverageIgnoreStart } catch (EventException $e) { throw new HelperException("Could not load helper. helperLoadEvent failed: '" . $e->getMessage() . "''"); // @codeCoverageIgnoreEnd } // If cancelled by event, abort loading helper if ($event->isCancelled()) { Logger::log("Not loading helper. Aborted by event"); return false; } // Iterate over helperPaths and attempt to load if helper exists foreach ($event->helperPaths as $helperPath) { $file = $helperPath . DS . $event->helperName . '.php'; $subfile = $helperPath . DS . $event->helperName . DS . $event->helperName . '.php'; if (file_exists($file)) { // Load and register include_once($file); $this->helpers[$event->helperName] = true; Logger::log("Loaded helper '".$event->helperName."'"); return true; } // If php file not in main directory, check subdirectories elseif (file_exists($subfile)) { // Load and register include_once($subfile); $this->helpers[$event->helperName] = true; Logger::log("Loaded helper '".$event->helperName."''"); return true; } } throw new HelperException("Could not load helper. Helper not found.", 1); } /** * Alias for load * @see load() for more details * * @param string $helperName Name of the helper * @param array $helperPaths * @return bool Whether the helper was successfully loaded (true if yes) * @throws HelperException */ public function get($helperName, array $helperPaths = []): bool { return $this->load($helperName, $helperPaths); } /** * Set the directories. Automatically gets invoked if helperPaths are added to FuzeWorks\Configurator. * * @param array $directories */ public function setDirectories(array $directories) { $this->helperPaths = array_merge($this->helperPaths, $directories); } /** * Add a path where helpers can be found * * @param string $directory The directory * @return void */ public function addHelperPath($directory) { if (!in_array($directory, $this->helperPaths)) { $this->helperPaths[] = $directory; } } /** * Remove a path where helpers can be found * * @param string $directory The directory * @return void */ public function removeHelperPath($directory) { if (($key = array_search($directory, $this->helperPaths)) !== false) { unset($this->helperPaths[$key]); } } /** * Get a list of all current helperPaths * * @return array Array of paths where helpers can be found */ public function getHelperPaths(): array { return $this->helperPaths; } }