/core/libs/classes/active_record/lib/Config.php
https://github.com/double-z/phpLightMVC · PHP · 196 lines · 55 code · 16 blank · 125 comment · 4 complexity · d9201144c4ea94095ed70908fc2a831a MD5 · raw file
- <?php
- /**
- * @package ActiveRecord
- */
- namespace ActiveRecord;
- use Closure;
- /**
- * Manages configuration options for ActiveRecord.
- *
- * <code>
- * ActiveRecord::initialize(function($cfg) {
- * $cfg->set_model_home('models');
- * $cfg->set_connections(array(
- * 'development' => 'mysql://user:pass@development.com/awesome_development',
- * 'production' => 'mysql://user:pass@production.com/awesome_production'));
- * });
- * </code>
- *
- * @package ActiveRecord
- */
- class Config extends Singleton
- {
- /**
- * Name of the connection to use by default.
- *
- * <code>
- * ActiveRecord\Config::initialize(function($cfg) {
- * $cfg->set_model_directory('/your/app/models');
- * $cfg->set_connections(array(
- * 'development' => 'mysql://user:pass@development.com/awesome_development',
- * 'production' => 'mysql://user:pass@production.com/awesome_production'));
- * });
- * </code>
- *
- * This is a singleton class so you can retrieve the {@link Singleton} instance by doing:
- *
- * <code>
- * $config = ActiveRecord\Config::instance();
- * </code>
- *
- * @var string
- */
- private $default_connection = 'development';
- /**
- * Contains the list of database connection strings.
- *
- * @var array
- */
- private $connections = array();
- /**
- * Directory for the auto_loading of model classes.
- *
- * @see activerecord_autoload
- * @var string
- */
- private $model_directory;
- /**
- * Allows config initialization using a closure.
- *
- * This method is just syntatic sugar.
- *
- * <code>
- * ActiveRecord\Config::initialize(function($cfg) {
- * $cfg->set_model_directory('/path/to/your/model_directory');
- * $cfg->set_connections(array(
- * 'development' => 'mysql://username:password@127.0.0.1/database_name'));
- * });
- * </code>
- *
- * You can also initialize by grabbing the singleton object:
- *
- * <code>
- * $cfg = ActiveRecord\Config::instance();
- * $cfg->set_model_directory('/path/to/your/model_directory');
- * $cfg->set_connections(array('development' =>
- * 'mysql://username:password@localhost/database_name'));
- * </code>
- *
- * @param Closure $initializer A closure
- * @return void
- */
- public static function initialize(Closure $initializer)
- {
- $initializer(parent::instance());
- }
- /**
- * Sets the list of database connection strings.
- *
- * <code>
- * $config->set_connections(array(
- * 'development' => 'mysql://username:password@127.0.0.1/database_name'));
- * </code>
- *
- * @param array $connections Array of connections
- * @param string $default_connection Optionally specify the default_connection
- * @return void
- * @throws ActiveRecord\ConfigException
- */
- public function set_connections($connections, $default_connection=null)
- {
- if (!is_array($connections))
- throw new ConfigException("Connections must be an array");
- if ($default_connection)
- $this->set_default_connection($default_connection);
- $this->connections = $connections;
- }
- /**
- * Returns the connection strings array.
- *
- * @return array
- */
- public function get_connections()
- {
- return $this->connections;
- }
- /**
- * Returns a connection string if found otherwise null.
- *
- * @param string $name Name of connection to retrieve
- * @return string connection info for specified connection name
- */
- public function get_connection($name)
- {
- if (array_key_exists($name, $this->connections))
- return $this->connections[$name];
- return null;
- }
- /**
- * Returns the default connection string or null if there is none.
- *
- * @return string
- */
- public function get_default_connection_string()
- {
- return array_key_exists($this->default_connection,$this->connections) ?
- $this->connections[$this->default_connection] : null;
- }
- /**
- * Returns the name of the default connection.
- *
- * @return string
- */
- public function get_default_connection()
- {
- return $this->default_connection;
- }
- /**
- * Set the name of the default connection.
- *
- * @param string $name Name of a connection in the connections array
- * @return void
- */
- public function set_default_connection($name)
- {
- $this->default_connection = $name;
- }
- /**
- * Sets the directory where models are located.
- *
- * @param string $dir Directory path containing your models
- * @return void
- * @throws ConfigException if specified directory was not found
- */
- public function set_model_directory($dir)
- {
- if (!file_exists($dir))
- throw new ConfigException("Invalid or non-existent directory: $dir");
- $this->model_directory = $dir;
- }
- /**
- * Returns the model directory.
- *
- * @return string
- */
- public function get_model_directory()
- {
- return $this->model_directory;
- }
- };
- ?>