YiiBase.php 17.7 KB
Newer Older
Qiang Xue committed
1
<?php
w  
Qiang Xue committed
2 3 4 5
/**
 * YiiBase class file.
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8
 * @license http://www.yiiframework.com/license/
 */
Qiang Xue committed
9

Qiang Xue committed
10
use yii\base\Exception;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
Qiang Xue committed
12
use yii\logging\Logger;
Qiang Xue committed
13

Qiang Xue committed
14
/**
w  
Qiang Xue committed
15
 * Gets the application start timestamp.
Qiang Xue committed
16
 */
w  
Qiang Xue committed
17
defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME', microtime(true));
Qiang Xue committed
18 19 20
/**
 * This constant defines whether the application should be in debug mode or not. Defaults to false.
 */
w  
Qiang Xue committed
21
defined('YII_DEBUG') or define('YII_DEBUG', false);
Qiang Xue committed
22 23 24 25 26
/**
 * This constant defines how much call stack information (file name and line number) should be logged by Yii::trace().
 * Defaults to 0, meaning no backtrace information. If it is greater than 0,
 * at most that number of call stacks will be logged. Note, only user application call stacks are considered.
 */
w  
Qiang Xue committed
27
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 0);
Qiang Xue committed
28
/**
w  
Qiang Xue committed
29
 * This constant defines the framework installation directory.
Qiang Xue committed
30
 */
w  
Qiang Xue committed
31
defined('YII_PATH') or define('YII_PATH', __DIR__);
Qiang Xue committed
32 33 34 35 36
/**
 * This constant defines whether error handling should be enabled. Defaults to true.
 */
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER', true);

w  
Qiang Xue committed
37

Qiang Xue committed
38
/**
w  
Qiang Xue committed
39
 * YiiBase is the core helper class for the Yii framework.
Qiang Xue committed
40
 *
w  
Qiang Xue committed
41
 * Do not use YiiBase directly. Instead, use its child class [[Yii]] where
Qiang Xue committed
42 43 44
 * you can customize methods of YiiBase.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
45
 * @since 2.0
Qiang Xue committed
46 47 48 49 50
 */
class YiiBase
{
	/**
	 * @var array class map used by the Yii autoloading mechanism.
w  
Qiang Xue committed
51 52
	 * The array keys are the class names, and the array values are the corresponding class file paths.
	 * This property mainly affects how [[autoload]] works.
Qiang Xue committed
53 54
	 * @see import
	 * @see autoload
w  
Qiang Xue committed
55 56 57 58 59 60
	 */
	public static $classMap = array();
	/**
	 * @var array list of directories where Yii will search for new classes to be included.
	 * The first directory in the array will be searched first, and so on.
	 * This property mainly affects how [[autoload]] works.
Qiang Xue committed
61 62
	 * @see import
	 * @see autoload
w  
Qiang Xue committed
63 64 65 66
	 */
	public static $classPath = array();
	/**
	 * @var yii\base\Application the application instance
Qiang Xue committed
67
	 */
Qiang Xue committed
68
	public static $app;
w  
Qiang Xue committed
69 70
	/**
	 * @var array registered path aliases
Qiang Xue committed
71 72
	 * @see getAlias
	 * @see setAlias
w  
Qiang Xue committed
73 74
	 */
	public static $aliases = array(
w  
Qiang Xue committed
75
		'@yii' => __DIR__,
w  
Qiang Xue committed
76
	);
Qiang Xue committed
77 78
	/**
	 * @var array initial property values that will be applied to objects newly created via [[createObject]].
Qiang Xue committed
79 80
	 * The array keys are class names without leading backslashes "\", and the array values are the corresponding
	 * name-value pairs for initializing the created class instances. For example,
Qiang Xue committed
81 82 83
	 *
	 * ~~~
	 * array(
Qiang Xue committed
84
	 *     'Bar' => array(
Qiang Xue committed
85 86 87
	 *         'prop1' => 'value1',
	 *         'prop2' => 'value2',
	 *     ),
Qiang Xue committed
88
	 *     'mycompany\foo\Car' => array(
Qiang Xue committed
89 90 91 92 93 94 95 96 97
	 *         'prop1' => 'value1',
	 *         'prop2' => 'value2',
	 *     ),
	 * )
	 * ~~~
	 *
	 * @see createObject
	 */
	public static $objectConfig = array();
Qiang Xue committed
98

w  
Qiang Xue committed
99 100
	private static $_imported = array();	// alias => class name or directory
	private static $_logger;
Qiang Xue committed
101 102 103 104 105 106

