PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/libs/classes/active_record/lib/Config.php

https://github.com/alvarotala/phpLightMVC
PHP | 196 lines | 55 code | 16 blank | 125 comment | 4 complexity | d9201144c4ea94095ed70908fc2a831a MD5 | raw file
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. use Closure;
  7. /**
  8. * Manages configuration options for ActiveRecord.
  9. *
  10. * <code>
  11. * ActiveRecord::initialize(function($cfg) {
  12. * $cfg->set_model_home('models');
  13. * $cfg->set_connections(array(
  14. * 'development' => 'mysql://user:pass@development.com/awesome_development',
  15. * 'production' => 'mysql://user:pass@production.com/awesome_production'));
  16. * });
  17. * </code>
  18. *
  19. * @package ActiveRecord
  20. */
  21. class Config extends Singleton
  22. {
  23. /**
  24. * Name of the connection to use by default.
  25. *
  26. * <code>
  27. * ActiveRecord\Config::initialize(function($cfg) {
  28. * $cfg->set_model_directory('/your/app/models');
  29. * $cfg->set_connections(array(
  30. * 'development' => 'mysql://user:pass@development.com/awesome_development',
  31. * 'production' => 'mysql://user:pass@production.com/awesome_production'));
  32. * });
  33. * </code>
  34. *
  35. * This is a singleton class so you can retrieve the {@link Singleton} instance by doing:
  36. *
  37. * <code>
  38. * $config = ActiveRecord\Config::instance();
  39. * </code>
  40. *
  41. * @var string
  42. */
  43. private $default_connection = 'development';
  44. /**
  45. * Contains the list of database connection strings.
  46. *
  47. * @var array
  48. */
  49. private $connections = array();
  50. /**
  51. * Directory for the auto_loading of model classes.
  52. *
  53. * @see activerecord_autoload
  54. * @var string
  55. */
  56. private $model_directory;
  57. /**
  58. * Allows config initialization using a closure.
  59. *
  60. * This method is just syntatic sugar.
  61. *
  62. * <code>
  63. * ActiveRecord\Config::initialize(function($cfg) {
  64. * $cfg->set_model_directory('/path/to/your/model_directory');
  65. * $cfg->set_connections(array(
  66. * 'development' => 'mysql://username:password@127.0.0.1/database_name'));
  67. * });
  68. * </code>
  69. *
  70. * You can also initialize by grabbing the singleton object:
  71. *
  72. * <code>
  73. * $cfg = ActiveRecord\Config::instance();
  74. * $cfg->set_model_directory('/path/to/your/model_directory');
  75. * $cfg->set_connections(array('development' =>
  76. * 'mysql://username:password@localhost/database_name'));
  77. * </code>
  78. *
  79. * @param Closure $initializer A closure
  80. * @return void
  81. */
  82. public static function initialize(Closure $initializer)
  83. {
  84. $initializer(parent::instance());
  85. }
  86. /**
  87. * Sets the list of database connection strings.
  88. *
  89. * <code>
  90. * $config->set_connections(array(
  91. * 'development' => 'mysql://username:password@127.0.0.1/database_name'));
  92. * </code>
  93. *
  94. * @param array $connections Array of connections
  95. * @param string $default_connection Optionally specify the default_connection
  96. * @return void
  97. * @throws ActiveRecord\ConfigException
  98. */
  99. public function set_connections($connections, $default_connection=null)
  100. {
  101. if (!is_array($connections))
  102. throw new ConfigException("Connections must be an array");
  103. if ($default_connection)
  104. $this->set_default_connection($default_connection);
  105. $this->connections = $connections;
  106. }
  107. /**
  108. * Returns the connection strings array.
  109. *
  110. * @return array
  111. */
  112. public function get_connections()
  113. {
  114. return $this->connections;
  115. }
  116. /**
  117. * Returns a connection string if found otherwise null.
  118. *
  119. * @param string $name Name of connection to retrieve
  120. * @return string connection info for specified connection name
  121. */
  122. public function get_connection($name)
  123. {
  124. if (array_key_exists($name, $this->connections))
  125. return $this->connections[$name];
  126. return null;
  127. }
  128. /**
  129. * Returns the default connection string or null if there is none.
  130. *
  131. * @return string
  132. */
  133. public function get_default_connection_string()
  134. {
  135. return array_key_exists($this->default_connection,$this->connections) ?
  136. $this->connections[$this->default_connection] : null;
  137. }
  138. /**
  139. * Returns the name of the default connection.
  140. *
  141. * @return string
  142. */
  143. public function get_default_connection()
  144. {
  145. return $this->default_connection;
  146. }
  147. /**
  148. * Set the name of the default connection.
  149. *
  150. * @param string $name Name of a connection in the connections array
  151. * @return void
  152. */
  153. public function set_default_connection($name)
  154. {
  155. $this->default_connection = $name;
  156. }
  157. /**
  158. * Sets the directory where models are located.
  159. *
  160. * @param string $dir Directory path containing your models
  161. * @return void
  162. * @throws ConfigException if specified directory was not found
  163. */
  164. public function set_model_directory($dir)
  165. {
  166. if (!file_exists($dir))
  167. throw new ConfigException("Invalid or non-existent directory: $dir");
  168. $this->model_directory = $dir;
  169. }
  170. /**
  171. * Returns the model directory.
  172. *
  173. * @return string
  174. */
  175. public function get_model_directory()
  176. {
  177. return $this->model_directory;
  178. }
  179. };
  180. ?>