From a0d5133f108085e9923d051d5fd85d311a5989eb Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 7 May 2024 17:14:26 +0100 Subject: [PATCH] MDL-81781 dataformat: public helper method to get writer instance. --- .upgradenotes/MDL-81781-2024060309073788.yml | 7 +++++ lib/classes/dataformat.php | 9 +++--- lib/tablelib.php | 7 ++--- lib/tests/dataformat_test.php | 32 +++++++++++++------- 4 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 .upgradenotes/MDL-81781-2024060309073788.yml diff --git a/.upgradenotes/MDL-81781-2024060309073788.yml b/.upgradenotes/MDL-81781-2024060309073788.yml new file mode 100644 index 00000000000..dd76955e05a --- /dev/null +++ b/.upgradenotes/MDL-81781-2024060309073788.yml @@ -0,0 +1,7 @@ +issueNumber: MDL-81781 +notes: + core: + - message: > + The `\core\dataformat::get_format_instance` method is now public, and + can be used to retrieve a writer instance for a given dataformat + type: changed diff --git a/lib/classes/dataformat.php b/lib/classes/dataformat.php index 70bdcec5a6b..99b92e70428 100644 --- a/lib/classes/dataformat.php +++ b/lib/classes/dataformat.php @@ -25,6 +25,7 @@ namespace core; use coding_exception; +use core\dataformat\base; use core_php_time_limit; use stored_file; @@ -41,15 +42,15 @@ class dataformat { * Return an instance of a dataformat writer from given dataformat type * * @param string $dataformat - * @return dataformat\base - * @throws coding_exception + * @return base + * + * @throws coding_exception For unknown dataformat */ - protected static function get_format_instance(string $dataformat): \core\dataformat\base { + public static function get_format_instance(string $dataformat): base { $classname = 'dataformat_' . $dataformat . '\writer'; if (!class_exists($classname)) { throw new coding_exception('Invalid dataformat', $dataformat); } - return new $classname(); } diff --git a/lib/tablelib.php b/lib/tablelib.php index 463ac019897..4954b4fd802 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -51,6 +51,7 @@ define('TABLE_P_BOTTOM', 2); */ define('TABLE_SHOW_ALL_PAGE_SIZE', 5000); +use core\dataformat; use core_table\local\filter\filterset; /** @@ -2315,11 +2316,7 @@ class table_dataformat_export_format extends table_default_export_format_parent throw new coding_exception("Output can not be buffered before instantiating table_dataformat_export_format"); } - $classname = 'dataformat_' . $dataformat . '\writer'; - if (!class_exists($classname)) { - throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php"); - } - $this->dataformat = new $classname; + $this->dataformat = dataformat::get_format_instance($dataformat); // The dataformat export time to first byte could take a while to generate... set_time_limit(0); diff --git a/lib/tests/dataformat_test.php b/lib/tests/dataformat_test.php index ebd10b0910a..27dc12ef4da 100644 --- a/lib/tests/dataformat_test.php +++ b/lib/tests/dataformat_test.php @@ -14,35 +14,45 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tests for the dataformat plugins - * - * @package core - * @copyright 2020 Paul Holden - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace core; +use coding_exception; use context_system; use core_component; /** - * Dataformat tests + * Tests for the dataformat plugins * * @package core * @covers \core\dataformat * @copyright 2020 Paul Holden * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class dataformat_test extends \advanced_testcase { +final class dataformat_test extends \advanced_testcase { + + /** + * Test getting writer instance for given dataformat + */ + public function test_get_format_instance(): void { + $instance = dataformat::get_format_instance('pdf'); + $this->assertInstanceOf(\dataformat_pdf\writer::class, $instance); + } + + /** + * Test getting writer instance for invalid dataformat + */ + public function test_get_format_instance_invalid(): void { + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Invalid dataformat (weird)'); + dataformat::get_format_instance('weird'); + } /** * Data provider to return array of dataformat types * * @return array */ - public function write_data_provider(): array { + public static function write_data_provider(): array { $data = []; $dataformats = core_component::get_plugin_list('dataformat');