Merge pull request #19230 from totten/master-phpseclib-cxnrpc
[civicrm-core.git] / CRM / Upgrade / DispatchPolicy.php
CommitLineData
1c3c3394
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17class CRM_Upgrade_DispatchPolicy {
18
19 /**
20 * Determine the dispatch policy
21 *
22 * @param string $phase
23 * Ex: 'upgrade.main' or 'upgrade.finish'.
24 * @return array
25 * @see \Civi\Core\CiviEventDispatcher::setDispatchPolicy()
26 */
27 public static function get($phase) {
28
29 // Should hooks dispatch while applying CiviCRM DB upgrades? The answer is
30 // mixed: it depends on the specific hook and the specific upgrade-step.
31 //
32 // Some example considerations:
33 // - If the "log_civicrm_*" tables and triggers are to be reconciled during
34 // the upgrade, then one probably needs access to the list of tables and
35 // triggers defined by extensions. These are provided by hooks.
36 // - If a hook fires while the DB has stale schema, and if the hook's logic
37 // has a direct (SQL) or indirect (BAO/API) dependency on the schema, then
38 // the hook is prone to fail. (Ex: CiviCRM 4.x and the migration from
39 // civicrm_domain.config_backend to civicrm_setting.)
40 // - If *any* hook from an extension is called, then it may use classes
41 // from the same extension, so the classloader / include-path / hook_config
42 // should be operational.
43 // - If there is a general system flush at the end of the upgrade (to rebuild
44 // important data-structures -- routing tables, container cache, metadata
45 // cache, etc), then there's a huge number of hooks that should fire.
46 // - When hooks (or variations like "rules") are used to define business-logic,
47 // they probably are not intended to fire during DB upgrade. Then again,
48 // upgrade-logic is usually written with lower-level semantics that avoid firing hooks.
49 //
50 // Related discussions:
51 // - https://github.com/civicrm/civicrm-core/pull/13551
52 // - https://lab.civicrm.org/dev/core/issues/1449
53 // - https://lab.civicrm.org/dev/core/issues/1460
54
55 $strict = getenv('CIVICRM_UPGRADE_STRICT') || CRM_Utils_Constant::value('CIVICRM_UPGRADE_STRICT');
56 $policies = [];
57
58 // The "upgrade.main" policy applies during the planning and incremental revisions.
59 // It's more restrictive, preventing interference from unexpected callpaths.
60 $policies['upgrade.main'] = [
61 'hook_civicrm_config' => 'run',
a9033ca7
TO
62 // cleanupPermissions() in some UF's can be destructive. Running prematurely could be actively harmful.
63 'hook_civicrm_permission' => 'fail',
1c3c3394
TO
64 '/^hook_civicrm_(pre|post)$/' => 'drop',
65 '/^hook_civicrm_/' => $strict ? 'warn-drop' : 'drop',
66 '/^civi\./' => 'run',
67 '/./' => $strict ? 'warn-drop' : 'drop',
68 ];
69
70 // The "upgrade.finish" policy applies at the end while performing the final clear/rebuild.
71 // It's more permissive, allowing more data-structures to rehydrate correctly.
72 $policies['upgrade.finish'] = [
73 '/^hook_civicrm_(pre|post)$/' => 'drop',
74 '/./' => 'run',
75 ];
76
77 // For comparison, "upgrade.old" is an estimation of the previous policy. It
78 // was applied at all times during the upgrade.
79 $policies['upgrade.old'] = [
80 'hook_civicrm_alterSettingsFolders' => 'run',
81 'hook_civicrm_alterSettingsMetaData' => 'run',
82 'hook_civicrm_triggerInfo' => 'run',
83 'hook_civicrm_alterLogTables' => 'run',
84 'hook_civicrm_container' => 'run',
85 'hook_civicrm_permission' => 'run',
86 'hook_civicrm_managed' => 'run',
87 'hook_civicrm_config' => 'run',
88 '/^hook_civicrm_(pre|post)$/' => 'drop',
89 '/^hook_civicrm_/' => 'drop',
90 '/^civi\./' => 'run',
91 '/./' => 'run',
92 ];
93
29b6189c
TO
94 // return $policies['upgrade.old'];
95 return $policies[$phase];
1c3c3394
TO
96 }
97
98}