Passed
Push — master ( 86fa88...ed1da9 )
by teng
01:20
created

AbstractApplication::getInstance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright (C) 2019 pokerapp.cn
4
 * @license   https://pokerapp.cn/license
5
 */
6
namespace poker\application;
7
8
use poker\container\Container;
9
10
use function rtrim;
11
use function dirname;
12
use function microtime;
13
14
/**
15
 * 抽象应用程序
16
 *
17
 * @author Levine <[email protected]>
18
 */
19
abstract class AbstractApplication
20
{
21
	/**
22
	 * 应用程序实例
23
	 *
24
	 * @var \poker\application\AbstractApplication
25
	 */
26
	protected static $instance;
27
28
	/**
29
	 * 应用程序开始时间
30
	 *
31
	 * @var float
32
	 */
33
	protected $startTime;
34
35
	/**
36
	 * 应用程序根路径地址
37
	 *
38
	 * @var string
39
	 */
40
	protected $rootPath;
41
42
	/**
43
	 * 容器实例
44
	 *
45
	 * @var \poker\container\Container
46
	 */
47
	protected $container;
48
49
	/**
50
	 * 构造函数
51
	 *
52
	 * @param string $rootPath 应用程序根路径地址
53
	 */
54
	public function __construct(string $rootPath)
55
	{
56
		$this->startTime = microtime(true);
57
		$this->rootPath  = rtrim($rootPath, '\\/');
58
		$this->container = Container::getInstance();
59
60
		// 应用程序初始化
61
		$this->initialize();
62
	}
63
64
	/**
65
	 * 返回应用程序实例
66
	 *
67
	 * @param  string                                 $rootPath 应用程序根路径地址
68
	 * @return \poker\application\AbstractApplication
69
	 */
70
	public static function getInstance(string $rootPath = null): AbstractApplication
71
	{
72
		if (null === static::$instance) {
73
			static::$instance = new static($rootPath ?: dirname($_SERVER['DOCUMENT_ROOT']));
74
		}
75
76
		return static::$instance;
77
	}
78
79
	/**
80
	 * 应用程序初始化
81
	 */
82
	protected function initialize(): void
83
	{
84
		$this->container->registerInstance([AbstractApplication::class, 'app'], $this);
85
	}
86
87
	/**
88
	 * 运行应用程序
89
	 */
90
	abstract public function run();
91
}
92