| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Base class which provides helpers to execute upgrade logic. |
| 5 | * |
| 6 | * LIFECYCLE METHODS: Subclasses may optionally define install(), postInstall(), |
| 7 | * uninstall(), enable(), disable(). |
| 8 | * |
| 9 | * UPGRADE METHODS: Subclasses may define any number of methods named "upgrade_NNNN()". |
| 10 | * Each value of NNNN is treated as a new schema revision. (See also: RevisionsTrait) |
| 11 | * |
| 12 | * QUEUE METHODS: Upgrade tasks execute within a queue. If an upgrader needs to perform |
| 13 | * a large amount of work, it can use "addTask()" / "prependTask()" / "appendTask()". |
| 14 | * (See also: QueueTrait) |
| 15 | * |
| 16 | * EXECUTE METHODS: When writing lifecycle methods, upgrade methods, or queue |
| 17 | * tasks, you may wish to execute common steps like "run a SQL file". |
| 18 | * (See also: TasksTrait) |
| 19 | */ |
| 20 | class CRM_Extension_Upgrader_Base implements CRM_Extension_Upgrader_Interface { |
| 21 | |
| 22 | use CRM_Extension_Upgrader_IdentityTrait; |
| 23 | use CRM_Extension_Upgrader_QueueTrait; |
| 24 | use CRM_Extension_Upgrader_RevisionsTrait; |
| 25 | use CRM_Extension_Upgrader_TasksTrait; |
| 26 | |
| 27 | /** |
| 28 | * {@inheritDoc} |
| 29 | */ |
| 30 | public function notify(string $event, array $params = []) { |
| 31 | $cb = [$this, 'on' . ucfirst($event)]; |
| 32 | return is_callable($cb) ? call_user_func_array($cb, $params) : NULL; |
| 33 | } |
| 34 | |
| 35 | // ******** Hook delegates ******** |
| 36 | |
| 37 | /** |
| 38 | * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install |
| 39 | */ |
| 40 | public function onInstall() { |
| 41 | $files = glob($this->getExtensionDir() . '/sql/*_install.sql'); |
| 42 | if (is_array($files)) { |
| 43 | foreach ($files as $file) { |
| 44 | CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); |
| 45 | } |
| 46 | } |
| 47 | $files = glob($this->getExtensionDir() . '/sql/*_install.mysql.tpl'); |
| 48 | if (is_array($files)) { |
| 49 | foreach ($files as $file) { |
| 50 | $this->executeSqlTemplate($file); |
| 51 | } |
| 52 | } |
| 53 | $files = glob($this->getExtensionDir() . '/xml/*_install.xml'); |
| 54 | if (is_array($files)) { |
| 55 | foreach ($files as $file) { |
| 56 | $this->executeCustomDataFileByAbsPath($file); |
| 57 | } |
| 58 | } |
| 59 | if (is_callable([$this, 'install'])) { |
| 60 | $this->install(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall |
| 66 | */ |
| 67 | public function onPostInstall() { |
| 68 | $revisions = $this->getRevisions(); |
| 69 | if (!empty($revisions)) { |
| 70 | $this->setCurrentRevision(max($revisions)); |
| 71 | } |
| 72 | if (is_callable([$this, 'postInstall'])) { |
| 73 | $this->postInstall(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_unnstall |
| 79 | */ |
| 80 | public function onUninstall() { |
| 81 | $files = glob($this->getExtensionDir() . '/sql/*_uninstall.mysql.tpl'); |
| 82 | if (is_array($files)) { |
| 83 | foreach ($files as $file) { |
| 84 | $this->executeSqlTemplate($file); |
| 85 | } |
| 86 | } |
| 87 | if (is_callable([$this, 'uninstall'])) { |
| 88 | $this->uninstall(); |
| 89 | } |
| 90 | $files = glob($this->getExtensionDir() . '/sql/*_uninstall.sql'); |
| 91 | if (is_array($files)) { |
| 92 | foreach ($files as $file) { |
| 93 | CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable |
| 100 | */ |
| 101 | public function onEnable() { |
| 102 | // stub for possible future use |
| 103 | if (is_callable([$this, 'enable'])) { |
| 104 | $this->enable(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable |
| 110 | */ |
| 111 | public function onDisable() { |
| 112 | // stub for possible future use |
| 113 | if (is_callable([$this, 'disable'])) { |
| 114 | $this->disable(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade |
| 120 | */ |
| 121 | public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) { |
| 122 | switch ($op) { |
| 123 | case 'check': |
| 124 | return [$this->hasPendingRevisions()]; |
| 125 | |
| 126 | case 'enqueue': |
| 127 | $this->setQueue($queue); |
| 128 | return $this->enqueuePendingRevisions(); |
| 129 | |
| 130 | default: |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |