Skip to content

Commit

Permalink
Merge pull request #19 from lucky091588/master
Browse files Browse the repository at this point in the history
Regenerate module and upgrader
  • Loading branch information
twomice authored Apr 26, 2021
2 parents 05631f1 + 3466e6e commit 28377a4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
96 changes: 59 additions & 37 deletions CRM/Jsumfields/Upgrader/Base.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
use CRM_Jsumfields_ExtensionUtil as E;

/**
* Base class which provides helpers to execute upgrade logic
*/
class CRM_Jsumfields_Upgrader_Base {

/**
* @var varies, subclass of ttis
* @var CRM_Jsumfields_Upgrader_Base
*/
public static $instance;

Expand All @@ -18,22 +19,25 @@ class CRM_Jsumfields_Upgrader_Base {
protected $ctx;

/**
* @var string, eg 'com.example.myextension'
* @var string
* eg 'com.example.myextension'
*/
protected $extensionName;

/**
* @var string, full path to the extension's source tree
* @var string
* full path to the extension's source tree
*/
protected $extensionDir;

/**
* @var array(revisionNumber) sorted numerically
* @var revisionNumber[]
* sorted numerically
*/
private $revisions;

/**
* @var boolean
* @var bool
* Flag to clean up extension revision data in civicrm_setting
*/
private $revisionStorageIsDeprecated = FALSE;
Expand All @@ -58,19 +62,25 @@ public static function instance() {
* Note: Each upgrader instance should only be associated with one
* task-context; otherwise, this will be non-reentrant.
*
* @code
* ```
* CRM_Jsumfields_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
* @endcode
* ```
*/
public static function _queueAdapter() {
$instance = self::instance();
$args = func_get_args();
$instance->ctx = array_shift($args);
$instance->queue = $instance->ctx->queue;
$method = array_shift($args);
return call_user_func_array(array($instance, $method), $args);
return call_user_func_array([$instance, $method], $args);
}

/**
* CRM_Jsumfields_Upgrader_Base constructor.
*
* @param $extensionName
* @param $extensionDir
*/
public function __construct($extensionName, $extensionDir) {
$this->extensionName = $extensionName;
$this->extensionDir = $extensionDir;
Expand All @@ -81,7 +91,8 @@ public function __construct($extensionName, $extensionDir) {
/**
* Run a CustomData file.
*
* @param string $relativePath the CustomData XML file path (relative to this extension's dir)
* @param string $relativePath
* the CustomData XML file path (relative to this extension's dir)
* @return bool
*/
public function executeCustomDataFile($relativePath) {
Expand All @@ -92,11 +103,12 @@ public function executeCustomDataFile($relativePath) {
/**
* Run a CustomData file
*
* @param string $xml_file the CustomData XML file path (absolute path)
* @param string $xml_file
* the CustomData XML file path (absolute path)
*
* @return bool
*/
protected static function executeCustomDataFileByAbsPath($xml_file) {
protected function executeCustomDataFileByAbsPath($xml_file) {
$import = new CRM_Utils_Migrate_Import();
$import->run($xml_file);
return TRUE;
Expand All @@ -105,7 +117,8 @@ protected static function executeCustomDataFileByAbsPath($xml_file) {
/**
* Run a SQL file.
*
* @param string $relativePath the SQL file path (relative to this extension's dir)
* @param string $relativePath
* the SQL file path (relative to this extension's dir)
*
* @return bool
*/
Expand All @@ -118,10 +131,14 @@ public function executeSqlFile($relativePath) {
}

/**
* Run the sql commands in the specified file.
*
* @param string $tplFile
* The SQL file path (relative to this extension's dir).
* Ex: "sql/mydata.mysql.tpl".
*
* @return bool
* @throws \CRM_Core_Exception
*/
public function executeSqlTemplate($tplFile) {
// Assign multilingual variable to Smarty.
Expand All @@ -140,17 +157,19 @@ public function executeSqlTemplate($tplFile) {
* Run one SQL query.
*
* This is just a wrapper for CRM_Core_DAO::executeSql, but it
* provides syntatic sugar for queueing several tasks that
* provides syntactic sugar for queueing several tasks that
* run different queries
*
* @return bool
*/
public function executeSql($query, $params = array()) {
public function executeSql($query, $params = []) {
// FIXME verify that we raise an exception on error
CRM_Core_DAO::executeQuery($query, $params);
return TRUE;
}

/**
* Syntatic sugar for enqueuing a task which calls a function in this class.
* Syntactic sugar for enqueuing a task which calls a function in this class.
*
* The task is weighted so that it is processed
* as part of the currently-pending revision.
Expand All @@ -162,11 +181,11 @@ public function addTask($title) {
$args = func_get_args();
$title = array_shift($args);
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
[get_class($this), '_queueAdapter'],
$args,
$title
);
return $this->queue->createItem($task, array('weight' => -1));
return $this->queue->createItem($task, ['weight' => -1]);
}

// ******** Revision-tracking helpers ********
Expand All @@ -192,30 +211,32 @@ public function hasPendingRevisions() {

/**
* Add any pending revisions to the queue.
*
* @param CRM_Queue_Queue $queue
*/
public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
$this->queue = $queue;

$currentRevision = $this->getCurrentRevision();
foreach ($this->getRevisions() as $revision) {
if ($revision > $currentRevision) {
$title = ts('Upgrade %1 to revision %2', array(
$title = E::ts('Upgrade %1 to revision %2', [
1 => $this->extensionName,
2 => $revision,
));
]);

// note: don't use addTask() because it sets weight=-1

$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
array('upgrade_' . $revision),
[get_class($this), '_queueAdapter'],
['upgrade_' . $revision],
$title
);
$this->queue->createItem($task);

$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
array('setCurrentRevision', $revision),
[get_class($this), '_queueAdapter'],
['setCurrentRevision', $revision],
$title
);
$this->queue->createItem($task);
Expand All @@ -226,11 +247,12 @@ public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
/**
* Get a list of revisions.
*
* @return array(revisionNumbers) sorted numerically
* @return array
* revisionNumbers sorted numerically
*/
public function getRevisions() {
if (!is_array($this->revisions)) {
$this->revisions = array();
$this->revisions = [];

$clazz = new ReflectionClass(get_class($this));
$methods = $clazz->getMethods();
Expand All @@ -255,7 +277,7 @@ public function getCurrentRevision() {

private function getCurrentRevisionDeprecated() {
$key = $this->extensionName . ':version';
if ($revision = CRM_Core_BAO_Setting::getItem('Extension', $key)) {
if ($revision = \Civi::settings()->get($key)) {
$this->revisionStorageIsDeprecated = TRUE;
}
return $revision;
Expand All @@ -280,7 +302,7 @@ private function deleteDeprecatedRevision() {
// ******** Hook delegates ********

/**
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
public function onInstall() {
$files = glob($this->extensionDir . '/sql/*_install.sql');
Expand All @@ -301,26 +323,26 @@ public function onInstall() {
$this->executeCustomDataFileByAbsPath($file);
}
}
if (is_callable(array($this, 'install'))) {
if (is_callable([$this, 'install'])) {
$this->install();
}
}

/**
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
*/
public function onPostInstall() {
$revisions = $this->getRevisions();
if (!empty($revisions)) {
$this->setCurrentRevision(max($revisions));
}
if (is_callable(array($this, 'postInstall'))) {
if (is_callable([$this, 'postInstall'])) {
$this->postInstall();
}
}

/**
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
*/
public function onUninstall() {
$files = glob($this->extensionDir . '/sql/*_uninstall.mysql.tpl');
Expand All @@ -329,7 +351,7 @@ public function onUninstall() {
$this->executeSqlTemplate($file);
}
}
if (is_callable(array($this, 'uninstall'))) {
if (is_callable([$this, 'uninstall'])) {
$this->uninstall();
}
$files = glob($this->extensionDir . '/sql/*_uninstall.sql');
Expand All @@ -341,29 +363,29 @@ public function onUninstall() {
}

/**
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
public function onEnable() {
// stub for possible future use
if (is_callable(array($this, 'enable'))) {
if (is_callable([$this, 'enable'])) {
$this->enable();
}
}

/**
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
*/
public function onDisable() {
// stub for possible future use
if (is_callable(array($this, 'disable'))) {
if (is_callable([$this, 'disable'])) {
$this->disable();
}
}

public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
switch ($op) {
case 'check':
return array($this->hasPendingRevisions());
return [$this->hasPendingRevisions()];

case 'enqueue':
return $this->enqueuePendingRevisions($queue);
Expand Down
4 changes: 2 additions & 2 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<url desc="Support">https://github.com/twomice/com.joineryhq.jsumfields/issues</url>
<url desc="Licensing">http://www.gnu.org/licenses/gpl-3.0.html</url>
</urls>
<releaseDate>2020-07-03</releaseDate>
<version>1.1</version>
<releaseDate>2021-04-20</releaseDate>
<version>1.2</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
Expand Down

0 comments on commit 28377a4

Please sign in to comment.