-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
105 lines (85 loc) · 2.29 KB
/
helpers.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
<?php
if (! defined('EXECUTION_ALLOWED')) {
exit('Unauthorized access denied.');
}
use League\Plates\Engine;
use Medoo\Medoo;
use PHPMailer\PHPMailer\PHPMailer;
function create_mailer(): PHPMailer
{
$mailer = new PHPMailer(true);
$mailer->isSMTP();
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
if (SMTP_USERNAME && SMTP_PASSWORD) {
$mailer->SMTPAuth = true;
$mailer->Username = SMTP_USERNAME;
$mailer->Password = SMTP_PASSWORD;
}
$mailer->SMTPDebug = defined('DEBUG_ENABLED');
$mailer->SMTPSecure = SMTP_ENCRYPTION;
return $mailer;
}
function get_or_create_db(): Medoo
{
static $database;
if (empty($database)) {
$database = new Medoo([
'type' => 'mysql',
'host' => DB_HOST,
'port' => DB_PORT,
'username' => DB_USERNAME,
'password' => DB_PASSWORD,
'database' => DB_NAME,
]);
}
return $database;
}
function migrate_database(): array
{
$db = get_or_create_db();
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `migrations` (
`name` VARCHAR(255),
PRIMARY KEY (`name`)
) ENGINE=InnoDB;
SQL;
$db->exec($sql);
$migrations = [];
foreach (new DirectoryIterator(__DIR__.'/migrations') as $file) {
$filename = $file->getFilename();
if (strpos($filename, '.') !== 0) {
$migrations[] = $filename;
}
}
sort($migrations);
$existing = array_column($db->select('migrations', ['name']), 'name');
$missing = [];
foreach ($migrations as $migration) {
if (in_array($migration, $existing)) {
continue;
}
$sql = file_get_contents(__DIR__.'/migrations/'.$migration);
$db->exec($sql);
$db->insert('migrations', ['name' => $migration]);
$missing[] = $migration;
}
return $missing;
}
function render_template(string $name, array $data = []): string
{
$engine = new Engine(__DIR__.'/templates');
return $engine->render($name, $data);
}
/**
* @param array|object $data
*/
function respond_with_json($data): void
{
header('content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
}
function respond_with_template(string $name, array $data = []): void
{
echo render_template($name, $data);
}