PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/site/application/third_party/php-activerecord/lib/Config.php

https://bitbucket.org/thiscode/thiscode-shop
PHP | 304 lines | 91 code | 28 blank | 185 comment | 7 complexity | 0382e700e342a10b5291b5b0839a0ca9 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. * Switch for logging.
  59. *
  60. * @var bool
  61. */
  62. private $logging = false;
  63. /**
  64. * Contains a Logger object that must impelement a log() method.
  65. *
  66. * @var object
  67. */
  68. private $logger;
  69. /**
  70. * The format to serialize DateTime values into.
  71. *
  72. * @var string
  73. */
  74. private $date_format = \DateTime::ISO8601;
  75. /**
  76. * Allows config initialization using a closure.
  77. *
  78. * This method is just syntatic sugar.
  79. *
  80. * <code>
  81. * ActiveRecord\Config::initialize(function($cfg) {
  82. * $cfg->set_model_directory('/path/to/your/model_directory');
  83. * $cfg->set_connections(array(
  84. * 'development' => 'mysql://username:password@127.0.0.1/database_name'));
  85. * });
  86. * </code>
  87. *
  88. * You can also initialize by grabbing the singleton object:
  89. *
  90. * <code>
  91. * $cfg = ActiveRecord\Config::instance();
  92. * $cfg->set_model_directory('/path/to/your/model_directory');
  93. * $cfg->set_connections(array('development' =>
  94. * 'mysql://username:password@localhost/database_name'));
  95. * </code>
  96. *
  97. * @param Closure $initializer A closure
  98. * @return void
  99. */
  100. public static function initialize(Closure $initializer)
  101. {
  102. $initializer(parent::instance());
  103. }
  104. /**
  105. * Sets the list of database connection strings.
  106. *
  107. * <code>
  108. * $config->set_connections(array(
  109. * 'development' => 'mysql://username:password@127.0.0.1/database_name'));
  110. * </code>
  111. *
  112. * @param array $connections Array of connections
  113. * @param string $default_connection Optionally specify the default_connection
  114. * @return void
  115. * @throws ActiveRecord\ConfigException
  116. */
  117. public function set_connections($connections, $default_connection=null)
  118. {
  119. if (!is_array($connections))
  120. throw new ConfigException("Connections must be an array");
  121. if ($default_connection)
  122. $this->set_default_connection($default_connection);
  123. $this->connections = $connections;
  124. }
  125. /**
  126. * Returns the connection strings array.
  127. *
  128. * @return array
  129. */
  130. public function get_connections()
  131. {
  132. return $this->connections;
  133. }
  134. /**
  135. * Returns a connection string if found otherwise null.
  136. *
  137. * @param string $name Name of connection to retrieve
  138. * @return string connection info for specified connection name
  139. */
  140. public function get_connection($name)
  141. {
  142. if (array_key_exists($name, $this->connections))
  143. return $this->connections[$name];
  144. return null;
  145. }
  146. /**
  147. * Returns the default connection string or null if there is none.
  148. *
  149. * @return string
  150. */
  151. public function get_default_connection_string()
  152. {
  153. return array_key_exists($this->default_connection,$this->connections) ?
  154. $this->connections[$this->default_connection] : null;
  155. }
  156. /**
  157. * Returns the name of the default connection.
  158. *
  159. * @return string
  160. */
  161. public function get_default_connection()
  162. {
  163. return $this->default_connection;
  164. }
  165. /**
  166. * Set the name of the default connection.
  167. *
  168. * @param string $name Name of a connection in the connections array
  169. * @return void
  170. */
  171. public function set_default_connection($name)
  172. {
  173. $this->default_connection = $name;
  174. }
  175. /**
  176. * Sets the directory where models are located.
  177. *
  178. * @param string $dir Directory path containing your models
  179. * @return void
  180. */
  181. public function set_model_directory($dir)
  182. {
  183. $this->model_directory = $dir;
  184. }
  185. /**
  186. * Returns the model directory.
  187. *
  188. * @return string
  189. * @throws ConfigException if specified directory was not found
  190. */
  191. public function get_model_directory()
  192. {
  193. if ($this->model_directory && !file_exists($this->model_directory))
  194. throw new ConfigException('Invalid or non-existent directory: '.$this->model_directory);
  195. return $this->model_directory;
  196. }
  197. /**
  198. * Turn on/off logging
  199. *
  200. * @param boolean $bool
  201. * @return void
  202. */
  203. public function set_logging($bool)
  204. {
  205. $this->logging = (bool)$bool;
  206. }
  207. /**
  208. * Sets the logger object for future SQL logging
  209. *
  210. * @param object $logger
  211. * @return void
  212. * @throws ConfigException if Logger objecct does not implement public log()
  213. */
  214. public function set_logger($logger)
  215. {
  216. $klass = Reflections::instance()->add($logger)->get($logger);
  217. if (!$klass->getMethod('log') || !$klass->getMethod('log')->isPublic())
  218. throw new ConfigException("Logger object must implement a public log method");
  219. $this->logger = $logger;
  220. }
  221. /**
  222. * Return whether or not logging is on
  223. *
  224. * @return boolean
  225. */
  226. public function get_logging()
  227. {
  228. return $this->logging;
  229. }
  230. /**
  231. * Returns the logger
  232. *
  233. * @return object
  234. */
  235. public function get_logger()
  236. {
  237. return $this->logger;
  238. }
  239. /**
  240. * @deprecated
  241. */
  242. public function get_date_format()
  243. {
  244. trigger_error('Use ActiveRecord\Serialization::$DATETIME_FORMAT. Config::get_date_format() has been deprecated.', E_USER_DEPRECATED);
  245. return Serialization::$DATETIME_FORMAT;
  246. }
  247. /**
  248. * @deprecated
  249. */
  250. public function set_date_format($format)
  251. {
  252. trigger_error('Use ActiveRecord\Serialization::$DATETIME_FORMAT. Config::set_date_format() has been deprecated.', E_USER_DEPRECATED);
  253. Serialization::$DATETIME_FORMAT = $format;
  254. }
  255. /**
  256. * Sets the url for the cache server to enable query caching.
  257. *
  258. * Only table schema queries are cached at the moment. A general query cache
  259. * will follow.
  260. *
  261. * Example:
  262. *
  263. * <code>
  264. * $config->set_cache("memcached://localhost");
  265. * $config->set_cache("memcached://localhost",array("expire" => 60));
  266. * </code>
  267. *
  268. * @param string $url Url to your cache server.
  269. * @param array $options Array of options
  270. */
  271. public function set_cache($url, $options=array())
  272. {
  273. Cache::initialize($url,$options);
  274. }
  275. };
  276. ?>