xref: /webtrees/app/Webtrees.php (revision bb1ec7dcf21066b641b778b9231f2ee56ad8cc1d)
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\SecurityHeaders;
36use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
37use Fisharebest\Webtrees\Http\Middleware\UseCache;
38use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
39use Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
40use Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
41use Fisharebest\Webtrees\Http\Middleware\UseLanguage;
42use Fisharebest\Webtrees\Http\Middleware\UseSession;
43use Fisharebest\Webtrees\Http\Middleware\UseTheme;
44use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
45use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
46use Nyholm\Psr7\Factory\Psr17Factory;
47use Psr\Http\Message\ResponseFactoryInterface;
48use Psr\Http\Message\ServerRequestFactoryInterface;
49use Psr\Http\Message\StreamFactoryInterface;
50use Psr\Http\Message\UploadedFileFactoryInterface;
51use Psr\Http\Message\UriFactoryInterface;
52
53use function app;
54use function error_reporting;
55use function set_error_handler;
56
57/**
58 * Definitions for the webtrees application.
59 */
60class Webtrees
61{
62    // The root folder of this installation
63    public const ROOT_DIR = __DIR__ . '/../';
64
65    // This is the location of system data, such as temporary and cache files.
66    // The system files are always in this location.
67    // It is also the default location of user data, such as media and GEDCOM files.
68    // The user files could be anywhere supported by Flysystem.
69    public const DATA_DIR  = self::ROOT_DIR . 'data/';
70
71    // Location of the file containing the database connection details.
72    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
73
74    // Location of the file that triggers maintenance mode.
75    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
76
77    // Location of our modules.
78    public const MODULES_PATH = 'modules_v4/';
79    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
80
81    // Enable debugging on development builds.
82    public const DEBUG = self::STABILITY !== '';
83
84    // We want to know about all PHP errors during development, and fewer in production.
85    public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL;
86
87    // The name of the application.
88    public const NAME = 'webtrees';
89
90    // Required version of database tables/columns/indexes/etc.
91    public const SCHEMA_VERSION = 43;
92
93    // e.g. "dev", "alpha", "beta", etc.
94    public const STABILITY = 'dev';
95
96    // Version number
97    public const VERSION = '2.0.2' . (self::STABILITY === '' ? '' : '-') . self::STABILITY;
98
99    // Project website.
100    public const URL = 'https://www.webtrees.net/';
101
102    // Project website.
103    public const GEDCOM_PDF = 'https://webtrees.github.io/downloads/gedcom-551.pdf';
104
105    private const MIDDLEWARE = [
106        PhpEnvironment::class,
107        EmitResponse::class,
108        SecurityHeaders::class,
109        ReadConfigIni::class,
110        BaseUrl::class,
111        HandleExceptions::class,
112        ClientIp::class,
113        UseDatabase::class,
114        UseDebugbar::class,
115        UpdateDatabaseSchema::class,
116        UseCache::class,
117        UseFilesystem::class,
118        UseSession::class,
119        UseLanguage::class,
120        CheckForMaintenanceMode::class,
121        UseTheme::class,
122        DoHousekeeping::class,
123        UseTransaction::class,
124        LoadRoutes::class,
125        BootModules::class,
126        Router::class,
127        NoRouteFound::class,
128    ];
129
130    /**
131     * Initialise the application.
132     *
133     * @return void
134     */
135    public function bootstrap(): void
136    {
137        // Show all errors and warnings in development, fewer in production.
138        error_reporting(self::ERROR_REPORTING);
139
140        set_error_handler($this->phpErrorHandler());
141    }
142
143    /**
144     * An error handler that can be passed to set_error_handler().
145     *
146     * @return Closure
147     */
148    private function phpErrorHandler(): Closure
149    {
150        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
151            // Ignore errors that are silenced with '@'
152            if (error_reporting() & $errno) {
153                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
154            }
155
156            return true;
157        };
158    }
159
160    /**
161     * We can use any PSR-7 / PSR-17 compatible message factory.
162     *
163     * @return void
164     */
165    public function selectMessageFactory(): void
166    {
167        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
168        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
169        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
170        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
171        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
172    }
173
174    /**
175     * The webtrees application is built from middleware.
176     *
177     * @return string[]
178     */
179    public function middleware(): array
180    {
181        return self::MIDDLEWARE;
182    }
183}
184