Merge pull request #19594 from eileenmcnaughton/535m
[civicrm-core.git] / setup / plugins / checkRequirements / CoreRequirementsAdapter.civi-setup.php
1 <?php
2 /**
3 * @file
4 *
5 * Run the PHP+MySQL system requirements checks from Civi\Install\Requirements.
6 *
7 * Aesthetically, I'd sorta prefer to remove this and (instead) migrate the
8 * `Requirements.php` so that each check was its own plugin. But for now this works.
9 */
10
11 if (!defined('CIVI_SETUP')) {
12 exit("Installation plugins must only be loaded by the installer.\n");
13 }
14
15 \Civi\Setup::dispatcher()
16 ->addListener('civi.setup.checkRequirements', function (\Civi\Setup\Event\CheckRequirementsEvent $e) {
17 $model = $e->getModel();
18 $r = new \Civi\Install\Requirements();
19
20 \Civi\Setup::log()->info(sprintf('[%s] Run Requirements::checkSystem()', basename(__FILE__)));
21 $systemMsgs = $r->checkSystem(array(/* no $file_paths to pass - we check those elsewhere */));
22 _corereqadapter_addMessages($e, 'system', $systemMsgs);
23
24 \Civi\Setup::log()->info(sprintf('[%s] Run Requirements::checkDatabase()', basename(__FILE__)));
25 list ($host, $port, $socket) = \Civi\Setup\DbUtil::decodeHostPort($model->db['server']);
26 $dbMsgs = $r->checkDatabase(array(
27 'host' => $host,
28 'port' => $port,
29 'socket' => $socket,
30 'username' => $model->db['username'],
31 'password' => $model->db['password'],
32 'database' => $model->db['database'],
33 'ssl_params' => $model->db['ssl_params'] ?? NULL,
34 ));
35 _corereqadapter_addMessages($e, 'database', $dbMsgs);
36 });
37
38 /**
39 * @param \Civi\Setup\Event\CheckRequirementsEvent $e
40 * Symbolic machine name for this group of messages.
41 * Ex: 'database' or 'system'.
42 * @param array $msgs
43 * A list of messages in the format used by \Civi\Install\Requirements
44 */
45 function _corereqadapter_addMessages($e, $section, $msgs) {
46 $severityMap = array(
47 \Civi\Install\Requirements::REQUIREMENT_OK => 'info',
48 \Civi\Install\Requirements::REQUIREMENT_WARNING => 'warning',
49 \Civi\Install\Requirements::REQUIREMENT_ERROR => 'error',
50 );
51
52 foreach ($msgs as $msg) {
53 $e->addMessage($severityMap[$msg['severity']], $section, $msg['title'], $msg['details']);
54 }
55 }