YiiBase.php 18.6 KB
Newer Older
Qiang Xue committed
1
<?php
w  
Qiang Xue committed
2 3
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6
 * @license http://www.yiiframework.com/license/
 */
Qiang Xue committed
7
use yii\base\Exception;
Qiang Xue committed
8
use yii\base\InvalidConfigException;
Qiang Xue committed
9
use yii\base\InvalidParamException;
Qiang Xue committed
10
use yii\logging\Logger;
Qiang Xue committed
11

Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * Gets the application start timestamp.
Qiang Xue committed
14
 */
w  
Qiang Xue committed
15
defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME', microtime(true));
Qiang Xue committed
16 17 18
/**
 * This constant defines whether the application should be in debug mode or not. Defaults to false.
 */
w  
Qiang Xue committed
19
defined('YII_DEBUG') or define('YII_DEBUG', false);
Qiang Xue committed
20 21 22 23 24
/**
 * 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
25
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 0);
Qiang Xue committed
26
/**
w  
Qiang Xue committed
27
 * This constant defines the framework installation directory.
Qiang Xue committed
28
 */
w  
Qiang Xue committed
29
defined('YII_PATH') or define('YII_PATH', __DIR__);
Qiang Xue committed
30 31 32 33 34
/**
 * 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
35

Qiang Xue committed
36
/**
w  
Qiang Xue committed
37
 * YiiBase is the core helper class for the Yii framework.
Qiang Xue committed
38
 *
w  
Qiang Xue committed
39
 * Do not use YiiBase directly. Instead, use its child class [[Yii]] where
Qiang Xue committed
40 41 42
 * you can customize methods of YiiBase.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
43
 * @since 2.0
Qiang Xue committed
44 45 46 47 48
 */
class YiiBase
{
	/**
	 * @var array class map used by the Yii autoloading mechanism.
w  
Qiang Xue committed
49 50
	 * 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
51 52
	 * @see import
	 * @see autoload
w  
Qiang Xue committed
53 54 55 56 57 58
	 */
	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
59 60
	 * @see import
	 * @see autoload
w  
Qiang Xue committed
61 62 63
	 */
	public static $classPath = array();
	/**
Qiang Xue committed
64
	 * @var yii\console\Application|yii\web\Application the application instance
Qiang Xue committed
65
	 */
Qiang Xue committed
66
	public static $app;
w  
Qiang Xue committed
67 68
	/**
	 * @var array registered path aliases
Qiang Xue committed
69 70
	 * @see getAlias
	 * @see setAlias
w  
Qiang Xue committed
71 72
	 */
	public static $aliases = array(
w  
Qiang Xue committed
73
		'@yii' => __DIR__,
w  
Qiang Xue committed
74
	);
Qiang Xue committed
75 76
	/**
	 * @var array initial property values that will be applied to objects newly created via [[createObject]].
Qiang Xue committed
77 78
	 * 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
79 80 81
	 *
	 * ~~~
	 * array(
Qiang Xue committed
82
	 *     'Bar' => array(
Qiang Xue committed
83 84 85
	 *         'prop1' => 'value1',
	 *         'prop2' => 'value2',
	 *     ),
Qiang Xue committed
86
	 *     'mycompany\foo\Car' => array(
Qiang Xue committed
87 88 89 90 91 92 93 94 95
	 *         'prop1' => 'value1',
	 *         'prop2' => 'value2',
	 *     ),
	 * )
	 * ~~~
	 *
	 * @see createObject
	 */
	public static $objectConfig = array();
Qiang Xue committed
96

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

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

	/**
	 * 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
113
	 * includes the class file when the class is referenced in the code the first time.
Qiang Xue committed
114
	 *
w  
Qiang Xue committed
115 116 117 118 119
	 * 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
120
	 *
w  
Qiang Xue committed
121 122
	 * 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
123
	 *
w  
Qiang Xue committed
124
	 * To import a class or a directory, one can use either path alias or class name (can be namespaced):
Qiang Xue committed
125
	 *
Qiang Xue committed
126 127
	 *  - `@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
128 129
	 *  - `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
130
	 *
w  
Qiang Xue committed
131
	 * @param string $alias path alias or a simple class name to be imported
Qiang Xue committed
132 133 134 135
	 * @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
136
	 * @throws Exception if the path alias is invalid
Qiang Xue committed
137
	 */
w  
Qiang Xue committed
138
	public static function import($alias, $forceInclude = false)
Qiang Xue committed
139
	{
w  
Qiang Xue committed
140 141 142
		if (isset(self::$_imported[$alias])) {
			return self::$_imported[$alias];
		}
Qiang Xue committed
143

Qiang Xue committed
144 145 146 147 148
		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
149
			if ($forceInclude && static::autoload($alias)) {
w  
Qiang Xue committed
150 151
				self::$_imported[$alias] = $alias;
			}
Qiang Xue committed
152 153 154
			return $alias;
		}

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

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

Qiang Xue committed
162
		$path = static::getAlias(dirname($alias));
Qiang Xue committed
163

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

