diff options
| author | Greg Roach <fisharebest@webtrees.net> | 2019-10-29 09:32:32 +0000 |
|---|---|---|
| committer | Greg Roach <fisharebest@webtrees.net> | 2019-10-29 09:43:22 +0000 |
| commit | 9e18e23b968678b192e5541acd3252e4978d69c3 (patch) | |
| tree | 5200345a8cc01c370c997f8f00b08e4a492ec5a4 /modules_v4 | |
| parent | 4e39d4b1e0b60b0125c6c93e6488b07bcd338a13 (diff) | |
| download | webtrees-9e18e23b968678b192e5541acd3252e4978d69c3.tar.gz webtrees-9e18e23b968678b192e5541acd3252e4978d69c3.tar.bz2 webtrees-9e18e23b968678b192e5541acd3252e4978d69c3.zip | |
Change signature of Module::boot()
Diffstat (limited to 'modules_v4')
3 files changed, 175 insertions, 158 deletions
diff --git a/modules_v4/example-server-configuration.disable/ExampleServerConfigurationModule.php b/modules_v4/example-server-configuration.disable/ExampleServerConfigurationModule.php new file mode 100644 index 0000000000..bc0d3c6ac1 --- /dev/null +++ b/modules_v4/example-server-configuration.disable/ExampleServerConfigurationModule.php @@ -0,0 +1,143 @@ +<?php + +/** + * An example module to modify PHP and database configuration. + */ + +declare(strict_types=1); + +namespace MyCustomNamespace; + +use Fisharebest\Webtrees\Module\AbstractModule; +use Fisharebest\Webtrees\Module\ModuleCustomInterface; +use Fisharebest\Webtrees\Module\ModuleCustomTrait; +use Fisharebest\Webtrees\Services\ServerCheckService; +use Illuminate\Database\Capsule\Manager as DB; + +class ExampleServerConfigurationModule extends AbstractModule implements ModuleCustomInterface +{ + use ModuleCustomTrait; + + /** @var ServerCheckService */ + private $server_check_service; + + /** + * Constructor. + * + * @param ServerCheckService $server_check_service + */ + public function __construct(ServerCheckService $server_check_service) + { + $this->server_check_service = $server_check_service; + } + + /** + * How should this module be identified in the control panel, etc.? + * + * @return string + */ + public function title(): string + { + return 'Server configuration'; + } + + /** + * A sentence describing what this module does. + * + * @return string + */ + public function description(): string + { + return 'Modify the server configuration'; + } + + /** + * The person or organisation who created this module. + * + * @return string + */ + public function customModuleAuthorName(): string + { + return 'Your name'; + } + + /** + * If you do not have access to the PHP.INI or MYSQL.CNF files on your server, then + * you may be able to change them. + */ + public function boot(): void + { + // IMPORTANT - not all servers allow you to change these settings. Sometimes, even + // attempting to change them can result in your script being terminated immediately. + // We attempt to detect whether this will happen, but it is not possible to + // do so with 100% accuracy. + + if (!$this->server_check_service->isFunctionDisabled('ini_set')) { + $this->phpIni(); + } + + if (!$this->server_check_service->isFunctionDisabled('set_time_limit')) { + $this->phpTimeLimit(); + } + + if (!$this->server_check_service->isFunctionDisabled('putenv')) { + $this->phpEnvironment(); + } + + if (DB::connection()->getDriverName() === 'mysql') { + $this->mysql(); + } + } + + /** + * Modify the PHP time limit. + */ + private function phpTimeLimit(): void + { + // Set the time limit for PHP scripts. + // Recommended settings are between 15 and 60 seconds. + // + // Typical webservers will not wait more than 60 seconds for a PHP response, + // so it is pointless to allow the server to continue using resources for + // a request that will be ignored. + + //set_time_limit(45); + } + + /** + * Modify the PHP environment variables. + */ + private function phpEnvironment(): void + { + // Some servers block access to the system temporary folder using open_basedir... + // + // Create a temporary folder somewhere we have read/write access, and tell PHP to use it. + //$tmp = __DIR__ . '/../../data/tmp'; + //if (!is_dir($tmp)) { + // mkdir($tmp); + //} + //putenv('TMPDIR=' . $tmp); + } + + /** + * Modify the PHP.INI settings. + */ + private function phpIni(): void + { + // Set the maximum amount of memory that PHP scripts can use. + // Recommended settings are between 128M and 1024M + + //ini_set('memory_limit', '256M'); + } + + /** + * Modify the MySQL connection. + */ + private function mysql(): void + { + // If you get the error "The SELECT would examine more than MAX_JOIN_SIZE rows", + // then setting this option may help. + + //DB::statement('SET SESSION sql_big_selects := 1'); + } +} diff --git a/modules_v4/example-server-configuration.disable/module.php b/modules_v4/example-server-configuration.disable/module.php index 87504e2958..1e21e33a0e 100644 --- a/modules_v4/example-server-configuration.disable/module.php +++ b/modules_v4/example-server-configuration.disable/module.php @@ -8,129 +8,6 @@ declare(strict_types=1); namespace MyCustomNamespace; -use Fisharebest\Webtrees\Module\AbstractModule; -use Fisharebest\Webtrees\Module\ModuleCustomInterface; -use Fisharebest\Webtrees\Module\ModuleCustomTrait; -use Fisharebest\Webtrees\Services\ServerCheckService; -use Illuminate\Database\Capsule\Manager as DB; +require __DIR__ . '/ExampleServerConfigurationModule.php'; -return new class extends AbstractModule implements ModuleCustomInterface { - use ModuleCustomTrait; - - /** @var ServerCheckService */ - private $server_check_service; - - /** - * How should this module be identified in the control panel, etc.? - * - * @return string - */ - public function title(): string - { - return 'Server configuration'; - } - - /** - * A sentence describing what this module does. - * - * @return string - */ - public function description(): string - { - return 'Modify the server configuration'; - } - - /** - * The person or organisation who created this module. - * - * @return string - */ - public function customModuleAuthorName(): string - { - return 'Your name'; - } - - /** - * If you do not have access to the PHP.INI or MYSQL.CNF files on your server, then - * you may be able to change them. - * - * @param ServerCheckService $server_check_service - */ - public function boot(ServerCheckService $server_check_service): void - { - $this->server_check_service = $server_check_service; - - // IMPORTANT - not all servers allow you to change these settings. Sometimes, even - // attempting to change them can result in your script being terminated immediately. - // We attempt to detect whether this will happen, but it is not possible to - // do so with 100% accuracy. - - if (!$this->server_check_service->isFunctionDisabled('ini_set')) { - $this->phpIni(); - } - - if (!$this->server_check_service->isFunctionDisabled('set_time_limit')) { - $this->phpTimeLimit(); - } - - if (!$this->server_check_service->isFunctionDisabled('putenv')) { - $this->phpEnvironment(); - } - - if (DB::connection()->getDriverName() === 'mysql') { - $this->mysql(); - } - } - - /** - * Modify the PHP time limit. - */ - private function phpTimeLimit(): void - { - // Set the time limit for PHP scripts. - // Recommended settings are between 15 and 60 seconds. - // - // Typical webservers will not wait more than 60 seconds for a PHP response, - // so it is pointless to allow the server to continue using resources for - // a request that will be ignored. - - //set_time_limit(45); - } - - /** - * Modify the PHP environment variables. - */ - private function phpEnvironment(): void - { - // Some servers block access to the system temporary folder using open_basedir... - // - // Create a temporary folder somewhere we have read/write access, and tell PHP to use it. - //$tmp = __DIR__ . '/../../data/tmp'; - //if (!is_dir($tmp)) { - // mkdir($tmp); - //} - //putenv('TMPDIR=' . $tmp); - } - - /** - * Modify the PHP.INI settings. - */ - private function phpIni(): void - { - // Set the maximum amount of memory that PHP scripts can use. - // Recommended settings are between 128M and 1024M - - //ini_set('memory_limit', '256M'); - } - - /** - * Modify the MySQL connection. - */ - private function mysql(): void - { - // If you get the error "The SELECT would examine more than MAX_JOIN_SIZE rows", - // then setting this option may help. - - //DB::statement('SET SESSION sql_big_selects := 1'); - } -}; +return app(ExampleServerConfigurationModule::class); diff --git a/modules_v4/example.disable/module.php b/modules_v4/example.disable/module.php index f3882a3b24..c1c4dae88b 100644 --- a/modules_v4/example.disable/module.php +++ b/modules_v4/example.disable/module.php @@ -1,25 +1,46 @@ <?php /** - * Example module + * Example module. */ declare(strict_types=1); namespace MyCustomNamespace; -use Fisharebest\Webtrees\Auth; -use Fisharebest\Webtrees\Contracts\UserInterface; +use Fisharebest\Localization\Translation; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Module\AbstractModule; use Fisharebest\Webtrees\Module\ModuleCustomInterface; use Fisharebest\Webtrees\Module\ModuleCustomTrait; -use Fisharebest\Webtrees\Tree; return new class extends AbstractModule implements ModuleCustomInterface { use ModuleCustomTrait; /** + * Constructor. The constructor is called on *all* modules, even ones that are disabled. + * This is a good place to load business logic ("services"). Type-hint the parameters and + * they will be injected automatically. + */ + public function __construct() + { + // NOTE: If your module is dependent on any of the business logic ("services"), + // then you would type-hint them in the constructor and let webtrees inject them + // for you. However, we can't use dependency injection on anonymous classes like + // this one. For an example of this, see the example-server-configuration module. + } + + /** + * Bootstrap. This function is called on *enabled* modules. + * It is a good place to register routes and views. + * + * @return void + */ + public function boot(): void + { + } + + /** * How should this module be identified in the control panel, etc.? * * @return string @@ -80,31 +101,6 @@ return new class extends AbstractModule implements ModuleCustomInterface { } /** - * Constructor. - */ - public function __construct() - { - // IMPORTANT - the constructor is called on *all* modules, even ones that are disabled. - // It is also called before the webtrees framework is initialised, and so other components - // will not yet exist. - } - - /** - * Boostrap. - * - * @param UserInterface $user A user (or visitor) object. - * @param Tree|null $tree Note that $tree can be null (if all trees are private). - */ - public function boot(UserInterface $user, ?Tree $tree): void - { - // The boot() function is called after the framework has been booted. - // We can now use the current user, tree, etc. - if ($tree !== null && !Auth::isAdmin($user)) { - return; - } - } - - /** * Additional/updated translations. * * @param string $language @@ -113,10 +109,6 @@ return new class extends AbstractModule implements ModuleCustomInterface { */ public function customTranslations(string $language): array { - // Here we are using an array for translations. - // If you had .MO files, you could use them with: - // return (new Translation('path/to/file.mo'))->asArray(); - switch ($language) { case 'en-AU': case 'en-GB': @@ -127,6 +119,11 @@ return new class extends AbstractModule implements ModuleCustomInterface { case 'fr-CA': return $this->frenchTranslations(); + case 'some-other-language': + // Arrays are preferred, and faster. + // If your module uses .MO files, then you can convert them to arrays like this. + return (new Translation('path/to/file.mo'))->asArray(); + default: return []; } |
