MDL-81781 dataformat: public helper method to get writer instance.

This commit is contained in:
Paul Holden 2024-05-07 17:14:26 +01:00
parent d3ae1391ab
commit a0d5133f10
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
4 changed files with 35 additions and 20 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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);

View File

@ -14,35 +14,45 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests for the dataformat plugins
*
* @package core
* @copyright 2020 Paul Holden <paulh@moodle.com>
* @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 <paulh@moodle.com>
* @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');