factory = Factory::getInstance(); $this->libraryPaths = Core::$appDirs; } /** * Add a library to FuzeWorks by adding an object. * * @param string $libraryName * @param object $libraryObject */ public function addLibraryObject(string $libraryName, $libraryObject) { $this->libraryObjects[strtolower($libraryName)] = $libraryObject; } /** * Add a library to FuzeWorks by adding its class name. * * @param string $libraryName * @param string $libraryClass * @throws LibraryException */ public function addLibraryClass(string $libraryName, string $libraryClass) { if (!class_exists($libraryClass, false)) throw new LibraryException("Could not add library class. '" . $libraryClass . "' could not be loaded.", 1); $this->libraryClasses[strtolower($libraryName)] = $libraryClass; } /** * Retrieve a library. * * Loads a library from one of the following sources in the following order: * - From the loaded libraries * - From the known libraryClasses * - From the provided alternate library directory * - From the earlier provided libraryPaths * * @param string $libraryName * @param array $parameters * @param array $altDirectories * @return object * @throws LibraryException */ public function get(string $libraryName, array $parameters = [], array $altDirectories = []) { // Test for empty string if (empty($libraryName)) { throw new LibraryException("Could not load library. No name provided", 1); } // Test if the library already exists $libraryNameLowerCase = strtolower($libraryName); $libraryClassname = '\Application\Library\\' . ucfirst($libraryName); $libraryFilename = ucfirst($libraryName); if (isset($this->libraryObjects[$libraryNameLowerCase])) return $this->libraryObjects[$libraryNameLowerCase]; // Library not found. First test if the libraryClass exists if (isset($this->libraryClasses[$libraryNameLowerCase])) return $this->initLibrary($libraryName, $this->libraryClasses[$libraryNameLowerCase], $parameters); // Try and load from the alternate directory if provided $paths = $this->libraryPaths; if (!empty($altDirectories)) $paths = $altDirectories; // Try and find the library in the libraryPaths foreach ($paths as $path) { // First look if a .php file exists in the libraryPath $classFile = $path . DS . $libraryFilename . '.php'; if (file_exists($classFile)) { require_once($classFile); return $this->initLibrary($libraryName, $libraryClassname); } $classFile = $path . DS . $libraryFilename . DS . $libraryFilename . '.php'; if (file_exists($classFile)) { require_once($classFile); return $this->initLibrary($libraryName, $libraryClassname); } } // Throw exception if not found throw new LibraryException("Could not load library. Library not found.", 1); } /** * Library Initializer * * Instantiates and returns a library. * Determines whether to use the parameters array or a config file * * @param string $libraryName * @param string $libraryClass * @param array $parameters * @throws LibraryException * @return object */ protected function initLibrary(string $libraryName, string $libraryClass, array $parameters = []) { // First check to see if the library is already loaded if (!class_exists($libraryClass, false)) { throw new LibraryException("Could not initiate library. Class not found", 1); } // Determine what parameters to use if (empty($parameters)) { try { $parameters = $this->factory->config->getConfig(strtolower($libraryName))->toArray(); } catch (ConfigException $e) { // No problem, just use an empty array instead $parameters = array(); } } // Load the class object $classObject = new $libraryClass($parameters); // Check if the address is already reserved, if it is, we can presume that a new instance is requested. // Otherwise this code would not be reached // Now load the class $this->libraryObjects[strtolower($libraryName)] = $classObject; $this->factory->logger->log("Loaded Library: ".$libraryName); return $this->libraryObjects[strtolower($libraryName)]; } /** * Set the directories. Automatically gets invoked if libraryPaths are added to FuzeWorks\Configurator. * * @param array $directories */ public function setDirectories(array $directories) { $this->libraryPaths = array_merge($this->libraryPaths, $directories); } /** * Add a path where libraries can be found * * @param string $directory The directory * @return void */ public function addLibraryPath($directory) { if (!in_array($directory, $this->libraryPaths)) { $this->libraryPaths[] = $directory; } } /** * Remove a path where libraries can be found * * @param string $directory The directory * @return void */ public function removeLibraryPath($directory) { if (($key = array_search($directory, $this->libraryPaths)) !== false) { unset($this->libraryPaths[$key]); } } /** * Get a list of all current libraryPaths * * @return array Array of paths where libraries can be found */ public function getLibraryPaths(): array { return $this->libraryPaths; } }