4 * Base class which provides helpers to execute upgrade logic.
6 * LIFECYCLE METHODS: Subclasses may optionally define install(), postInstall(),
7 * uninstall(), enable(), disable().
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)
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)
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)
20 class CRM_Extension_Upgrader_Base
implements CRM_Extension_Upgrader_Interface
{
22 use CRM_Extension_Upgrader_IdentityTrait
;
23 use CRM_Extension_Upgrader_QueueTrait
;
24 use CRM_Extension_Upgrader_RevisionsTrait
;
25 use CRM_Extension_Upgrader_TasksTrait
;
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;
35 // ******** Hook delegates ********
38 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
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);
47 $files = glob($this->getExtensionDir() . '/sql/*_install.mysql.tpl');
48 if (is_array($files)) {
49 foreach ($files as $file) {
50 $this->executeSqlTemplate($file);
53 $files = glob($this->getExtensionDir() . '/xml/*_install.xml');
54 if (is_array($files)) {
55 foreach ($files as $file) {
56 $this->executeCustomDataFileByAbsPath($file);
59 if (is_callable([$this, 'install'])) {
65 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
67 public function onPostInstall() {
68 $revisions = $this->getRevisions();
69 if (!empty($revisions)) {
70 $this->setCurrentRevision(max($revisions));
72 if (is_callable([$this, 'postInstall'])) {
78 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_unnstall
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);
87 if (is_callable([$this, 'uninstall'])) {
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);
99 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
101 public function onEnable() {
102 // stub for possible future use
103 if (is_callable([$this, 'enable'])) {
109 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
111 public function onDisable() {
112 // stub for possible future use
113 if (is_callable([$this, 'disable'])) {
119 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
121 public function onUpgrade($op, CRM_Queue_Queue
$queue = NULL) {
124 return [$this->hasPendingRevisions()];
127 $this->setQueue($queue);
128 return $this->enqueuePendingRevisions();