-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
164 lines (148 loc) · 3.29 KB
/
code.power
File metadata and controls
164 lines (148 loc) · 3.29 KB
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
/**
* The Table Class.
*
* @var Table|null
* @since 3.2.2
*/
protected ?Table $table;
/**
* The Schema Class.
*
* @var Schema|null
* @since 3.2.2
*/
protected ?Schema $schema;
/**
* Application object.
*
* @since 3.2.2
**/
protected $app;
/**
* Constructor.
*
* @param Schema|null $schema The Schema Class.
* @param Table|null $table The Table Class.
* @param $app The app object.
*
* @throws \Exception
* @since 3.2.2
*/
public function __construct(?Schema $schema = null, ?Table $table = null, $app = null)
{
$this->schema = $schema;
$this->table = $table;
$this->app = $app ?: Factory::getApplication();
// Validate classes are set
// Since this class is often called from outside a container
$this->initializeInstances();
// I don't care! I have more important thing to do, maybe later... (last updated in 1983 ;)
}
/**
* Make sure that the database schema is up-to-date.
*
* @return void
* @since 3.2.2
*/
public function run(): void
{
if ($this->schema === null)
{
$this->app->enqueueMessage('We failed to find/load the Schema class', 'warning');
return;
}
// try to load the update the tables with the schema class
try
{
$messages = $this->schema->update();
}
catch (\Exception $e)
{
$this->app->enqueueMessage($e->getMessage(), 'warning');
return;
}
foreach ($messages as $message)
{
$this->app->enqueueMessage($message, 'message');
}
}
/**
* Initialize the needed class instances if needed
*
* @return void
* @since 3.2.2
*/
protected function initializeInstances(): void
{
if ($this->schema !== null)
{
return;
}
if ($this->table === null)
{
$this->setTableInstance();
}
$this->setSchemaInstance();
}
/**
* set the schema class instance
*
* @return void
* @since 3.2.2
*/
protected function setSchemaInstance(): void
{
// make sure the class is loaded
if (ClassHelper::exists(
$this->getSchemaClass(), $this->getCode(), $this->getPowerPath()
))
{
// instantiate the schema class
$this->schema = new ($this->getSchemaClass())($this->table);
}
}
/**
* set the table class instance
*
* @return void
* @since 3.2.2
*/
protected function setTableInstance(): void
{
// make sure the class is loaded
if (ClassHelper::exists(
$this->getTableClass(), $this->getCode(), $this->getPowerPath()
))
{
// instantiate the table class
$this->table = new ($this->getTableClass())();
}
}
/**
* Get the targeted component code
*
* @return string
* @since 3.2.2
*/
abstract protected function getCode(): string;
/**
* Get the targeted component power path
*
* @return string
* @since 3.2.2
*/
abstract protected function getPowerPath(): string;
/**
* Get the fully qualified name of the schema class.
*
* @return string
* @since 3.2.2
*/
abstract protected function getSchemaClass(): string;
/**
* Get the fully qualified name of the table class.
*
* @return string
* @since 3.2.2
*/
abstract protected function getTableClass(): string;