	/**
	 * @return string the version of Yii framework
	 */
	public static function getVersion()
	{
w  
Qiang Xue committed
107
		return '2.0-dev';
Qiang Xue committed
108 109 110 111 112 113 114
	}

	/**
	 * Imports a class or a directory.
	 *
	 * Importing a class is like including the corresponding class file.
	 * The main difference is that importing a class is much lighter because it only
w  
Qiang Xue committed
115
	 * includes the class file when the class is referenced in the code the first time.
Qiang Xue committed
116
	 *
w  
Qiang Xue committed
117 118 119 120 121
	 * Importing a directory will add the directory to the front of the [[classPath]] array.
	 * When [[autoload]] is loading an unknown class, it will search in the directories
	 * specified in [[classPath]] to find the corresponding class file to include.
	 * For this reason, if multiple directories are imported, the directories imported later
	 * will take precedence in class file searching.
Qiang Xue committed
122
	 *
w  
Qiang Xue committed
123 124
	 * The same class or directory can be imported multiple times. Only the first importing
	 * will count. Importing a directory does not import any of its subdirectories.
Qiang Xue committed
125
	 *
w  
Qiang Xue committed
126
	 * To import a class or a directory, one can use either path alias or class name (can be namespaced):
Qiang Xue committed
127
	 *
Qiang Xue committed
128 129
	 *  - `@app/components/GoogleMap`: importing the `GoogleMap` class with a path alias;
	 *  - `@app/components/*`: importing the whole `components` directory with a path alias;
Qiang Xue committed
130 131
	 *  - `GoogleMap`: importing the `GoogleMap` class with a class name. [[autoload()]] will be used
	 *  when this class is used for the first time.
Qiang Xue committed
132
	 *
w  
Qiang Xue committed
133
	 * @param string $alias path alias or a simple class name to be imported
Qiang Xue committed
134 135 136 137
	 * @param boolean $forceInclude whether to include the class file immediately. If false, the class file
	 * will be included only when the class is being used. This parameter is used only when
	 * the path alias refers to a class.
	 * @return string the class name or the directory that this alias refers to
Qiang Xue committed
138
	 * @throws Exception if the path alias is invalid
Qiang Xue committed
139
	 */
w  
Qiang Xue committed
140
	public static function import($alias, $forceInclude = false)
Qiang Xue committed
141
	{
w  
Qiang Xue committed
142 143 144
		if (isset(self::$_imported[$alias])) {
			return self::$_imported[$alias];
		}
Qiang Xue committed
145

Qiang Xue committed
146 147 148 149 150
		if ($alias[0] !== '@') {
			// a simple class name
			if (class_exists($alias, false) || interface_exists($alias, false)) {
				return self::$_imported[$alias] = $alias;
			}
w  
Qiang Xue committed
151
			if ($forceInclude && static::autoload($alias)) {
w  
Qiang Xue committed
152 153
				self::$_imported[$alias] = $alias;
			}
Qiang Xue committed
154 155 156
			return $alias;
		}

w  
Qiang Xue committed
157
		$className = basename($alias);
w  
Qiang Xue committed
158
		$isClass = $className !== '*';
Qiang Xue committed
159

w  
Qiang Xue committed
160 161 162
		if ($isClass && (class_exists($className, false) || interface_exists($className, false))) {
			return self::$_imported[$alias] = $className;
		}
Qiang Xue committed
163

w  
Qiang Xue committed
164
		if (($path = static::getAlias(dirname($alias))) === false) {
Qiang Xue committed
165
			throw new Exception('Invalid path alias: ' . $alias);
w  
Qiang Xue committed
166
		}
Qiang Xue committed
167

w  
Qiang Xue committed
168 169 170 171
		if ($isClass) {
			if ($forceInclude) {
				require($path . "/$className.php");
				self::$_imported[$alias] = $className;
Qiang Xue committed
172
			} else {
Qiang Xue committed
173
				self::$classMap[$className] = $path . DIRECTORY_SEPARATOR . "$className.php";
w  
Qiang Xue committed
174 175
			}
			return $className;
Qiang Xue committed
176 177
		} else {
			// a directory
w  
Qiang Xue committed
178 179
			array_unshift(self::$classPath, $path);
			return self::$_imported[$alias] = $path;
Qiang Xue committed
180 181 182 183
		}
	}

