-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathConfig.php
333 lines (299 loc) · 7.73 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?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;
/**
* Switch for logging.
*
* @var bool
*/
private $logging = false;
/**
* Contains a Logger object that must impelement a log() method.
*
* @var object
*/
private $logger;
/**
* Contains the class name for the Date class to use. Must have a public format() method and a
* public static createFromFormat($format, $time) method
*
* @var string
*/
private $date_class = 'ActiveRecord\\DateTime';
/**
* The format to serialize DateTime values into.
*
* @var string
*/
private $date_format = \DateTime::ISO8601;
/**
* 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
*/
public function set_model_directory($dir)
{
$this->model_directory = $dir;
}
/**
* Returns the model directory.
*
* @return string
* @throws ConfigException if specified directory was not found
*/
public function get_model_directory()
{
if ($this->model_directory && !file_exists($this->model_directory))
throw new ConfigException('Invalid or non-existent directory: '.$this->model_directory);
return $this->model_directory;
}
/**
* Turn on/off logging
*
* @param boolean $bool
* @return void
*/
public function set_logging($bool)
{
$this->logging = (bool)$bool;
}
/**
* Sets the logger object for future SQL logging
*
* @param object $logger
* @return void
* @throws ConfigException if Logger objecct does not implement public log()
*/
public function set_logger($logger)
{
$klass = Reflections::instance()->add($logger)->get($logger);
if (!$klass->getMethod('log') || !$klass->getMethod('log')->isPublic())
throw new ConfigException("Logger object must implement a public log method");
$this->logger = $logger;
}
/**
* Return whether or not logging is on
*
* @return boolean
*/
public function get_logging()
{
return $this->logging;
}
/**
* Returns the logger
*
* @return object
*/
public function get_logger()
{
return $this->logger;
}
public function set_date_class($date_class)
{
try {
$klass = Reflections::instance()->add($date_class)->get($date_class);
} catch (\ReflectionException $e) {
throw new ConfigException("Cannot find date class");
}
if (!$klass->hasMethod('format') || !$klass->getMethod('format')->isPublic())
throw new ConfigException('Given date class must have a "public format($format = null)" method');
if (!$klass->hasMethod('createFromFormat') || !$klass->getMethod('createFromFormat')->isPublic())
throw new ConfigException('Given date class must have a "public static createFromFormat($format, $time)" method');
$this->date_class = $date_class;
}
public function get_date_class()
{
return $this->date_class;
}
/**
* @deprecated
*/
public function get_date_format()
{
trigger_error('Use ActiveRecord\Serialization::$DATETIME_FORMAT. Config::get_date_format() has been deprecated.', E_USER_DEPRECATED);
return Serialization::$DATETIME_FORMAT;
}
/**
* @deprecated
*/
public function set_date_format($format)
{
trigger_error('Use ActiveRecord\Serialization::$DATETIME_FORMAT. Config::set_date_format() has been deprecated.', E_USER_DEPRECATED);
Serialization::$DATETIME_FORMAT = $format;
}
/**
* Sets the url for the cache server to enable query caching.
*
* Only table schema queries are cached at the moment. A general query cache
* will follow.
*
* Example:
*
* <code>
* $config->set_cache("memcached://localhost");
* $config->set_cache("memcached://localhost",array("expire" => 60));
* </code>
*
* @param string $url Url to your cache server.
* @param array $options Array of options
*/
public function set_cache($url, $options=array())
{
Cache::initialize($url,$options);
}
}