Passed
Push — master ( 4e39d4...a81abc )
by Greg
06:05
created

ExampleServerConfigurationModule   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 121
rs 10
c 1
b 0
f 0
wmc 13

9 Methods

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