-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialise.php
117 lines (110 loc) · 4.66 KB
/
initialise.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
<?php
declare(strict_types = 1);
use DI\ContainerBuilder;
use Http\Factory\Guzzle\ResponseFactory;
use Http\Factory\Guzzle\StreamFactory;
use Metroapps\NationalRailTimetable\Config\Config;
use Miklcct\RailOpenTimetableData\Models\Date;
use Miklcct\RailOpenTimetableData\Repositories\FixedLinkRepositoryInterface;
use Miklcct\RailOpenTimetableData\Repositories\LocationRepositoryInterface;
use Miklcct\RailOpenTimetableData\Repositories\MongodbFixedLinkRepository;
use Miklcct\RailOpenTimetableData\Repositories\MongodbLocationRepository;
use Miklcct\RailOpenTimetableData\Repositories\MongodbServiceRepositoryFactory;
use Miklcct\RailOpenTimetableData\Repositories\ServiceRepositoryFactoryInterface;
use Miklcct\ThinPhpApp\Response\ViewResponseFactory;
use Miklcct\ThinPhpApp\Response\ViewResponseFactoryInterface;
use MongoDB\Client;
use MongoDB\Database;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
use Symfony\Component\Cache\Psr16Cache;
use Teapot\HttpException;
use Whoops\Handler\Handler;
use Whoops\Handler\PlainTextHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
use function DI\autowire;
use function Miklcct\RailOpenTimetableData\get_generated;
/**
* Return the 2 databases defined in the application
*
* The database at index 0 is the one currently used, while the one at index 1 is available for importing new data
*
* @return Database[]
*/
function get_databases() : array {
$container = get_container();
/** @var Config $config */
$config = $container->get(Config::class);
$databases = array_map(
static fn (string $name) => $container->get(Client::class)->selectDatabase($name)
, [$config->databaseName, $config->alternativeDatabaseName]
);
/** @var (Date|null)[] $generated_dates */
$generated_dates = array_map(
get_generated(...)
, $databases
);
if ($generated_dates[0]?->toDateTimeImmutable() < $generated_dates[1]?->toDateTimeImmutable()) {
return [$databases[1], $databases[0]];
}
return $databases;
}
function get_container() : ContainerInterface {
static $container;
if ($container === null) {
$container = (new ContainerBuilder())->addDefinitions(
[
Client::class => static function(ContainerInterface $container) : Client {
$config = $container->get(Config::class);
return new Client(uri: $config->mongodbUri ?? 'mongodb://127.0.0.1/', uriOptions: $config->mongodbUriOptions ?? [], driverOptions: ['typeMap' => ['array' => 'array']]);
},
Config::class => static fn() : Config => require __DIR__ . '/config.php',
Database::class => static function() {
return get_databases()[0];
},
CacheInterface::class =>
static fn() => new Psr16Cache(new PhpFilesAdapter('', 0, __DIR__ . '/var/cache', true)),
LocationRepositoryInterface::class => autowire(MongodbLocationRepository::class),
ServiceRepositoryFactoryInterface::class =>
static fn(ContainerInterface $container) => new MongodbServiceRepositoryFactory($container->get(Database::class), $container->get(CacheInterface::class)),
FixedLinkRepositoryInterface::class => autowire(MongodbFixedLinkRepository::class),
ViewResponseFactoryInterface::class => autowire(ViewResponseFactory::class),
ResponseFactoryInterface::class => autowire(ResponseFactory::class),
StreamFactoryInterface::class => autowire(StreamFactory::class),
]
)
->build();
}
return $container;
}
function is_development() : bool {
return $_SERVER['SERVER_NAME'] === 'gbtt.localhost';
}
require_once __DIR__ . '/vendor/autoload.php';
$whoops = new Run;
if (PHP_SAPI === 'cli') {
$whoops->pushHandler(new PlainTextHandler());
} else {
$pretty_page_handler = new PrettyPageHandler;
$pretty_page_handler->setEditor(PrettyPageHandler::EDITOR_PHPSTORM);
$whoops->pushHandler($pretty_page_handler);
$whoops->pushHandler(
new class extends Handler {
public function handle() : void {
$exception = $this->getException();
if ($exception instanceof HttpException) {
$this->getRun()->sendHttpCode($exception->getCode());
}
}
}
);
}
$whoops->register();
set_time_limit(120);
ini_set('memory_limit', '4G');
date_default_timezone_set('Europe/London');
umask(0o002);