	/**
w  
Qiang Xue committed
180
	 * Translates a path alias into an actual path.
m  
Qiang Xue committed
181
	 *
w  
Qiang Xue committed
182
	 * The path alias can be either a root alias registered via [[setAlias]] or an
w  
Qiang Xue committed
183 184 185
	 * 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
186
	 *
w  
Qiang Xue committed
187 188
	 * 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
189
	 *
w  
Qiang Xue committed
190 191
	 * Note, this method does not ensure the existence of the resulting path.
	 * @param string $alias alias
Qiang Xue committed
192 193
	 * @param boolean $throwException whether to throw an exception if the given alias is invalid.
	 * If this is false and an invalid alias is given, false will be returned by this method.
Qiang Xue committed
194
	 * @return string|boolean path corresponding to the alias, false if the root alias is not previously registered.
Qiang Xue committed
195
	 * @throws InvalidParamException if the alias is invalid while $throwException is true.
w  
Qiang Xue committed
196
	 * @see setAlias
Qiang Xue committed
197
	 */
Qiang Xue committed
198
	public static function getAlias($alias, $throwException = true)
Qiang Xue committed
199
	{
Qiang Xue committed
200 201 202 203 204 205 206 207 208 209
		if (is_string($alias)) {
			if (isset(self::$aliases[$alias])) {
				return self::$aliases[$alias];
			} elseif ($alias === '' || $alias[0] !== '@') { // not an alias
				return $alias;
			} elseif (($pos = strpos($alias, '/')) !== false || ($pos = strpos($alias, '\\')) !== false) {
				$rootAlias = substr($alias, 0, $pos);
				if (isset(self::$aliases[$rootAlias])) {
					return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos);
				}
Qiang Xue committed
210 211
			}
		}
Qiang Xue committed
212 213 214 215 216
		if ($throwException) {
			throw new InvalidParamException("Invalid path alias: $alias");
		} else {
			return false;
		}
Qiang Xue committed
217 218 219
	}

	/**
w  
Qiang Xue committed
220
	 * Registers a path alias.
m  
Qiang Xue committed
221
	 *
w  
Qiang Xue committed
222 223
	 * 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
224
	 *
w  
Qiang Xue committed
225
	 * Note that this method neither checks the existence of the path nor normalizes the path.
m  
Qiang Xue committed
226 227
	 * Any trailing '/' and '\' characters in the path will be trimmed.
	 *
w  
Qiang Xue committed
228
	 * @param string $alias alias to the path. The alias must start with '@'.
m  
Qiang Xue committed
229 230 231 232 233 234
	 * @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
235
	 *
Qiang Xue committed
236
	 * @throws Exception if $path is an invalid alias
w  
Qiang Xue committed
237
	 * @see getAlias
Qiang Xue committed
238
	 */
w  
Qiang Xue committed
239
	public static function setAlias($alias, $path)
Qiang Xue committed
240
	{
w  
Qiang Xue committed
241
		if ($path === null) {
w  
Qiang Xue committed
242
			unset(self::$aliases[$alias]);
Qiang Xue committed
243
		} elseif (strncmp($path, '@', 1)) {
w  
Qiang Xue committed
244
			self::$aliases[$alias] = rtrim($path, '\\/');
Qiang Xue committed
245
		} else {
Qiang Xue committed
246
			self::$aliases[$alias] = static::getAlias($path);
m  
Qiang Xue committed
247
		}
Qiang Xue committed
248 249 250 251
	}

	/**
	 * Class autoload loader.
w  
Qiang Xue committed
252 253 254 255 256 257 258 259 260 261 262 263 264
	 * 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
265 266
	 * @param string $className class name
	 * @return boolean whether the class has been loaded successfully
Qiang Xue committed
267
	 * @throws Exception if the class file does not exist
Qiang Xue committed
268 269 270
	 */
	public static function autoload($className)
	{
w  
Qiang Xue committed
271
		if (isset(self::$classMap[$className])) {
Qiang Xue committed
272
			include(self::$classMap[$className]);
w  
Qiang Xue committed
273 274 275 276
			return true;
		}

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

Qiang Xue committed
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;
				}
			}
		}

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

		return false;
Qiang Xue committed
313 314
	}

