Пример #1
0
 /**
  *
  * @param string $name
  * @param bool $required
  */
 public static function retrieve($name, $required = true)
 {
     $group = explode('.', $name, 2);
     $group = $group[0];
     if (!isset(self::$configuration[$group])) {
         // Load the configuration group
         $defaultFolder = dirname(__FILE__) . '/config/';
         if (self::$folder == null) {
             self::$folder = $defaultFolder;
         }
         $file = self::$folder . $group . '.php';
         $defaultFile = $defaultFolder . $group . '.php';
         if (!file_exists($file)) {
             // Try default folder
             if (!file_exists($defaultFile)) {
                 if ($required === true) {
                     throw new Exception("{$file} does not exists");
                 } else {
                     return false;
                 }
             } else {
                 $file = $defaultFile;
             }
         }
         include $file;
         if (!isset($config) || empty($config)) {
             throw new Exception("Could not find config array in {$file}");
         }
         self::$configuration[$group] = $config;
     }
     $value = self::retrieveKey(self::$configuration, $name);
     if ($required === true && ($value === NULL || empty($value))) {
         throw new Exception("Could not find {$name} in {$file}");
     }
     return $value;
 }
Пример #2
0
 /**
  * Makes a singular word plural.
  *
  * @param string $str word to pluralize
  * @param int $count
  * @return string
  */
 public static function plural($str, $count = NULL)
 {
     // Remove garbage
     $str = strtolower(trim($str));
     if (is_string($count)) {
         // Convert to integer when using a digit string
         $count = (int) $count;
     }
     // Do nothing with singular
     if ($count === 1) {
         return $str;
     }
     // Cache key name
     $key = 'plural_' . $str . $count;
     if (isset(self::$cache[$key])) {
         return self::$cache[$key];
     }
     if (self::uncountable($str)) {
         return self::$cache[$key] = $str;
     }
     if (empty(self::$irregular)) {
         // Cache irregular words
         self::$irregular = AetherDatabaseConfig::retrieve('inflector.irregular');
     }
     if (isset(self::$irregular[$str])) {
         $str = self::$irregular[$str];
     } elseif (preg_match('/[sxz]$/', $str) || preg_match('/[^aeioudgkprt]h$/', $str)) {
         $str .= 'es';
     } elseif (preg_match('/[^aeiou]y$/', $str)) {
         // Change "y" to "ies"
         $str = substr_replace($str, 'ies', -1);
     } else {
         $str .= 's';
     }
     // Set the cache and return
     return self::$cache[$key] = $str;
 }
Пример #3
0
 /**
  * Sets up the database configuration, loads the database driver
  *
  * @param array $config
  */
 public function __construct($config = array())
 {
     if (empty($config)) {
         // Load the default config
         $config = AetherDatabaseConfig::retrieve('database.default');
     } elseif (is_array($config) && count($config) > 0) {
         if (!array_key_exists('connection', $config)) {
             $config = array('connection' => $config);
         }
     } elseif (is_string($config)) {
         // The config is a DSN string
         if (strpos($config, '://') !== false) {
             $config = array('connection' => $config);
         } else {
             $name = $config;
             // Test the config name
             if (($config = AetherDatabaseConfig::retrieve('database.' . $config)) === null) {
                 throw new DatabaseException("undefined config {$name}");
             }
         }
     }
     // Merge the default config with the passed config
     $this->config = array_merge($this->config, $config);
     // If the connection is a DSN string
     if (is_string($this->config['connection'])) {
         // Make sure the connection is valid
         if (strpos($this->config['connection'], '://') === false) {
             throw new DatabaseException('invalid_dsn ' . $this->config['connection']);
         }
         // Parse the DSN, creating an array to hold the connection parameters
         $db = array('type' => FALSE, 'user' => FALSE, 'pass' => FALSE, 'host' => FALSE, 'port' => FALSE, 'socket' => FALSE, 'database' => FALSE);
         // Get the protocol and arguments
         list($db['type'], $connection) = explode('://', $this->config['connection'], 2);
         if (strpos($connection, '@') !== false) {
             // Get the username and password
             list($db['pass'], $connection) = explode('@', $connection, 2);
             // Check if a password is supplied
             $logindata = explode(':', $db['pass'], 2);
             $db['pass'] = count($logindata) > 1 ? $logindata[1] : '';
             $db['user'] = $logindata[0];
             // Prepare for finding the database
             $connection = explode('/', $connection);
             // Find the database name
             $db['database'] = array_pop($connection);
             // Reset connection string
             $connection = implode('/', $connection);
             // Find the socket
             if (preg_match('/^unix\\([^)]++\\)/', $connection)) {
                 // This one is a little hairy: we explode based on the end of
                 // the socket, removing the 'unix(' from the connection string
                 list($db['socket'], $connection) = explode(')', substr($connection, 5), 2);
             } elseif (strpos($connection, ':') !== false) {
                 // Fetch the host and port name
                 list($db['host'], $db['port']) = explode(':', $connection, 2);
             } else {
                 $db['host'] = $connection;
             }
         } else {
             // File connection
             $connection = explode('/', $connection);
             // Find database file name
             $db['database'] = array_pop($connection);
             // Find database directory name
             $db['socket'] = implode('/', $connection) . '/';
         }
         // Reset the connection array to the database config
         $this->config['connection'] = $db;
     }
     // Set the driver name
     $driver = 'AetherDatabase' . ucfirst($this->config['connection']['type']) . 'Driver';
     // Load the driver
     if (!AetherDatabaseConfig::autoLoad($driver)) {
         throw new DatabaseException('driver_not_found ' . $this->config['connection']['type']);
     }
     // Initialize the driver
     $this->driver = new $driver($this->config);
     // Validate the driver
     if (!$this->driver instanceof AetherDatabaseDriver) {
         throw new DatabaseException('driver ' . $this->config['connection']['type'] . ' does not implement DatabaseDriver');
     }
     // ::log('debug', 'Database library initialized');
 }
