|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * An example module to modify PHP and database configuration. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace ExampleNamespace; |
| 10 | + |
| 11 | +use Fisharebest\Webtrees\Module\AbstractModule; |
| 12 | +use Fisharebest\Webtrees\Module\ModuleCustomInterface; |
| 13 | +use Fisharebest\Webtrees\Module\ModuleCustomTrait; |
| 14 | +use Fisharebest\Webtrees\Services\ServerCheckService; |
| 15 | +use Illuminate\Database\Capsule\Manager as DB; |
| 16 | + |
| 17 | +class ExampleModuleServerConfig extends AbstractModule implements ModuleCustomInterface |
| 18 | +{ |
| 19 | + use ModuleCustomTrait; |
| 20 | + |
| 21 | + private ServerCheckService $server_check_service; |
| 22 | + |
| 23 | + /** |
| 24 | + * Constructor. |
| 25 | + * |
| 26 | + * @param ServerCheckService $server_check_service |
| 27 | + */ |
| 28 | + public function __construct(ServerCheckService $server_check_service) |
| 29 | + { |
| 30 | + $this->server_check_service = $server_check_service; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * How should this module be identified in the control panel, etc.? |
| 35 | + * |
| 36 | + * @return string |
| 37 | + */ |
| 38 | + public function title(): string |
| 39 | + { |
| 40 | + return 'Example module'; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * If you do not have access to the PHP.INI or MYSQL.CNF files on your server, then |
| 45 | + * you may be able to change them. |
| 46 | + */ |
| 47 | + public function boot(): void |
| 48 | + { |
| 49 | + // IMPORTANT - not all servers allow you to change these settings. Sometimes, even |
| 50 | + // attempting to change them can result in your script being terminated immediately. |
| 51 | + // We attempt to detect whether this will happen, but it is not possible to |
| 52 | + // do so with 100% accuracy. |
| 53 | + |
| 54 | + if (!$this->server_check_service->isFunctionDisabled('ini_set')) { |
| 55 | + $this->phpIni(); |
| 56 | + } |
| 57 | + |
| 58 | + if (!$this->server_check_service->isFunctionDisabled('set_time_limit')) { |
| 59 | + $this->phpTimeLimit(); |
| 60 | + } |
| 61 | + |
| 62 | + if (!$this->server_check_service->isFunctionDisabled('putenv')) { |
| 63 | + $this->phpEnvironment(); |
| 64 | + } |
| 65 | + |
| 66 | + if (DB::connection()->getDriverName() === 'mysql') { |
| 67 | + $this->mysql(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Modify the PHP time limit. |
| 73 | + */ |
| 74 | + private function phpTimeLimit(): void |
| 75 | + { |
| 76 | + // Set the time limit for PHP scripts. |
| 77 | + // Recommended settings are between 15 and 60 seconds. |
| 78 | + // |
| 79 | + // Typical webservers will not wait more than 60 seconds for a PHP response, |
| 80 | + // so it is pointless to allow the server to continue using resources for |
| 81 | + // a request that will be ignored. |
| 82 | + |
| 83 | + //set_time_limit(45); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Modify the PHP environment variables. |
| 88 | + */ |
| 89 | + private function phpEnvironment(): void |
| 90 | + { |
| 91 | + // Some servers block access to the system temporary folder using open_basedir... |
| 92 | + // |
| 93 | + // Create a temporary folder somewhere we have read/write access, and tell PHP to use it. |
| 94 | + //$tmp = __DIR__ . '/../../data/tmp'; |
| 95 | + //if (!is_dir($tmp)) { |
| 96 | + // mkdir($tmp); |
| 97 | + //} |
| 98 | + //putenv('TMPDIR=' . $tmp); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Modify the PHP.INI settings. |
| 103 | + */ |
| 104 | + private function phpIni(): void |
| 105 | + { |
| 106 | + // Set the maximum amount of memory that PHP scripts can use. |
| 107 | + // Recommended settings are between 128M and 1024M |
| 108 | + |
| 109 | + //ini_set('memory_limit', '256M'); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Modify the MySQL connection. |
| 114 | + */ |
| 115 | + private function mysql(): void |
| 116 | + { |
| 117 | + // If you get the error "The SELECT would examine more than MAX_JOIN_SIZE rows", |
| 118 | + // then setting this option may help. |
| 119 | + |
| 120 | + //DB::statement('SET SESSION sql_big_selects := 1'); |
| 121 | + } |
| 122 | +} |
0 commit comments