-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.inc.php
62 lines (47 loc) · 1.77 KB
/
helpers.inc.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
<?php
if (!defined('IN_INDEX')) { exit("This file can only be included in index.php."); }
function get_domain(): string {
$domena = preg_replace('/[^a-zA-Z0-9\.]/', '', $_SERVER['HTTP_HOST']);
return $domena;
}
class DB {
private static $dbh = null;
public static function getInstance(): PDO {
if (!self::$dbh) {
try {
self::$dbh = new PDO(
'mysql:host=' . CONFIG['db_host'] . ';dbname=' . CONFIG['db_name'] . ';charset=utf8mb4',
CONFIG['db_user'],
CONFIG['db_password']
);
self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit("Cannot connect to the database: " . $e->getMessage());
}
}
return self::$dbh;
}
}
class TwigHelper {
private static $twig = null;
private static $msg = [];
public static function getInstance(): \Twig\Environment {
if (!self::$twig) {
$twig_loader = new \Twig\Loader\FilesystemLoader('templates');
self::$twig = new \Twig\Environment($twig_loader);
}
return self::$twig;
}
public static function addMsg(string $text, string $type): void {
self::$msg[] = [
'text' => $text,
'type' => $type
];
}
public static function getMsg(): array {
return self::$msg;
}
}
TwigHelper::getInstance()->addGlobal('CONFIG', CONFIG);
TwigHelper::getInstance()->addFunction(new \Twig\TwigFunction('get_domain', 'get_domain'));
TwigHelper::getInstance()->addFunction(new \Twig\TwigFunction('get_msg', 'TwigHelper::getMsg'));