isInstantiable()) { return new $nameBridge(); } return false; } /** * Sets the working directory. * * @param string $dir Path to the directory containing bridges. * @throws \LogicException if the provided path is not a valid string. * @throws \Exception if the provided path does not exist. * @throws \InvalidArgumentException if $dir is not a directory. * @return void */ public static function setWorkingDir($dir){ self::$workingDir = null; if(!is_string($dir)) { throw new \InvalidArgumentException('Working directory is not a valid string!'); } if(!file_exists($dir)) { throw new \Exception('Working directory does not exist!'); } if(!is_dir($dir)) { throw new \InvalidArgumentException('Working directory is not a directory!'); } self::$workingDir = realpath($dir) . '/'; } /** * Returns the working directory. * The working directory must be specified with {@see Bridge::setWorkingDir()}! * * @throws \LogicException if the working directory is not set. * @return string The current working directory. */ public static function getWorkingDir(){ if(is_null(self::$workingDir)) { throw new \LogicException('Working directory is not set!'); } return self::$workingDir; } /** * Returns the list of bridge names based on the working directory. * * The list is cached internally to allow for successive calls. * * @return array List of bridge names */ public static function listBridges(){ static $listBridge = array(); // Initialized on first call if(empty($listBridge)) { $dirFiles = scandir(self::getWorkingDir()); if($dirFiles !== false) { foreach($dirFiles as $fileName) { if(preg_match('/^([^.]+)Bridge\.php$/U', $fileName, $out)) { $listBridge[] = $out[1]; } } } } return $listBridge; } /** * Checks if a bridge is whitelisted. * * @param string $name Name of the bridge. * @return bool True if the bridge is whitelisted. */ public static function isWhitelisted($name){ return in_array(self::sanitizeBridgeName($name), self::getWhitelist()); } /** * Returns the whitelist. * * On first call this function reads the whitelist from {@see WHITELIST}. * * Each line in the file specifies one bridge on the whitelist. * * An empty file disables all bridges. * * If the file only only contains `*`, all bridges are whitelisted. * * Use {@see Bridge::setWhitelist()} to specify a default whitelist **before** * calling this function! The list is cached internally to allow for * successive calls. If {@see Bridge::setWhitelist()} gets called after this * function, the whitelist is **not** updated again! * * @return array Array of whitelisted bridges */ public static function getWhitelist() { static $firstCall = true; // Initialized on first call if($firstCall) { // Create initial whitelist or load from disk if (!file_exists(WHITELIST) && !empty(self::$whitelist)) { file_put_contents(WHITELIST, implode("\n", self::$whitelist)); } else { $contents = trim(file_get_contents(WHITELIST)); if($contents === '*') { // Whitelist all bridges self::$whitelist = self::listBridges(); } else { self::$whitelist = array_map('self::sanitizeBridgeName', explode("\n", $contents)); } } } return self::$whitelist; } /** * Sets the (default) whitelist. * * If this function is called **before** {@see Bridge::getWhitelist()}, the * provided whitelist will be replaced by a custom whitelist specified in * {@see WHITELIST} (if it exists). * * If this function is called **after** {@see Bridge::getWhitelist()}, the * provided whitelist is taken as is (not updated by the custom whitelist * again). * * @param array $default The whitelist as array of bridge names. * @return void */ public static function setWhitelist($default = array()) { self::$whitelist = array_map('self::sanitizeBridgeName', $default); } /** * Returns the sanitized bridge name. * * The bridge name can be specified in various ways: * * The PHP file name (i.e. `GitHubIssueBridge.php`) * * The PHP file name without file extension (i.e. `GitHubIssueBridge`) * * The bridge name (i.e. `GitHubIssue`) * * Casing is ignored (i.e. `GITHUBISSUE` and `githubissue` are the same). * * A bridge file matching the given bridge name must exist in the working * directory! * * @param string $name The bridge name * @return string|null The sanitized bridge name if the provided name is * valid, null otherwise. */ protected static function sanitizeBridgeName($name) { if(is_string($name)) { // Trim trailing '.php' if exists if(preg_match('/(.+)(?:\.php)/', $name, $matches)) { $name = $matches[1]; } // Trim trailing 'Bridge' if exists if(preg_match('/(.+)(?:Bridge)/i', $name, $matches)) { $name = $matches[1]; } // The name is valid if a corresponding bridge file is found on disk if(in_array(strtolower($name), array_map('strtolower', self::listBridges()))) { $index = array_search(strtolower($name), array_map('strtolower', self::listBridges())); return self::listBridges()[$index]; } Debug::log('Invalid bridge name specified: "' . $name . '"!'); } return null; // Bad parameter } }