w  
Qiang Xue committed
315
	/**
Qiang Xue committed
316
	 * Creates a new object using the given configuration.
w  
Qiang Xue committed
317
	 *
Qiang Xue committed
318 319 320
	 * 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
321 322 323
	 * the rest of the name-value pairs in the array will be used to initialize
	 * the corresponding object properties.
	 *
Qiang Xue committed
324
	 * The object type can be either a class name or the [[getAlias|alias]] of
w  
Qiang Xue committed
325
	 * the class. For example,
w  
Qiang Xue committed
326
	 *
Qiang Xue committed
327
	 * - `\app\components\GoogleMap`: fully-qualified namespaced class.
Qiang Xue committed
328
	 * - `@app/components/GoogleMap`: an alias
Qiang Xue committed
329 330 331
	 *
	 * Below are some usage examples:
	 *
w  
Qiang Xue committed
332
	 * ~~~
Qiang Xue committed
333
	 * $object = \Yii::createObject('@app/components/GoogleMap');
Qiang Xue committed
334 335
	 * $object = \Yii::createObject(array(
	 *     'class' => '\app\components\GoogleMap',
w  
Qiang Xue committed
336 337 338 339
	 *     'apiKey' => 'xyz',
	 * ));
	 * ~~~
	 *
Qiang Xue committed
340 341 342 343 344 345 346 347 348 349
	 * 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
350
	 *
Qiang Xue committed
351 352
	 * @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
353
	 * @return mixed the created object
Qiang Xue committed
354
	 * @throws InvalidConfigException if the configuration is invalid.
w  
Qiang Xue committed
355
	 */
Qiang Xue committed
356
	public static function createObject($config)
w  
Qiang Xue committed
357
	{
Qiang Xue committed
358 359
		static $reflections = array();

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

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

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

Qiang Xue committed
392
	/**
w  
Qiang Xue committed
393 394 395 396 397
	 * 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
398
	 */
w  
Qiang Xue committed
399
	public static function trace($message, $category = 'application')
Qiang Xue committed
400
	{
w  
Qiang Xue committed
401
		if (YII_DEBUG) {
Qiang Xue committed
402
			self::getLogger()->log($message, Logger::LEVEL_TRACE, $category);
w  
Qiang Xue committed
403
		}
Qiang Xue committed
404 405 406
	}

	/**
w  
Qiang Xue committed
407 408 409 410 411
	 * 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
412
	 */
Qiang Xue committed
413
	public static function error($message, $category = 'application')
Qiang Xue committed
414
	{
Qiang Xue committed
415
		self::getLogger()->log($message, Logger::LEVEL_ERROR, $category);
w  
Qiang Xue committed
416 417 418 419 420 421 422 423 424
	}

	/**
	 * 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
425
	public static function warning($message, $category = 'application')
w  
Qiang Xue committed
426
	{
Qiang Xue committed
427
		self::getLogger()->log($message, Logger::LEVEL_WARNING, $category);
Qiang Xue committed
428 429 430
	}

	/**
w  
Qiang Xue committed
431 432 433 434 435 436
	 * 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
437
	public static function info($message, $category = 'application')
w  
Qiang Xue committed
438
	{
Qiang Xue committed
439
		self::getLogger()->log($message, Logger::LEVEL_INFO, $category);
w  
Qiang Xue committed
440 441 442 443 444 445 446 447 448
	}

	/**
	 * 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
449 450 451 452
	 * // some code to be profiled
	 *     \Yii::beginProfile('block2');
	 *     // some other code to be profiled
	 *     \Yii::endProfile('block2');
w  
Qiang Xue committed
453 454
	 * \Yii::endProfile('block1');
	 * ~~~
Qiang Xue committed
455 456
	 * @param string $token token for the code block
	 * @param string $category the category of this log message
Qiang Xue committed
457 458
	 * @see endProfile
	 */
Qiang Xue committed
459
	public static function beginProfile($token, $category = 'application')
Qiang Xue committed
460
	{
Qiang Xue committed
461
		self::getLogger()->log($token, Logger::LEVEL_PROFILE_BEGIN, $category);
Qiang Xue committed
462 463 464 465
	}

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

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

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

	/**
w  
Qiang Xue committed
499 500
	 * 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
501 502 503 504 505 506 507 508
	 */
	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.
Qiang Xue committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
	 *
	 * The translation will be conducted according to the message category and the target language.
	 * To specify the category of the message, prefix the message with the category name and separate it
	 * with "|". For example, "app|hello world". If the category is not specified, the default category "app"
	 * will be used. The actual message translation is done by a [[\yii\i18n\MessageSource|message source]].
	 *
	 * In case when a translated message has different plural forms (separated by "|"), this method
	 * will also attempt to choose an appropriate one according to a given numeric value which is
	 * specified as the first parameter (indexed by 0) in `$params`.
	 *
	 * For example, if a translated message is "I have an apple.|I have {n} apples.", and the first
	 * parameter is 2, the message returned will be "I have 2 apples.". Note that the placeholder "{n}"
	 * will be replaced with the given number.
	 *
	 * For more details on how plural rules are applied, please refer to:
	 * [[http://www.unicode.org/cldr/charts/supplemental/language_plural_rules.html]]
	 *
	 * @param string $message the message to be translated.
	 * @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
	 * @param string $language the language code (e.g. `en_US`, `en`). If this is null, the current
	 * [[\yii\base\Application::language|application language]] will be used.
	 * @return string the translated message.
Qiang Xue committed
531
	 */
Qiang Xue committed
532
	public static function t($message, $params = array(), $language = null)
Qiang Xue committed
533
	{
Qiang Xue committed
534
		return Yii::$app->getI18N()->translate($message, $params, $language);
Qiang Xue committed
535 536
	}
}