diff options
| -rw-r--r-- | app/Database.php | 90 | ||||
| -rw-r--r-- | app/Http/Middleware/CheckForMaintenanceMode.php | 4 | ||||
| -rw-r--r-- | app/Http/Middleware/ReadConfigIni.php | 68 | ||||
| -rw-r--r-- | app/Http/Middleware/UseDatabase.php | 65 | ||||
| -rw-r--r-- | app/Http/Middleware/UseDebugbar.php | 3 | ||||
| -rw-r--r-- | app/Webtrees.php | 4 | ||||
| -rw-r--r-- | tests/TestCase.php | 8 | ||||
| -rw-r--r-- | tests/app/DatabaseTest.php | 34 |
8 files changed, 125 insertions, 151 deletions
diff --git a/app/Database.php b/app/Database.php deleted file mode 100644 index d2d7022a80..0000000000 --- a/app/Database.php +++ /dev/null @@ -1,90 +0,0 @@ -<?php -/** - * webtrees: online genealogy - * Copyright (C) 2019 webtrees development team - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTASET NAMES 'utf8' COLLATE 'utf8_unicode_ci'LITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -declare(strict_types=1); - -namespace Fisharebest\Webtrees; - -use Illuminate\Database\Capsule\Manager as DB; -use Illuminate\Database\Query\Builder; - -/** - * Extend PHP's native PDO class. - */ -class Database -{ - /** - * Implement the singleton pattern, using a static accessor. - * - * @param string[] $config - * - * @return void - */ - public static function connect(array $config): void - { - $config['dbtype'] = $config['dbtype'] ?? 'mysql'; - - if ($config['dbtype'] === 'sqlite') { - $config['dbname'] = Webtrees::ROOT_DIR . 'data/' . $config['dbname'] . '.sqlite'; - } - - $capsule = new DB(); - $capsule->addConnection([ - 'driver' => $config['dbtype'], - 'host' => $config['dbhost'], - 'port' => $config['dbport'], - 'database' => $config['dbname'], - 'username' => $config['dbuser'], - 'password' => $config['dbpass'], - 'prefix' => $config['tblpfx'], - 'prefix_indexes' => true, - // For MySQL - 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', - 'timezone' => '+00:00', - 'engine' => 'InnoDB', - 'modes' => [ - 'ANSI', - 'STRICT_TRANS_TABLES', - 'NO_ZERO_IN_DATE', - 'NO_ZERO_DATE', - 'ERROR_FOR_DIVISION_BY_ZERO', - ], - // For SQLite - 'foreign_key_constraints' => true, - ]); - $capsule->setAsGlobal(); - - // Add logging/debugging. - DebugBar::initPDO($capsule->getConnection()->getPdo()); - - self::registerMacros(); - } - - /** - * Register macros to help search for substrings - * - * @return void - */ - public static function registerMacros(): void - { - Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { - $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); - - /** @psalm-suppress InvalidScope - The scope of $this depends on its use, not its definition */ - return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); - }); - } -} diff --git a/app/Http/Middleware/CheckForMaintenanceMode.php b/app/Http/Middleware/CheckForMaintenanceMode.php index 62043b8b0d..0c8d7516d9 100644 --- a/app/Http/Middleware/CheckForMaintenanceMode.php +++ b/app/Http/Middleware/CheckForMaintenanceMode.php @@ -27,7 +27,7 @@ use Psr\Http\Server\RequestHandlerInterface; /** * Middleware to check whether the site is offline. */ -class CheckForMaintenanceMode implements MiddlewareInterface +class CheckForMaintenanceMode implements MiddlewareInterface, StatusCodeInterface { /** * @param ServerRequestInterface $request @@ -43,7 +43,7 @@ class CheckForMaintenanceMode implements MiddlewareInterface 'url' => (string) $request->getUri(), ]); - return response($html, StatusCodeInterface::STATUS_SERVICE_UNAVAILABLE); + return response($html, self::STATUS_SERVICE_UNAVAILABLE); } return $handler->handle($request); diff --git a/app/Http/Middleware/ReadConfigIni.php b/app/Http/Middleware/ReadConfigIni.php new file mode 100644 index 0000000000..2f7a455276 --- /dev/null +++ b/app/Http/Middleware/ReadConfigIni.php @@ -0,0 +1,68 @@ +<?php +/** + * webtrees: online genealogy + * Copyright (C) 2019 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +declare(strict_types=1); + +namespace Fisharebest\Webtrees\Http\Middleware; + +use function file_exists; +use Fisharebest\Webtrees\Http\Controllers\SetupController; +use Fisharebest\Webtrees\Webtrees; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use function parse_ini_file; + +/** + * Middleware to read (or create) the webtrees configuration file. + */ +class ReadConfigIni implements MiddlewareInterface +{ + /** @var SetupController $controller */ + private $setup_controller; + + /** + * @param SetupController $setup_controller + */ + public function __construct(SetupController $setup_controller) + { + $this->setup_controller = $setup_controller; + } + + /** + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + // Read the configuration settings. + if (file_exists(Webtrees::CONFIG_FILE)) { + $config = parse_ini_file(Webtrees::CONFIG_FILE); + + // Store the configuration settings as request attributes. + foreach ($config as $key => $value) { + $request = $request->withAttribute($key, $value); + } + + return $handler->handle($request); + } + + // No configuration file? Run the setup wizard to create one. + return $this->setup_controller->setup($request); + } +} diff --git a/app/Http/Middleware/UseDatabase.php b/app/Http/Middleware/UseDatabase.php index de7cf108bf..633a6053e2 100644 --- a/app/Http/Middleware/UseDatabase.php +++ b/app/Http/Middleware/UseDatabase.php @@ -17,32 +17,19 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; -use function file_exists; -use Fisharebest\Webtrees\Database; -use Fisharebest\Webtrees\Http\Controllers\SetupController; use Fisharebest\Webtrees\Webtrees; +use Illuminate\Database\Capsule\Manager as DB; +use Illuminate\Database\Query\Builder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use function parse_ini_file; /** * Middleware to connect to the database. */ class UseDatabase implements MiddlewareInterface { - /** @var SetupController $controller */ - private $setup_controller; - - /** - * @param SetupController $setup_controller - */ - public function __construct(SetupController $setup_controller) - { - $this->setup_controller = $setup_controller; - } - /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler @@ -51,16 +38,50 @@ class UseDatabase implements MiddlewareInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - // Read the connection settings and create the database - if (file_exists(Webtrees::CONFIG_FILE)) { - $database_config = parse_ini_file(Webtrees::CONFIG_FILE); + // Earlier versions of webtrees did not have a dbtype config option. They always used mysql. + $driver = $request->getAttribute('dbtype', 'mysql'); - Database::connect($database_config); + $dbname = $request->getAttribute('dbname'); - return $handler->handle($request); + if ($driver === 'sqlite') { + $dbname = Webtrees::ROOT_DIR . 'data/' . $dbname . '.sqlite'; } - // No database connection? Run the setup wizard to create one. - return $this->setup_controller->setup($request); + $capsule = new DB(); + + $capsule->addConnection([ + 'driver' => $driver, + 'host' => $request->getAttribute('dbhost'), + 'port' => $request->getAttribute('dbport'), + 'database' => $dbname, + 'username' => $request->getAttribute('dbuser'), + 'password' => $request->getAttribute('dbpass'), + 'prefix' => $request->getAttribute('tblpfx'), + 'prefix_indexes' => true, + // For MySQL + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci', + 'timezone' => '+00:00', + 'engine' => 'InnoDB', + 'modes' => [ + 'ANSI', + 'STRICT_TRANS_TABLES', + 'NO_ZERO_IN_DATE', + 'NO_ZERO_DATE', + 'ERROR_FOR_DIVISION_BY_ZERO', + ], + // For SQLite + 'foreign_key_constraints' => true, + ]); + + $capsule->setAsGlobal(); + + Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { + $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); + + return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); + }); + + return $handler->handle($request); } } diff --git a/app/Http/Middleware/UseDebugbar.php b/app/Http/Middleware/UseDebugbar.php index aa2e080faa..607b425d90 100644 --- a/app/Http/Middleware/UseDebugbar.php +++ b/app/Http/Middleware/UseDebugbar.php @@ -20,6 +20,7 @@ namespace Fisharebest\Webtrees\Http\Middleware; use DebugBar\StandardDebugBar; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\DebugBar; +use Illuminate\Database\Capsule\Manager as DB; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -47,6 +48,8 @@ class UseDebugbar implements MiddlewareInterface, StatusCodeInterface if (class_exists(StandardDebugBar::class)) { DebugBar::enable(); + DebugBar::initPDO(DB::connection()->getPdo()); + $response = $handler->handle($request); if ($this->shouldSendDataOnNextPage($response)) { diff --git a/app/Webtrees.php b/app/Webtrees.php index fbdcf18c84..3921a73597 100644 --- a/app/Webtrees.php +++ b/app/Webtrees.php @@ -18,7 +18,6 @@ declare(strict_types=1); namespace Fisharebest\Webtrees; use Closure; -use DebugBar\StandardDebugBar; use ErrorException; use Fisharebest\Webtrees\Http\Middleware\BootModules; use Fisharebest\Webtrees\Http\Middleware\CheckCsrf; @@ -29,6 +28,7 @@ use Fisharebest\Webtrees\Http\Middleware\HandleExceptions; use Fisharebest\Webtrees\Http\Middleware\ModuleMiddleware; use Fisharebest\Webtrees\Http\Middleware\NoRouteFound; use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment; +use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni; use Fisharebest\Webtrees\Http\Middleware\RequestRouter; use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; use Fisharebest\Webtrees\Http\Middleware\UseCache; @@ -49,7 +49,6 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UriFactoryInterface; -use Psr\Http\Server\MiddlewareInterface; use Throwable; use function app; use function class_exists; @@ -201,6 +200,7 @@ class Webtrees EmitResponse::class, HandleExceptions::class, CheckForMaintenanceMode::class, + ReadConfigIni::class, UseDatabase::class, UseDebugbar::class, UpdateDatabaseSchema::class, diff --git a/tests/TestCase.php b/tests/TestCase.php index 3faa06dba9..4f2197d56f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -31,6 +31,7 @@ use Fisharebest\Webtrees\Services\UserService; use Illuminate\Cache\ArrayStore; use Illuminate\Cache\Repository; use Illuminate\Database\Capsule\Manager as DB; +use Illuminate\Database\Query\Builder; use League\Flysystem\Filesystem; use League\Flysystem\Memory\MemoryAdapter; use Nyholm\Psr7\Factory\Psr17Factory; @@ -110,7 +111,12 @@ class TestCase extends \PHPUnit\Framework\TestCase implements StatusCodeInterfac 'database' => ':memory:', ]); $capsule->setAsGlobal(); - Database::registerMacros(); + + Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { + $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); + + return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); + }); // Migrations create logs, which requires an IP address, which requires a request self::createRequest(); diff --git a/tests/app/DatabaseTest.php b/tests/app/DatabaseTest.php deleted file mode 100644 index 45b6ef14f4..0000000000 --- a/tests/app/DatabaseTest.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php -/** - * webtrees: online genealogy - * Copyright (C) 2019 webtrees development team - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -declare(strict_types=1); - -namespace Fisharebest\Webtrees; - -/** - * Test harness for the class Statement - */ -class DatabaseTest extends TestCase -{ - /** - * Test that the class exists - * - * @return void - */ - public function testClassExists(): void - { - $this->assertTrue(class_exists('\Fisharebest\Webtrees\Database')); - } -} |