	/**
w  
Qiang Xue committed
184
	 * Translates a path alias into an actual path.
m  
Qiang Xue committed
185
	 *
w  
Qiang Xue committed
186
	 * The path alias can be either a root alias registered via [[setAlias]] or an
w  
Qiang Xue committed
187 188 189
	 * alias starting with a root alias (e.g. `@yii/base/Component.php`).
	 * In the latter case, the root alias will be replaced by the corresponding registered path
	 * and the remaining part will be appended to it.
m  
Qiang Xue committed
190
	 *
w  
Qiang Xue committed
191 192
	 * In case the given parameter is not an alias (i.e., not starting with '@'),
	 * it will be returned back without change.
w  
Qiang Xue committed
193
	 *
w  
Qiang Xue committed
194 195
	 * Note, this method does not ensure the existence of the resulting path.
	 * @param string $alias alias
Qiang Xue committed
196
	 * @return string|boolean path corresponding to the alias, false if the root alias is not previously registered.
w  
Qiang Xue committed
197
	 * @see setAlias
Qiang Xue committed
198
	 */
Qiang Xue committed
199
	public static function getAlias($alias)
Qiang Xue committed
200
	{
Qiang Xue committed
201 202 203
		if (!is_string($alias)) {
			return false;
		} elseif (isset(self::$aliases[$alias])) {
w  
Qiang Xue committed
204
			return self::$aliases[$alias];
Qiang Xue committed
205
		} elseif ($alias === '' || $alias[0] !== '@') { // not an alias
w  
Qiang Xue committed
206
			return $alias;
Qiang Xue committed
207
		} elseif (($pos = strpos($alias, '/')) !== false) {
w  
Qiang Xue committed
208
			$rootAlias = substr($alias, 0, $pos);
w  
Qiang Xue committed
209 210
			if (isset(self::$aliases[$rootAlias])) {
				return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos);
Qiang Xue committed
211 212
			}
		}
Qiang Xue committed
213
		return false;
Qiang Xue committed
214 215 216
	}

	/**
w  
Qiang Xue committed
217
	 * Registers a path alias.
m  
Qiang Xue committed
218
	 *
w  
Qiang Xue committed
219 220
	 * A path alias is a short name representing a path (a file path, a URL, etc.)
	 * A path alias must start with '@' (e.g. '@yii').
m  
Qiang Xue committed
221
	 *
w  
Qiang Xue committed
222
	 * Note that this method neither checks the existence of the path nor normalizes the path.
m  
Qiang Xue committed
223 224
	 * Any trailing '/' and '\' characters in the path will be trimmed.
	 *
w  
Qiang Xue committed
225
	 * @param string $alias alias to the path. The alias must start with '@'.
m  
Qiang Xue committed
226 227 228 229 230 231
	 * @param string $path the path corresponding to the alias. This can be
	 *
	 * - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`)
	 * - a URL (e.g. `http://www.yiiframework.com`)
	 * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the
	 *   actual path first by calling [[getAlias]].
Qiang Xue committed
232
	 * @throws Exception if $path is an invalid alias
w  
Qiang Xue committed
233
	 * @see getAlias
Qiang Xue committed
234
	 */
w  
Qiang Xue committed
235
	public static function setAlias($alias, $path)