Пример #4
0
 /**
  * Fetches SQL type information about a field, in a generic format.
  *
  * @param string $str field datatype
  * @return array
  */
 protected function sqlType($str)
 {
     static $sqlTypes;
     if ($sqlTypes === NULL) {
         // Load SQL data types
         $sqlTypes = AetherDatabaseConfig::retrieve('sql_types');
     }
     $str = strtolower(trim($str));
     if (($open = strpos($str, '(')) !== false) {
         // Find closing bracket
         $close = strpos($str, ')', $open) - 1;
         // Find the type without the size
         $type = substr($str, 0, $open);
     } else {
         // No length
         $type = $str;
     }
     if (empty($sqlTypes[$type])) {
         exit('Unknown field type: ' . $type . '. ' . 'Please report this to: drift@hardware.no!');
     }
     // Fetch the field definition
     $field = $sqlTypes[$type];
     switch ($field['type']) {
         case 'string':
         case 'float':
             if (isset($close)) {
                 // Add the length to the field info
                 $field['length'] = substr($str, $open + 1, $close - $open);
             }
             break;
         case 'int':
             // Add unsigned value
             $field['unsigned'] = strpos($str, 'unsigned') !== false;
             break;
     }
     return $field;
 }
Пример #5
0
 public function resultArray($object = null, $type = PGSQL_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = 'pg_fetch_object';
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = 'pg_fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == 'pg_fetch_object') {
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         }
     }
     if ($this->totalRows) {
         pg_result_seek($this->result, 0);
         while ($row = $fetch($this->result, NULL, $type)) {
             $rows[] = $row;
         }
     }
     return $rows;
 }
Пример #6
0
 public function resultArray($object = NULL, $type = PDO::FETCH_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = PDO::FETCH_OBJ;
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = PDO::FETCH_OBJ;
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == PDO::FETCH_OBJ) {
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         }
     }
     try {
         while ($row = $this->result->fetch($fetch)) {
             $rows[] = $row;
         }
     } catch (PDOException $e) {
         throw new DatabaseException('database error: ' . $e->getMessage());
         return false;
     }
     return $rows;
 }
Пример #7
0
 public function resultArray($object = NULL, $type = MYSQL_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = 'mysql_fetch_object';
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = 'mysql_fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == 'mysql_fetch_object') {
             if (is_string($this->returnType) && AetherDatabaseConfig::autoLoad($this->returnType)) {
                 $type = $this->returnType;
             } else {
                 $type = 'stdClass';
             }
         }
     }
     if (mysql_num_rows($this->result)) {
         // Reset the pointer location to make sure things work properly
         mysql_data_seek($this->result, 0);
         while ($row = $fetch($this->result, $type)) {
             $rows[] = $row;
         }
     }
     return isset($rows) ? $rows : array();
 }
Пример #8
0
 public function resultArray($object = NULL, $type = MYSQLI_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = 'fetch_object';
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = 'fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == 'fetch_object') {
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         }
     }
     if ($this->result->num_rows) {
         // Reset the pointer location to make sure things work properly
         $this->result->data_seek(0);
         while ($row = $this->result->{$fetch}($type)) {
             $rows[] = $row;
         }
     }
     return isset($rows) ? $rows : array();
 }