-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
126 lines (107 loc) · 3.55 KB
/
Application.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
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license /~https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Applications;
use Exception;
use Opulence\Applications\Tasks\Dispatchers\ITaskDispatcher;
use Opulence\Applications\Tasks\TaskTypes;
/**
* Defines an application
* @deprecated 1.1.0 This class will be removed
*/
class Application
{
/** @var string The current Opulence version */
private static $opulenceVersion = '1.2.0';
/** @var ITaskDispatcher The task dispatcher */
private $taskDispatcher = null;
/** @var string The version of the application */
private $version = '';
/** @var bool Whether or not the application is currently running */
private $isRunning = false;
/**
* @param ITaskDispatcher $taskDispatcher The task dispatcher
* @param ?string $version The version of the application
* @deprecated 1.1.0 The $version parameter will soon not be accepted
*/
public function __construct(ITaskDispatcher $taskDispatcher, string $version = null)
{
$this->taskDispatcher = $taskDispatcher;
$this->version = $version ?? self::$opulenceVersion;
}
/**
* @return string
* @deprecated 1.1.0 This method will be removed
*/
public function getVersion() : string
{
return $this->version;
}
/**
* @return bool
* @deprecated 1.1.0 This method will be removed
*/
public function isRunning() : bool
{
return $this->isRunning;
}
/**
* Shuts down this application
*
* @param ?callable $shutdownTask The task to perform on shutdown
* @return mixed|null The return value of the task if there was one, otherwise null
* @throws Exception Thrown if there was an error shutting down the application
* @deprecated 1.1.0 This method will be removed
*/
public function shutDown(callable $shutdownTask = null)
{
$taskReturnValue = null;
// Don't shut down a shutdown application
if ($this->isRunning) {
try {
$this->taskDispatcher->dispatch(TaskTypes::PRE_SHUTDOWN);
$this->isRunning = false;
if ($shutdownTask !== null) {
$taskReturnValue = $shutdownTask();
}
$this->taskDispatcher->dispatch(TaskTypes::POST_SHUTDOWN);
} catch (Exception $ex) {
$this->isRunning = false;
throw $ex;
}
}
return $taskReturnValue;
}
/**
* Starts this application
*
* @param ?callable $startTask The task to perform on startup
* @return mixed|null The return value of the task if there was one, otherwise null
* @throws Exception Thrown if there was a problem starting the application
* @deprecated 1.1.0 This method will be removed
*/
public function start(callable $startTask = null)
{
$taskReturnValue = null;
// Don't start a running application
if (!$this->isRunning) {
try {
$this->taskDispatcher->dispatch(TaskTypes::PRE_START);
$this->isRunning = true;
if ($startTask !== null) {
$taskReturnValue = $startTask();
}
$this->taskDispatcher->dispatch(TaskTypes::POST_START);
} catch (Exception $ex) {
$this->shutDown();
throw $ex;
}
}
return $taskReturnValue;
}
}