Qiang Xue committed
236
	{
w  
Qiang Xue committed
237
		if ($path === null) {
w  
Qiang Xue committed
238
			unset(self::$aliases[$alias]);
Qiang Xue committed
239
		} elseif ($path[0] !== '@') {
w  
Qiang Xue committed
240
			self::$aliases[$alias] = rtrim($path, '\\/');
Qiang Xue committed
241
		} elseif (($p = static::getAlias($path)) !== false) {
m  
Qiang Xue committed
242
			self::$aliases[$alias] = $p;
Qiang Xue committed
243
		} else {
Qiang Xue committed
244
			throw new Exception('Invalid path: ' . $path);
m  
Qiang Xue committed
245
		}
Qiang Xue committed
246 247 248 249
	}

	/**
	 * Class autoload loader.
w  
Qiang Xue committed
250 251 252 253 254 255 256 257 258 259 260 261 262
	 * This method is invoked automatically when the execution encounters an unknown class.
	 * The method will attempt to include the class file as follows:
	 *
	 * 1. Search in [[classMap]];
	 * 2. If the class is namespaced (e.g. `yii\base\Component`), it will attempt
	 *    to include the file associated with the corresponding path alias
	 *    (e.g. `@yii/base/Component.php`);
	 * 3. If the class is named in PEAR style (e.g. `PHPUnit_Framework_TestCase`),
	 *    it will attempt to include the file associated with the corresponding path alias
	 *    (e.g. `@PHPUnit/Framework/TestCase.php`);
	 * 4. Search in [[classPath]];
	 * 5. Return false so that other autoloaders have chance to include the class file.
	 *
Qiang Xue committed
263 264
	 * @param string $className class name
	 * @return boolean whether the class has been loaded successfully
Qiang Xue committed
265
	 * @throws Exception if the class file does not exist
Qiang Xue committed
266 267 268
	 */
	public static function autoload($className)
	{
w  
Qiang Xue committed
269
		if (isset(self::$classMap[$className])) {
Qiang Xue committed
270
			include(self::$classMap[$className]);
w  
Qiang Xue committed
271 272 273 274
			return true;
		}

		if (strpos($className, '\\') !== false) {
Qiang Xue committed
275
			// namespaced class, e.g. yii\base\Component
w  
Qiang Xue committed
276
			// convert namespace to path alias, e.g. yii\base\Component to @yii/base/Component
w  
Qiang Xue committed
277
			$alias = '@' . str_replace('\\', '/', ltrim($className, '\\'));
w  
Qiang Xue committed
278
			if (($path = static::getAlias($alias)) !== false) {
Qiang Xue committed
279
				$classFile = $path . '.php';
Qiang Xue committed
280
			}
Qiang Xue committed
281 282
		} elseif (($pos = strpos($className, '_')) !== false) {
			// PEAR-styled class, e.g. PHPUnit_Framework_TestCase
w  
Qiang Xue committed
283 284
			// convert class name to path alias, e.g. PHPUnit_Framework_TestCase to @PHPUnit/Framework/TestCase
			$alias = '@' . str_replace('_', '/', $className);
w  
Qiang Xue committed
285
			if (($path = static::getAlias($alias)) !== false) {
Qiang Xue committed
286
				$classFile = $path . '.php';
w  
Qiang Xue committed
287 288 289
			}
		}

Qiang Xue committed
290 291 292 293 294 295 296 297 298 299 300 301 302
		if (!isset($classFile)) {
			// search in include paths
			foreach (self::$classPath as $path) {
				$path .= DIRECTORY_SEPARATOR . $className . '.php';
				if (is_file($path)) {
					$classFile = $path;
					$alias = $className;
				}
			}
		}

		if (isset($classFile, $alias)) {
			if (!YII_DEBUG || basename(realpath($classFile)) === basename($alias) . '.php') {
w  
Qiang Xue committed
303 304
				include($classFile);
				return true;
Qiang Xue committed
305
			} else {
Qiang Xue committed
306
				throw new Exception("Class name '$className' does not match the class file '" . realpath($classFile) . "'. Have you checked their case sensitivity?");
w  
Qiang Xue committed
307 308 309 310
			}
		}

		return false;
Qiang Xue committed
311 312
	}

w  
Qiang Xue committed
313
	/**
Qiang Xue committed
314
	 * Creates a new object using the given configuration.
w  
Qiang Xue committed
315
	 *
Qiang Xue committed
316 317 318
	 * The configuration can be either a string or an array.
	 * If a string, it is treated as the *object type*; if an array,
	 * it must contain a `class` element specifying the *object type*, and
w  
Qiang Xue committed
319 320 321
	 * the rest of the name-value pairs in the array will be used to initialize
	 * the corresponding object properties.
	 *
Qiang Xue committed
322
	 * The object type can be either a class name or the [[getAlias|alias]] of
w  
Qiang Xue committed
323
	 * the class. For example,
w  
Qiang Xue committed
324
	 *
Qiang Xue committed
325
	 * - `\app\components\GoogleMap`: fully-qualified namespaced class.
Qiang Xue committed
326
	 * - `@app/components/GoogleMap`: an alias
Qiang Xue committed
327 328 329
	 *
	 * Below are some usage examples:
	 *
w  
Qiang Xue committed
330
	 * ~~~
Qiang Xue committed
331
	 * $object = \Yii::createObject('@app/components/GoogleMap');
Qiang Xue committed
332 333
	 * $object = \Yii::createObject(array(
	 *     'class' => '\app\components\GoogleMap',
w  
Qiang Xue committed
334 335 336 337
	 *     'apiKey' => 'xyz',
	 * ));
	 * ~~~
	 *
Qiang Xue committed
338 339 340 341 342 343 344 345 346 347
	 * This method can be used to create any object as long as the object's constructor is
	 * defined like the following:
	 *
	 * ~~~
	 * public function __construct(..., $config = array()) {
	 * }
	 * ~~~
	 *
	 * The method will pass the given configuration as the last parameter of the constructor,
	 * and any additional parameters to this method will be passed as the rest of the constructor parameters.
w  
Qiang Xue committed
348
	 *
Qiang Xue committed
349 350
	 * @param string|array $config the configuration. It can be either a string representing the class name
	 * or an array representing the object configuration.
w  
Qiang Xue committed
351
	 * @return mixed the created object
Qiang Xue committed
352
	 * @throws InvalidConfigException if the configuration is invalid.
w  
Qiang Xue committed
353
	 */
