Merge pull request #22115 from artfulrobot/artfulrobot-api4-count-methods
[civicrm-core.git] / CRM / Extension / Upgrader / Base.php
CommitLineData
31236900
TO
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 */
20class 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;
81ab7ee3 26 use CRM_Extension_Upgrader_SchemaTrait;
31236900
TO
27
28 /**
29 * {@inheritDoc}
30 */
31 public function notify(string $event, array $params = []) {
32 $cb = [$this, 'on' . ucfirst($event)];
33 return is_callable($cb) ? call_user_func_array($cb, $params) : NULL;
34 }
35
36 // ******** Hook delegates ********
37
38 /**
39 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
40 */
41 public function onInstall() {
42 $files = glob($this->getExtensionDir() . '/sql/*_install.sql');
43 if (is_array($files)) {
44 foreach ($files as $file) {
45 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
46 }
47 }
48 $files = glob($this->getExtensionDir() . '/sql/*_install.mysql.tpl');
49 if (is_array($files)) {
50 foreach ($files as $file) {
51 $this->executeSqlTemplate($file);
52 }
53 }
54 $files = glob($this->getExtensionDir() . '/xml/*_install.xml');
55 if (is_array($files)) {
56 foreach ($files as $file) {
57 $this->executeCustomDataFileByAbsPath($file);
58 }
59 }
60 if (is_callable([$this, 'install'])) {
61 $this->install();
62 }
63 }
64
65 /**
66 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
67 */
68 public function onPostInstall() {
69 $revisions = $this->getRevisions();
70 if (!empty($revisions)) {
71 $this->setCurrentRevision(max($revisions));
72 }
73 if (is_callable([$this, 'postInstall'])) {
74 $this->postInstall();
75 }
76 }
77
78 /**
79 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_unnstall
80 */
81 public function onUninstall() {
82 $files = glob($this->getExtensionDir() . '/sql/*_uninstall.mysql.tpl');
83 if (is_array($files)) {
84 foreach ($files as $file) {
85 $this->executeSqlTemplate($file);
86 }
87 }
88 if (is_callable([$this, 'uninstall'])) {
89 $this->uninstall();
90 }
91 $files = glob($this->getExtensionDir() . '/sql/*_uninstall.sql');
92 if (is_array($files)) {
93 foreach ($files as $file) {
94 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
95 }
96 }
97 }
98
99 /**
100 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
101 */
102 public function onEnable() {
103 // stub for possible future use
104 if (is_callable([$this, 'enable'])) {
105 $this->enable();
106 }
107 }
108
109 /**
110 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
111 */
112 public function onDisable() {
113 // stub for possible future use
114 if (is_callable([$this, 'disable'])) {
115 $this->disable();
116 }
117 }
118
119 /**
120 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
121 */
122 public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
123 switch ($op) {
124 case 'check':
125 return [$this->hasPendingRevisions()];
126
127 case 'enqueue':
128 $this->setQueue($queue);
129 return $this->enqueuePendingRevisions();
130
131 default:
132 }
133 }
134
135}