Completed
Push — master ( 2eca18...281374 )
by Jacob
36s
created

bootstrap.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
$startTime = microtime(true);
4
$startMemory = memory_get_usage();
5
ini_set('display_errors', 0);
6
7
8
require_once __DIR__ . '/vendor/autoload.php';
9
10
$container = new Pimple\Container();
11
12
13
// load the config for the application
14
$config_path = __DIR__ . '/config.json';
15
16
$handle = @fopen($config_path, 'r');
17
if ($handle === false) {
18
    throw new RuntimeException("Could not load config");
19
}
20
$config = fread($handle, filesize($config_path));
21
fclose($handle);
22
23
$config = json_decode($config);
24
$last_json_error = json_last_error();
25
if ($last_json_error !== JSON_ERROR_NONE) {
26
    throw new RuntimeException("Could not parse config - JSON error detected");
27
}
28
$container['config'] = $config;
29
30
// timezones are fun
31
date_default_timezone_set('America/Phoenix'); // todo - belongs in configuration
32
$container['default_timezone'] = function ($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    return new DateTimeZone('America/Phoenix');
34
};
35
36
37
// configure the db connections holder
38
$db_connections = new Aura\Sql\ConnectionLocator();
39
$db_connections->setDefault(function () use ($config) {
40
    $connection = $config->database->slave;
41
    return new Aura\Sql\ExtendedPdo(
42
        "mysql:host={$connection->host}",
43
        $connection->user,
44
        $connection->password
45
    );
46
});
47
$db_connections->setWrite('master', function () use ($config) {
48
    $connection = $config->database->master;
49
    return new Aura\Sql\ExtendedPdo(
50
        "mysql:host={$connection->host}",
51
        $connection->user,
52
        $connection->password
53
    );
54
});
55
$db_connections->setRead('slave', function () use ($config) {
56
    $connection = $config->database->slave;
57
    $pdo = new Aura\Sql\ExtendedPdo(
58
        "mysql:host={$connection->host}",
59
        $connection->user,
60
        $connection->password
61
    );
62
63
    $profiler = new Aura\Sql\Profiler();
64
    $profiler->setActive(true);
65
    $pdo->setProfiler($profiler);
66
67
    return $pdo;
68
});
69
$container['db_connection_locator'] = $db_connections;
70
71
72
// configure the comment service connection
73
$container['comment_service_api'] = function () use ($config) {
74
    $configuration = (new Jacobemerick\CommentService\Configuration())
75
        ->setUsername($config->comments->user)
76
        ->setPassword($config->comments->password)
77
        ->addDefaultHeader('Content-Type', 'application/json')
78
        ->setHost($config->comments->host)
79
        ->setCurlTimeout($config->comments->timeout);
80
81
    $client = new Jacobemerick\CommentService\ApiClient($configuration);
82
    return new Jacobemerick\CommentService\Api\DefaultApi($client);
83
};
84
85
86
// setup mail handler
87
$container['mail'] = $container->factory(function ($c) {
88
    return (new Jacobemerick\Archangel\Archangel())->setLogger($c['logger']);
89
});
90
91
92
// setup the logger
93
$container['setup_logger'] = $container->protect(function ($name) use ($container) {
94
    $logger = new Monolog\Logger($name);
95
96
    $logPath = __DIR__ . "/logs/{$name}.log";
97
    $streamHandler = new Monolog\Handler\StreamHandler($logPath, Monolog\Logger::INFO);
98
    // todo - make this more useful
99
    // $streamHandler->setFormatter(
100
    //     new Monolog\Formatter\LineFormatter("[%datetime%] %channel%.%level_name%: %message%\n")
101
    // );
102
    $logger->pushHandler($streamHandler);
103
104
    Monolog\ErrorHandler::register($logger);
105
    $container['logger'] = $logger;
106
});
107
108
109
// adds profiler
110
$console = new Particletree\Pqp\Console();
111
$profiler = new Particletree\Pqp\PhpQuickProfiler($startTime);
112
$profiler->setConsole($console);
113
$container['console'] = $console;
114
$container['profiler'] = $profiler;
115
$container['console']->logMemory($startMemory, 'PHP - Pre-bootstrap memory', true);
116
117
118
// sets up logger, modifes with profiler handler
119
$pqpHandler = new Jacobemerick\MonologPqp\PqpHandler($container['console']);
120
$container['setup_logger']($namespace);
121
$container['logger']->pushHandler($pqpHandler);
122
123
124
// sets up shutdown function to display profiler
125
register_shutdown_function(function () use ($container) {
126
    if (
127
        !isset($_COOKIE['debugger']) ||
128
        $_COOKIE['debugger'] != 'display'
129
    ) {
130
        return;
131
    }
132
133
    $dbProfiles = $container['db_connection_locator']
134
        ->getRead()
135
        ->getProfiler()
136
        ->getProfiles();
137
    $dbProfiles = array_filter($dbProfiles, function ($profile) {
138
        return $profile['function'] == 'perform';
139
    });
140
    $dbProfiles = array_map(function ($profile) {
141
        return [
142
            'sql' => trim(preg_replace('/\s+/', ' ', $profile['statement'])),
143
            'parameters' => $profile['bind_values'],
144
            'time' => $profile['duration'],
145
        ];
146
    }, $dbProfiles);
147
    $container['profiler']->setProfiledQueries($dbProfiles);
148
    $container['profiler']->setDisplay(new Particletree\Pqp\Display());
149
    $container['profiler']->display($container['db_connection_locator']->getRead());
150
});
151
152
$container['console']->logMemory(null, 'PHP - Post-boostrap memory');
153
$container['console']->logSpeed('Post-bootstrap time');
154