Qiang Xue committed
354
	public static function createObject($config)
w  
Qiang Xue committed
355
	{
Qiang Xue committed
356 357
		static $reflections = array();

w  
Qiang Xue committed
358
		if (is_string($config)) {
w  
Qiang Xue committed
359
			$class = $config;
w  
Qiang Xue committed
360
			$config = array();
Qiang Xue committed
361
		} elseif (isset($config['class'])) {
w  
Qiang Xue committed
362
			$class = $config['class'];
w  
Qiang Xue committed
363
			unset($config['class']);
Qiang Xue committed
364
		} else {
Qiang Xue committed
365
			throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
w  
Qiang Xue committed
366 367
		}

w  
Qiang Xue committed
368 369
		if (!class_exists($class, false)) {
			$class = static::import($class, true);
w  
Qiang Xue committed
370 371
		}

Qiang Xue committed
372
		if (($n = func_num_args()) > 1) {
Qiang Xue committed
373 374 375
			/** @var $reflection \ReflectionClass */
			if (isset($reflections[$class])) {
				$reflection = $reflections[$class];
Qiang Xue committed
376
			} else {
Qiang Xue committed
377 378 379 380 381 382
				$reflection = $reflections[$class] = new \ReflectionClass($class);
			}
			$args = func_get_args();
			array_shift($args); // remove $config
			if ($config !== array()) {
				$args[] = $config;
Qiang Xue committed
383
			}
Qiang Xue committed
384
			return $reflection->newInstanceArgs($args);
Qiang Xue committed
385
		} else {
Qiang Xue committed
386
			return $config === array() ? new $class : new $class($config);
Qiang Xue committed
387
		}
w  
Qiang Xue committed
388 389
	}

Qiang Xue committed
390
	/**
w  
Qiang Xue committed
391 392 393 394 395
	 * Logs a trace message.
	 * Trace messages are logged mainly for development purpose to see
	 * the execution work flow of some code.
	 * @param string $message the message to be logged.
	 * @param string $category the category of the message.
Qiang Xue committed
396
	 */
w  
Qiang Xue committed
397
	public static function trace($message, $category = 'application')
Qiang Xue committed
398
	{
w  
Qiang Xue committed
399
		if (YII_DEBUG) {
Qiang Xue committed
400
			self::getLogger()->log($message, Logger::LEVEL_TRACE, $category);
w  
Qiang Xue committed
401
		}
Qiang Xue committed
402 403 404
	}

	/**
w  
Qiang Xue committed
405 406 407 408 409
	 * Logs an error message.
	 * An error message is typically logged when an unrecoverable error occurs
	 * during the execution of an application.
	 * @param string $message the message to be logged.
	 * @param string $category the category of the message.
Qiang Xue committed
410
	 */
Qiang Xue committed
411
	public static function error($message, $category = 'application')
Qiang Xue committed
412
	{
Qiang Xue committed
413
		self::getLogger()->log($message, Logger::LEVEL_ERROR, $category);
w  
Qiang Xue committed
414 415 416 417 418 419 420 421 422
	}

	/**
	 * Logs a warning message.
	 * A warning message is typically logged when an error occurs while the execution
	 * can still continue.
	 * @param string $message the message to be logged.
	 * @param string $category the category of the message.
	 */
Qiang Xue committed
423
	public static function warning($message, $category = 'application')
w  
Qiang Xue committed
424
	{
Qiang Xue committed
425
		self::getLogger()->log($message, Logger::LEVEL_WARNING, $category);
Qiang Xue committed
426 427 428
	}

	/**
w  
Qiang Xue committed
429 430 431 432 433 434
	 * Logs an informative message.
	 * An informative message is typically logged by an application to keep record of
	 * something important (e.g. an administrator logs in).
	 * @param string $message the message to be logged.
	 * @param string $category the category of the message.
	 */
