xref: /webtrees/app/Webtrees.php (revision cd51dbdfbd6a6a2e8b78479e839819cddc801c34)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use ErrorException;
24use Fisharebest\Webtrees\Http\Middleware\BootModules;
25use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
26use Fisharebest\Webtrees\Http\Middleware\ClientIp;
27use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
28use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
29use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
30use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
31use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
32use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
33use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
34use Fisharebest\Webtrees\Http\Middleware\Router;
35use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
36use Fisharebest\Webtrees\Http\Middleware\UseCache;
37use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
38use Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
39use Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
40use Fisharebest\Webtrees\Http\Middleware\UseLanguage;
41use Fisharebest\Webtrees\Http\Middleware\UseSession;
42use Fisharebest\Webtrees\Http\Middleware\UseTheme;
43use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
44use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
45use Nyholm\Psr7\Factory\Psr17Factory;
46use Psr\Http\Message\ResponseFactoryInterface;
47use Psr\Http\Message\ServerRequestFactoryInterface;
48use Psr\Http\Message\StreamFactoryInterface;
49use Psr\Http\Message\UploadedFileFactoryInterface;
50use Psr\Http\Message\UriFactoryInterface;
51
52use function app;
53use function error_reporting;
54use function set_error_handler;
55
56/**
57 * Definitions for the webtrees application.
58 */
59class Webtrees
60{
61    // The root folder of this installation
62    public const ROOT_DIR = __DIR__ . '/../';
63
64    // This is the location of system data, such as temporary and cache files.
65    // The system files are always in this location.
66    // It is also the default location of user data, such as media and GEDCOM files.
67    // The user files could be anywhere supported by Flysystem.
68    public const DATA_DIR  = self::ROOT_DIR . 'data/';
69
70    // Location of the file containing the database connection details.
71    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
72
73    // Location of the file that triggers maintenance mode.
74    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
75
76    // Location of our modules.
77    public const MODULES_PATH = 'modules_v4/';
78    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
79
80    // Enable debugging on development builds.
81    public const DEBUG = self::STABILITY !== '';
82
83    // We want to know about all PHP errors during development, and fewer in production.
84    public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL;
85
86    // The name of the application.
87    public const NAME = 'webtrees';
88
89    // Required version of database tables/columns/indexes/etc.
90    public const SCHEMA_VERSION = 43;
91
92    // e.g. "dev", "alpha", "beta", etc.
93    public const STABILITY = 'dev';
94
95    // Version number
96    public const VERSION = '2.0.2' . (self::STABILITY === '' ? '' : '-') . self::STABILITY;
97
98    // Project website.
99    public const URL = 'https://www.webtrees.net/';
100
101    private const MIDDLEWARE = [
102        PhpEnvironment::class,
103        EmitResponse::class,
104        ReadConfigIni::class,
105        BaseUrl::class,
106        HandleExceptions::class,
107        ClientIp::class,
108        UseDatabase::class,
109        UseDebugbar::class,
110        UpdateDatabaseSchema::class,
111        UseCache::class,
112        UseFilesystem::class,
113        UseSession::class,
114        UseLanguage::class,
115        CheckForMaintenanceMode::class,
116        UseTheme::class,
117        DoHousekeeping::class,
118        UseTransaction::class,
119        LoadRoutes::class,
120        BootModules::class,
121        Router::class,
122        NoRouteFound::class,
123    ];
124
125    /**
126     * Initialise the application.
127     *
128     * @return void
129     */
130    public function bootstrap(): void
131    {
132        // Show all errors and warnings in development, fewer in production.
133        error_reporting(self::ERROR_REPORTING);
134
135        set_error_handler($this->phpErrorHandler());
136    }
137
138    /**
139     * An error handler that can be passed to set_error_handler().
140     *
141     * @return Closure
142     */
143    private function phpErrorHandler(): Closure
144    {
145        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
146            // Ignore errors that are silenced with '@'
147            if (error_reporting() & $errno) {
148                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
149            }
150
151            return true;
152        };
153    }
154
155    /**
156     * We can use any PSR-7 / PSR-17 compatible message factory.
157     *
158     * @return void
159     */
160    public function selectMessageFactory(): void
161    {
162        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
163        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
164        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
165        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
166        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
167    }
168
169    /**
170     * The webtrees application is built from middleware.
171     *
172     * @return string[]
173     */
174    public function middleware(): array
175    {
176        return self::MIDDLEWARE;
177    }
178}
179