/
bootstrap.php
153 lines (123 loc) · 4.78 KB
/
bootstrap.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
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
<?php
$startTime = microtime(true);
$startMemory = memory_get_usage();
ini_set('display_errors', 0);
require_once __DIR__ . '/vendor/autoload.php';
$container = new Pimple\Container();
// load the config for the application
$config_path = __DIR__ . '/config.json';
$handle = @fopen($config_path, 'r');
if ($handle === false) {
throw new RuntimeException("Could not load config");
}
$config = fread($handle, filesize($config_path));
fclose($handle);
$config = json_decode($config);
$last_json_error = json_last_error();
if ($last_json_error !== JSON_ERROR_NONE) {
throw new RuntimeException("Could not parse config - JSON error detected");
}
$container['config'] = $config;
// timezones are fun
date_default_timezone_set('America/Phoenix'); // todo - belongs in configuration
$container['default_timezone'] = function ($c) {
return new DateTimeZone('America/Phoenix');
};
// configure the db connections holder
$db_connections = new Aura\Sql\ConnectionLocator();
$db_connections->setDefault(function () use ($config) {
$connection = $config->database->slave;
return new Aura\Sql\ExtendedPdo(
"mysql:host={$connection->host}",
$connection->user,
$connection->password
);
});
$db_connections->setWrite('master', function () use ($config) {
$connection = $config->database->master;
return new Aura\Sql\ExtendedPdo(
"mysql:host={$connection->host}",
$connection->user,
$connection->password
);
});
$db_connections->setRead('slave', function () use ($config) {
$connection = $config->database->slave;
$pdo = new Aura\Sql\ExtendedPdo(
"mysql:host={$connection->host}",
$connection->user,
$connection->password
);
$profiler = new Aura\Sql\Profiler();
$profiler->setActive(true);
$pdo->setProfiler($profiler);
return $pdo;
});
$container['db_connection_locator'] = $db_connections;
// configure the comment service connection
$container['comment_service_api'] = function () use ($config) {
$configuration = (new Jacobemerick\CommentService\Configuration())
->setUsername($config->comments->user)
->setPassword($config->comments->password)
->addDefaultHeader('Content-Type', 'application/json')
->setHost($config->comments->host)
->setCurlTimeout($config->comments->timeout);
$client = new Jacobemerick\CommentService\ApiClient($configuration);
return new Jacobemerick\CommentService\Api\DefaultApi($client);
};
// setup mail handler
$container['mail'] = $container->factory(function ($c) {
return (new Jacobemerick\Archangel\Archangel())->setLogger($c['logger']);
});
// setup the logger
$container['setup_logger'] = $container->protect(function ($name) use ($container) {
$logger = new Monolog\Logger($name);
$logPath = __DIR__ . "/logs/{$name}.log";
$streamHandler = new Monolog\Handler\StreamHandler($logPath, Monolog\Logger::INFO);
// todo - make this more useful
// $streamHandler->setFormatter(
// new Monolog\Formatter\LineFormatter("[%datetime%] %channel%.%level_name%: %message%\n")
// );
$logger->pushHandler($streamHandler);
Monolog\ErrorHandler::register($logger);
$container['logger'] = $logger;
});
// adds profiler
$console = new Particletree\Pqp\Console();
$profiler = new Particletree\Pqp\PhpQuickProfiler($startTime);
$profiler->setConsole($console);
$container['console'] = $console;
$container['profiler'] = $profiler;
$container['console']->logMemory($startMemory, 'PHP - Pre-bootstrap memory', true);
// sets up logger, modifes with profiler handler
$pqpHandler = new Jacobemerick\MonologPqp\PqpHandler($container['console']);
$container['setup_logger']($namespace);
$container['logger']->pushHandler($pqpHandler);
// sets up shutdown function to display profiler
register_shutdown_function(function () use ($container) {
if (
!isset($_COOKIE['debugger']) ||
$_COOKIE['debugger'] != 'display'
) {
return;
}
$dbProfiles = $container['db_connection_locator']
->getRead()
->getProfiler()
->getProfiles();
$dbProfiles = array_filter($dbProfiles, function ($profile) {
return $profile['function'] == 'perform';
});
$dbProfiles = array_map(function ($profile) {
return [
'sql' => trim(preg_replace('/\s+/', ' ', $profile['statement'])),
'parameters' => $profile['bind_values'],
'time' => $profile['duration'],
];
}, $dbProfiles);
$container['profiler']->setProfiledQueries($dbProfiles);
$container['profiler']->setDisplay(new Particletree\Pqp\Display());
$container['profiler']->display($container['db_connection_locator']->getRead());
});
$container['console']->logMemory(null, 'PHP - Post-boostrap memory');
$container['console']->logSpeed('Post-bootstrap time');