Qiang Xue committed
435
	public static function info($message, $category = 'application')
w  
Qiang Xue committed
436
	{
Qiang Xue committed
437
		self::getLogger()->log($message, Logger::LEVEL_INFO, $category);
w  
Qiang Xue committed
438 439 440 441 442 443 444 445 446
	}

	/**
	 * Marks the beginning of a code block for profiling.
	 * This has to be matched with a call to [[endProfile]] with the same category name.
	 * The begin- and end- calls must also be properly nested. For example,
	 *
	 * ~~~
	 * \Yii::beginProfile('block1');
Qiang Xue committed
447 448 449 450
	 * // some code to be profiled
	 *     \Yii::beginProfile('block2');
	 *     // some other code to be profiled
	 *     \Yii::endProfile('block2');
w  
Qiang Xue committed
451 452
	 * \Yii::endProfile('block1');
	 * ~~~
Qiang Xue committed
453 454
	 * @param string $token token for the code block
	 * @param string $category the category of this log message
Qiang Xue committed
455 456
	 * @see endProfile
	 */
Qiang Xue committed
457
	public static function beginProfile($token, $category = 'application')
Qiang Xue committed
458
	{
Qiang Xue committed
459
		self::getLogger()->log($token, Logger::LEVEL_PROFILE_BEGIN, $category);
Qiang Xue committed
460 461 462 463
	}

	/**
	 * Marks the end of a code block for profiling.
w  
Qiang Xue committed
464
	 * This has to be matched with a previous call to [[beginProfile]] with the same category name.
Qiang Xue committed
465 466
	 * @param string $token token for the code block
	 * @param string $category the category of this log message
Qiang Xue committed
467 468
	 * @see beginProfile
	 */
Qiang Xue committed
469
	public static function endProfile($token, $category = 'application')
Qiang Xue committed
470
	{
Qiang Xue committed
471
		self::getLogger()->log($token, Logger::LEVEL_PROFILE_END, $category);
Qiang Xue committed
472 473 474
	}

	/**
w  
Qiang Xue committed
475 476
	 * Returns the message logger object.
	 * @return \yii\logging\Logger message logger
Qiang Xue committed
477 478 479
	 */
	public static function getLogger()
	{
w  
Qiang Xue committed
480
		if (self::$_logger !== null) {
Qiang Xue committed
481
			return self::$_logger;
Qiang Xue committed
482
		} else {
Qiang Xue committed
483
			return self::$_logger = new Logger;
w  
Qiang Xue committed
484
		}
w  
Qiang Xue committed
485 486 487 488
	}

	/**
	 * Sets the logger object.
Qiang Xue committed
489
	 * @param Logger $logger the logger object.
w  
Qiang Xue committed
490 491 492 493
	 */
	public static function setLogger($logger)
	{
		self::$_logger = $logger;
Qiang Xue committed
494 495 496
	}

	/**
w  
Qiang Xue committed
497 498
	 * Returns an HTML hyperlink that can be displayed on your Web page showing Powered by Yii" information.
	 * @return string an HTML hyperlink that can be displayed on your Web page showing Powered by Yii" information
Qiang Xue committed
499 500 501 502 503 504 505 506
	 */
	public static function powered()
	{
		return 'Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>.';
	}

	/**
	 * Translates a message to the specified language.
507
	 * This method supports choice format (see {@link CChoiceFormat}),
Qiang Xue committed
508 509 510 511 512
	 * i.e., the message returned will be chosen from a few candidates according to the given
	 * number value. This feature is mainly used to solve plural format issue in case
	 * a message has different plural forms in some languages.
	 * @param string $message the original message
	 * @param array $params parameters to be applied to the message using <code>strtr</code>.
513
	 * The first parameter can be a number without key.
Qiang Xue committed
514 515
	 * And in this case, the method will call {@link CChoiceFormat::format} to choose
	 * an appropriate message translation.
516
	 * You can pass parameter for {@link CChoiceFormat::format}
Qiang Xue committed
517 518 519 520 521
	 * or plural forms format without wrapping it with array.
	 * @param string $language the target language. If null (default), the {@link CApplication::getLanguage application language} will be used.
	 * @return string the translated message
	 * @see CMessageSource
	 */
Qiang Xue committed
522
	public static function t($message, $params = array(), $language = null)
Qiang Xue committed
523
	{
Qiang Xue committed
524
		return Yii::$app->getI18N()->translate($message, $params, $language);
Qiang Xue committed
525 526
	}
}