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'); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 3f180ecd79b..ce3d25e93cf 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -1,6 +1,11 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 4.4.2 === + +* The `\core\dataformat::get_format_instance` method is now public, and can be used to retrieve a writer instance for + a given dataformat + === 4.4.1 === * Use server timezone when constructing `\DateTimeImmutable` for the system `\core\clock` implementation.