/** * Constructor. * * You can either pass an existing database connection as PDO instance or * a Doctrine DBAL Connection or a DSN string that will be used to * lazy-connect to the database when the cache is actually used. * * List of available options: * * db_table: The name of the table [default: cache_items] * * db_id_col: The column where to store the cache id [default: item_id] * * db_data_col: The column where to store the cache data [default: item_data] * * db_lifetime_col: The column where to store the lifetime [default: item_lifetime] * * db_time_col: The column where to store the timestamp [default: item_time] * * db_username: The username when lazy-connect [default: ''] * * db_password: The password when lazy-connect [default: ''] * * db_connection_options: An array of driver-specific connection options [default: array()] * * @param \PDO|Connection|string $connOrDsn A \PDO or Connection instance or DSN string or null * @param string $namespace * @param int $defaultLifetime * @param array $options An associative array of options * * @throws InvalidArgumentException When first argument is not PDO nor Connection nor string * @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION * @throws InvalidArgumentException When namespace contains invalid characters */ public function __construct($connOrDsn, $namespace = '', $defaultLifetime = 0, array $options = array()) { if (isset($namespace[0]) && preg_match('#[^-+.A-Za-z0-9]#', $namespace, $match)) { throw new InvalidArgumentException(sprintf('Namespace contains "%s" but only characters in [-+.A-Za-z0-9] are allowed.', $match[0])); } if ($connOrDsn instanceof \PDO) { if (\PDO::ERRMODE_EXCEPTION !== $connOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) { throw new InvalidArgumentException(sprintf('"%s" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION))', __CLASS__)); } $this->conn = $connOrDsn; } elseif ($connOrDsn instanceof Connection) { $this->conn = $connOrDsn; } elseif (is_string($connOrDsn)) { $this->dsn = $connOrDsn; } else { throw new InvalidArgumentException(sprintf('"%s" requires PDO or Doctrine\\DBAL\\Connection instance or DSN string as first argument, "%s" given.', __CLASS__, is_object($connOrDsn) ? get_class($connOrDsn) : gettype($connOrDsn))); } $this->table = isset($options['db_table']) ? $options['db_table'] : $this->table; $this->idCol = isset($options['db_id_col']) ? $options['db_id_col'] : $this->idCol; $this->dataCol = isset($options['db_data_col']) ? $options['db_data_col'] : $this->dataCol; $this->lifetimeCol = isset($options['db_lifetime_col']) ? $options['db_lifetime_col'] : $this->lifetimeCol; $this->timeCol = isset($options['db_time_col']) ? $options['db_time_col'] : $this->timeCol; $this->username = isset($options['db_username']) ? $options['db_username'] : $this->username; $this->password = isset($options['db_password']) ? $options['db_password'] : $this->password; $this->connectionOptions = isset($options['db_connection_options']) ? $options['db_connection_options'] : $this->connectionOptions; parent::__construct($namespace, $defaultLifetime); }