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 10, 2023
1 parent e908dfc commit 2a062b2
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 13 deletions.
19 changes: 13 additions & 6 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 Down Expand Up @@ -80,7 +80,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 +104,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 +130,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 +151,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 +236,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 +245,13 @@ public function get_cached_data(): \stdClass {
return $data;
}

/**
* Get the cache used by the datasource.
*
* @return \cache
*/
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 <jasondendulk@catalyst-au.net>
* @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
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
2 changes: 1 addition & 1 deletion tests/fixtures/null_datasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace mod_cms;

use mod_cms\local\datasource\base as dsbase;
use mod_cms\local\datasource\base_mod_cms as dsbase;
use mod_cms\local\datasource\traits\nullcache;

/**
Expand Down

0 comments on commit 2a062b2

Please sign in to comment.