/** * Create a new config interface. * @static * * @param string|array $dsn Configuration options, may be serialized as assoc set (string) * @param array $options Other options (will be overwriten by DSN) * @return Config */ public static function with($dsn, $options = array()) { $options = extract_dsn($dsn) + (array) $options; if (get_called_class() !== __CLASS__) { $class = get_called_class(); return new $class($options); } // If driver is unknown, assume driver is filename or extension and use driver 'file' or 'dir' if (!isset(self::$drivers[$options['driver']])) { if (strpos($options['driver'], '.') === false && strpos($options['driver'], '/') === false) { $options['ext'] = $options['driver']; if (!isset($options['path']) && isset($options[0])) { $options['path'] = $options[0]; } } else { $options['path'] = $options['driver']; } $options['driver'] = isset($options['path']) && (file_exists($options['path']) ? is_dir($options['path']) : strpos($options['path'], '.') === false) ? 'dir' : 'file'; } if (!isset($options['driver'])) { throw new Exception("Unable to create Config object: No driver specified"); } if (!isset(self::$drivers[$options['driver']])) { throw new Exception("Unable to create Config object: Unknown driver '{$options['driver']}'"); } $class = self::$drivers[$options['driver']]; if (!load_class($class)) { throw new Exception("Unable to create {$class} object for driver '{$options['driver']}': Class does not exist."); } unset($options['driver']); return new $class($options); }
/** * Factory method. * * @param string|array $dsn DSN/method (string) or array(method, prop=>value, ...]) * @param array $props Values for public properties * @return Crypt */ public static function with($dsn, $props = array()) { $dsn_props = extract_dsn($dsn); $method = array_shift($dsn_props); $props = $dsn_props + $props; if (!isset(self::$drivers[$method])) { throw new Exception("Unable to encrypt: No driver found for method '{$method}'."); } $class = self::$drivers[$method]; if (!load_class($class)) { throw new Exception("Unable to encrypt: Could not load class '{$class}' specified for method '{$method}'. Check your include paths."); } return new $class($props); }
/** * Create a new config interface. * * @param string|array $dsn Configuration options, may be serialized as assoc set (string) * @param array $options Configuration options (which do not appear in DSN) * @return Cacher */ public static function with($dsn, $options = array()) { $dsn_options = is_string($dsn) ? extract_dsn($dsn) : $dsn; $options = (array) $dsn_options + (array) $options; if ($options['driver'] == 'alias') { return self::getInstance(isset($options[0]) ? $options[0] : null); } if (!isset(self::$drivers[$options['driver']])) { throw new Exception("Unable to create Cache object: Unknown driver '{$options['driver']}'"); } $class = self::$drivers[$options['driver']]; if (!load_class($class)) { throw new Exception("Unable to create {$class} object: Class does not exist."); } return new $class($options); }
/** * Create a new RPC client. * If DSN is given, information will be extracted, merged with $option and passed to the constructor of the driver, * otherwise all additional arguments are passed. * * @param string $dsn DSN or driver * @param array $options * @return RPC_Client_Handler */ static function create($dsn, $options = null) { $matches = null; if (preg_match('/^(\\w+)\\:(.*)$/', $dsn, $matches)) { $driver = $matches[1]; $options = extract_dsn($dsn) + (array) $options; } else { $driver = $dsn; $args = func_get_args(); array_shift($args); } $class = self::getDriverClass($driver); if (!$class) { throw new Exception("Unable to create RPC client: Unknown driver '{$driver}'"); } if (isset($args)) { $reflection = new ReflectionClass($class); return $reflection->newInstanceArgs($args); } return new $class($options); }
protected $chainInput; /** * Create a new Transformer. * * @param string|array $dsn Transformation options, may be serialized as assoc set (string) * @param array $options Other options (will be overwriten by DSN) * @return Transformer */ public static function with($dsn, $options = array()) { $prefix = func_num_args() > 2 ? func_get_arg(2) . '-' : ''; $options = (is_scalar($dsn) ? extract_dsn($dsn) : (array) $dsn) + (array) $options; if (!isset($options['driver']) && !isset($options[0])) { throw new Exception("Unable to create Transform object: No driver specified"); } $driver = $prefix . (isset($options['driver']) ? $options['driver'] : $options[0]); if (!isset(self::$drivers[$driver]) && strpos($driver, '.') !== false && isset(self::$drivers[$prefix . pathinfo($driver, PATHINFO_EXTENSION)])) { if (isset($options['driver'])) { $options[0] = $options['driver']; } $driver = $prefix . pathinfo($driver, PATHINFO_EXTENSION); } if (!isset(self::$drivers[$driver])) { throw new Exception("Unable to create Transform object: Unknown driver '{$driver}'"); } $class = self::$drivers[$driver]; if (!load_class($class)) { throw new Exception("Unable to create {$class} object: Class does not exist."); } return new $class($options);
/** * Open a new connection to MySQL database server. * @static * * @param string $host Hostname, hostname:port or DSN * @param string $username * @param string $password * @param string $dbname * @param string $port * @param string $socket * @param array $settings Additional settings * @return DB_MySQL */ public function connect($host = null, $user = null, $password = null, $database = null, $port = null, $unix_socket = null, array $settings = array()) { if (isset($this) && $this instanceof self) { throw new Exception("DB_MySQL instance is already created."); } // Aliases $hostname =& $host; $db =& $database; $dbname =& $database; $username =& $user; $pwd =& $password; if (is_array($host)) { $dsn_settings = $host + DB::$defaultOptions; if (isset($dsn_settings['dsn'])) { $dsn_settings = extract_dsn($dsn_settings['dsn']) + $dsn_settings; unset($dsn_settings['dsn']); } $host = null; extract($dsn_settings, EXTR_IF_EXISTS); } elseif (strpos($host, '=') !== false) { $dsn = $host; $host = null; $dsn_settings = extract_dsn($dsn) + DB::$defaultOptions; extract($dsn_settings, EXTR_IF_EXISTS); } else { $dsn_settings = DB::$defaultOptions; } $matches = null; if (preg_match('/^(\\w+):(\\d+)$/', $host, $matches)) { list(, $host, $port) = $matches; } $class = self::$classes['native']; $native = new $class($host, $user, $password, $database, $port, $unix_socket); if (!$native) { throw new DB_Exception("Connecting to mysql database failed: " . \mysqli::connect_error()); } $settings = compact('host', 'user', 'password', 'database', 'port', 'unix_socket') + $dsn_settings + $settings; return new self($native, $settings); }
/** * Store AUTH info to session data. */ protected function storeInfo() { $this->info = null; if ($this->user) { $this->info['uid'] = $this->user->getId(); $this->info['checksum'] = $this->checksum(); } $matches = null; if (is_string($this->store)) { $this->store = extract_dsn($this->store); } switch ($this->store['driver']) { case 'none': break; case 'session': session_start(); $_SESSION['AUTH'] = $this->info; break; case 'cookie': $cookie_params = $this->store + session_get_cookie_params(); if (!isset($this->info)) { $this->info = array(); foreach (array_keys($_COOKIE) as $key) { if (!preg_match('/^AUTH_(.*)$/i', $key, $matches)) { continue; } setcookie($key, null, 1, $cookie_params['path'], $cookie_params['domain'], $cookie_params['secure'], $cookie_params['httponly']); unset($_COOKIE[$key]); } } else { foreach (array_combine_assoc($this->info, 'AUTH', '_') as $key => $value) { setcookie($key, $value, ($cookie_params['lifetime'] <= 1 ? 0 : time()) + $cookie_params['lifetime'], $cookie_params['path'], $cookie_params['domain'], $cookie_params['secure'], $cookie_params['httponly']); $_COOKIE[$key] = $value; } } break; case 'request': HTTP::addUrlRewriteVar('AUTH', $this->info); $_REQUEST['AUTH'] = $this->info; break; case 'env': putenv('AUTH=' . $this->info ? escapeshellarg(implode_assoc(';', $this->info)) : null); break; default: throw new Exception("Invalid option '{$this->store['driver']}' specified for storing info."); } }
/** * Extract the connection parameters from a DSN string. * Returns array(driver, filters, props) * * @param string|array $dsn * @return array */ public static function extractDSN($dsn) { $args = array(); $filters = array(); $props = array(); $matches = null; // Extract DSN if (!is_string($dsn)) { $props = $dsn; $driver = strtolower(array_shift($props)); } elseif (strpos($dsn, '+') !== false && preg_match_all('/((?:\\"(?:[^\\"\\\\]++|\\\\.)++\\")|(?:\'(?:[^\'\\\\]++|\\\\.)++\')|[^\\+\\"\']++)++/', $dsn, $matches) >= 2) { $a = null; $driver = 'container'; $props = $matches[0]; $filters = null; foreach ($props as $i => $prop) { if (preg_match('/^\\s*(filter\\s*(?:\\[("(?:\\\\"|[^"])*")|(\'(?:\\\\\'|[^\'])*\'|[^\\]]+)\\]\\s*)?)=(.*)$/', $prop, $filters)) { parse_str($filters[1] . '=' . unquote(trim($filters[2])), $a); $filters = array_replace_recursive($filters, $a); unset($props[$i]); } } } else { $props = extract_dsn($dsn); $driver = strtolower(array_shift($props)); } // Get filters and properties from arguments if (isset($args['filter'])) { $filters = $args['filter']; unset($args['filter']); if (!is_array($filters)) { $filters = split_set(',', $filters); } } return array($driver, $filters, $props); }
/** * Factory; Create a new router. * * @param string|array $dsn Configuration options, may be serialized as assoc set (string) * @param array $options Configuration options (which do not appear in DSN) * @return Router */ public static function with($dsn, $options = array()) { if (get_called_class() == __CLASS__) { $dsn_options = is_string($dsn) ? extract_dsn($dsn) : $dsn; $options = (array) $dsn_options + (is_array($options) || $options instanceof \ArrayObject ? $options : array($options)); if (load_class('Q\\Config')) { $cfg_options = Config::i()->get('route' . (empty($options['driver']) ? '' : '.' . $options['driver'])); if ($cfg_options) { if (!is_array($cfg_options) && !$options instanceof \ArrayObject) { $cfg_options = split_set(';', $cfg_options); } $options += $cfg_options; if (isset($cfg_options['driver'])) { $options['driver'] = $cfg_options['driver']; } } } if (empty($options['driver'])) { throw new Exception("Unable to create Route object: No driver specified."); } if (!isset(self::$drivers[$options['driver']])) { throw new Exception("Unable to create Route object: Unknown driver '{$options['driver']}'"); } $class = self::$drivers[$options['driver']]; if (!load_class($class)) { throw new Exception("Unable to create {$class} object: Class does not exist."); } } else { $options = (is_array($dsn) ? $dsn : array($dsn)) + $options; $class = get_called_class(); } return new $class($options); }