Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMCL-101: Regenerate civix file and Base upgrader class #23

Merged
merged 1 commit into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 60 additions & 39 deletions CRM/EventsExtras/Upgrader/Base.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
<?php

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

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

/**
* @var variessubclassofthis
* @var CRM_EventsExtras_Upgrader_Base
*/
static $instance;
public static $instance;

/**
* @var CRM_Queue_TaskContext
*/
protected $ctx;

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

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

/**
* @var arrayrevisionNumbersortednumerically
* @var array
* sorted numerically
*/
private $revisions;

Expand All @@ -43,10 +47,9 @@ class CRM_EventsExtras_Upgrader_Base {
*/
public static function instance() {
if (!self::$instance) {
// FIXME auto-generate
self::$instance = new CRM_EventsExtras_Upgrader(
'uk.co.compucorp.eventsextras',
realpath(__DIR__ . '/../../../')
E::path()
);
}
return self::$instance;
Expand All @@ -58,19 +61,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_EventsExtras_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_EventsExtras_Upgrader_Base constructor.
*
* @param $extensionName
* @param $extensionDir
*/
public function __construct($extensionName, $extensionDir) {
$this->extensionName = $extensionName;
$this->extensionDir = $extensionDir;
Expand All @@ -81,7 +90,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 +102,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 +116,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 +130,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 +156,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 +180,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 +210,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 +246,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 +276,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 +301,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 +322,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 +350,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 +362,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
Loading