Skip to content

Commit

Permalink
Issue #98: Refactor to get the datasource to supply the cache object.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypha committed Oct 11, 2023
1 parent e908dfc commit 69cf722
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 19 deletions.
38 changes: 25 additions & 13 deletions classes/local/datasource/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class base {
/**
* Create the list of datasources to be used.
*/
public static function register_datasources() {
final public static function register_datasources() {
// Only need to do this once.
if (!empty(self::$datasourceclasses)) {
return;
Expand All @@ -62,13 +62,15 @@ public static function register_datasources() {
self::add_datasource_class(__NAMESPACE__ . '\\' . $classname);
}

// Register any datasources defined in other plugins.
$plugintypes = get_plugins_with_function('modcms_datasources', 'lib.php');
foreach ($plugintypes as $plugins) {
foreach ($plugins as $pluginfunction) {
$result = $pluginfunction();
foreach ($result as $classname) {
self::add_datasource_class($classname);
if (!PHPUNIT_TEST) {
// Register any datasources defined in other plugins.
$plugintypes = get_plugins_with_function('modcms_datasources', 'lib.php');
foreach ($plugintypes as $plugins) {
foreach ($plugins as $pluginfunction) {
$result = $pluginfunction();
foreach ($result as $classname) {
self::add_datasource_class($classname);
}
}
}
}
Expand All @@ -80,7 +82,7 @@ public static function register_datasources() {
* @param string $classname
* @throws \moodle_exception
*/
public static function add_datasource_class(string $classname) {
final public static function add_datasource_class(string $classname) {
// Test for existence.
if (!class_exists($classname)) {
throw new \moodle_exception('error:class_missing', 'mod_cms', '', $classname);
Expand All @@ -104,7 +106,7 @@ public static function add_datasource_class(string $classname) {
* @param bool $enabledonly
* @return \Generator
*/
public static function get_datasources($cms, bool $enabledonly = true) {
final public static function get_datasources($cms, bool $enabledonly = true) {
self::register_datasources();

if ($cms === null) {
Expand All @@ -130,7 +132,7 @@ public static function get_datasources($cms, bool $enabledonly = true) {
* @param cms|cms_types $cms
* @return false|base The datasource, or false if not found.
*/
public static function get_datasource(string $name, $cms) {
final public static function get_datasource(string $name, $cms) {
self::register_datasources();

if (!isset(self::$datasourceclasses[$name])) {
Expand All @@ -151,7 +153,7 @@ public static function get_datasource(string $name, $cms) {
* @param bool $optionalonly
* @return array
*/
public static function get_datasource_labels(bool $optionalonly = true): array {
final public static function get_datasource_labels(bool $optionalonly = true): array {
self::register_datasources();

$labels = [];
Expand Down Expand Up @@ -236,7 +238,7 @@ public function get_cached_data(): \stdClass {
return $this->get_data();
}

$cache = \cache::make('mod_cms', 'cms_content_' . self::get_shortname());
$cache = $this->get_cache();
$data = $cache->get($key);
if ($data === false) {
$data = $this->get_data();
Expand All @@ -245,6 +247,16 @@ public function get_cached_data(): \stdClass {
return $data;
}

/**
* Get the cache used by the datasource. This is required for caching the data returned by get_data(),
* and needs to be declared in the plugin of the datasource.
*
* If there is no caching done, have this return null.
*
* @return \cache|null
*/
abstract public function get_cache(): ?\cache;

/**
* Add fields to the CMS instance form.
*
Expand Down
40 changes: 40 additions & 0 deletions classes/local/datasource/base_mod_cms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace mod_cms\local\datasource;

use mod_cms\local\lib;
use mod_cms\local\model\cms;
use mod_cms\local\model\cms_types;

/**
* Base class for mod_cms defined data sources.
*
* @package mod_cms
* @author Jason den Dulk <[email protected]>
* @copyright 2023, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_mod_cms extends base {
/**
* Get the cache used by the datasource.
*
* @return \cache
*/
public function get_cache(): \cache {
return \cache::make('mod_cms', 'cms_content_' . self::get_shortname());
}
}
2 changes: 1 addition & 1 deletion classes/local/datasource/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @copyright 2023, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fields extends base {
class fields extends base_mod_cms {
/** @var cmsfield_handler Custom field handler. */
protected $cfhandler;

Expand Down
2 changes: 1 addition & 1 deletion classes/local/datasource/images.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @copyright 2023, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class images extends base {
class images extends base_mod_cms {
/** The maximum amount of files allowed. */
const MAX_FILES = 50;

Expand Down
2 changes: 1 addition & 1 deletion classes/local/datasource/roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @copyright 2023, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class roles extends base {
class roles extends base_mod_cms {
/** Prefix used for form elements. */
const FORM_PREFIX = 'roles_';

Expand Down
2 changes: 1 addition & 1 deletion classes/local/datasource/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @copyright 2023, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class site extends base {
class site extends base_mod_cms {

/** @var bool Set to false when the datasource doees not cache. */
public static $usecache = false;
Expand Down
10 changes: 10 additions & 0 deletions classes/local/datasource/traits/nullcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait nullcache {
/**
* Get the cache used by the datasource.
* For null caching, this returns null.
*
* @return \cache|null
*/
public function get_cache(): ?\cache {
return null;
}

/**
* Returns the cache key fragment for the instance data.
* If null, then caching should be avoided, both here and for the overall instance.
Expand Down
2 changes: 1 addition & 1 deletion classes/local/datasource/userlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @copyright 2023, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class userlist extends base {
class userlist extends base_mod_cms {
/** Default number of rows to include in the list edit form. */
const DEFAULT_NUM_ROWS = 2;
/** Prefix to use for list elements. */
Expand Down
2 changes: 1 addition & 1 deletion classes/local/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function get_name(): string {
* Gets content from a template using a cache.
* If the cache key is null, then the cache is not used.
*
* @param string $cachename
* @param string $cachename Should either be 'cms_content' or 'cms_name'.
* @param string $varname Variable that holds the template.
* @return string
*/
Expand Down

0 comments on commit 69cf722

Please sign in to comment.