* @param $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
* @return object an object implementing the requested renderer interface.
*/
- public function get_renderer($module);
+ public function get_renderer($module, $page);
}
* @since Moodle 2.0
*/
abstract class renderer_factory_base implements renderer_factory {
- /** The theme we are rendering for. */
+ /** @var theme_config the theme we belong to. */
protected $theme;
- /** The page we are doing output for. */
- protected $page;
-
- /** Used to cache renderers as they are created. */
- protected $renderers = array();
-
- protected $opencontainers;
-
/**
* Constructor.
- * @param object $theme the theme we are rendering for.
- * @param moodle_page $page the page we are doing output for.
+ * @param theme_config $theme the theme we belong to.
*/
- public function __construct($theme, $page) {
+ public function __construct($theme) {
$this->theme = $theme;
- $this->page = $page;
- $this->opencontainers = new xhtml_container_stack();
}
-
- /* Implement the interface method. */
- public function get_renderer($module) {
- // Cache the renderers by module name, and delegate the actual
- // construction to the create_renderer method.
- if (!array_key_exists($module, $this->renderers)) {
- $this->renderers[$module] = $this->create_renderer($module);
- }
-
- return $this->renderers[$module];
- }
-
- /**
- * Subclasses should override this method to actually create an instance of
- * the appropriate renderer class, based on the module name. That is,
- * this method should implement the same contract as
- * {@link renderer_factory::get_renderer}.
- *
- * @param $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
- * @return object an object implementing the requested renderer interface.
- */
- abstract public function create_renderer($module);
-
/**
* For a given module name, return the name of the standard renderer class
* that defines the renderer interface for that module.
* @since Moodle 2.0
*/
class standard_renderer_factory extends renderer_factory_base {
- /**
- * Constructor.
- * @param object $theme the theme we are rendering for.
- * @param moodle_page $page the page we are doing output for.
- */
- public function __construct($theme, $page) {
- parent::__construct($theme, $page);
- }
-
/* Implement the subclass method. */
- public function create_renderer($module) {
+ public function get_renderer($module, $page) {
if ($module == 'core') {
- return new moodle_core_renderer($this->opencontainers, $this->page, $this);
+ return new moodle_core_renderer($page);
} else {
$class = $this->standard_renderer_class_for_module($module);
- return new $class($this->opencontainers, $this->get_renderer('core'), $this->page);
+ return new $class($page, $this->get_renderer('core', $page));
}
}
}
* @since Moodle 2.0
*/
class cli_renderer_factory extends standard_renderer_factory {
- /**
- * Constructor.
- * @param object $theme the theme we are rendering for.
- * @param moodle_page $page the page we are doing output for.
- */
- public function __construct($theme, $page) {
- parent::__construct($theme, $page);
- $this->renderers = array('core' => new cli_core_renderer($this->opencontainers, $this->page, $this));
+ /* Implement the subclass method. */
+ public function get_renderer($module, $page) {
+ if ($module == 'core') {
+ return new cli_core_renderer($page);
+ } else {
+ parent::get_renderer($module, $page);
+ }
}
}
* @param object $theme the theme we are rendering for.
* @param moodle_page $page the page we are doing output for.
*/
- public function __construct($theme, $page) {
+ public function __construct($theme) {
global $CFG;
- parent::__construct($theme, $page);
+ parent::__construct($theme);
// Initialise $this->prefixes.
$renderersfile = $theme->dir . '/renderers.php';
}
/* Implement the subclass method. */
- public function create_renderer($module) {
+ public function get_renderer($module, $page) {
foreach ($this->prefixes as $prefix) {
$classname = $prefix . $module . '_renderer';
if (class_exists($classname)) {
if ($module == 'core') {
- return new $classname($this->opencontainers, $this->page, $this);
+ return new $classname($page);
} else {
- return new $classname($this->opencontainers, $this->get_renderer('core'), $this->page);
+ return new $classname($page, $this->get_renderer('core', $page));
}
}
}
- return parent::create_renderer($module);
+ return parent::get_renderer($module, $page);
}
}
* @param object $theme the theme we are rendering for.
* @param moodle_page $page the page we are doing output for.
*/
- public function __construct($theme, $page) {
+ public function __construct($theme) {
global $CFG;
- parent::__construct($theme, $page);
+ parent::__construct($theme);
// Initialise $this->searchpaths.
if ($theme->name != 'standardtemplate') {
}
/* Implement the subclass method. */
- public function create_renderer($module) {
+ public function get_renderer($module, $page) {
// Refine the list of search paths for this module.
$searchpaths = array();
foreach ($this->searchpaths as $rootpath) {
// Create a template_renderer that copies the API of the standard renderer.
$copiedclass = $this->standard_renderer_class_for_module($module);
- return new template_renderer($copiedclass, $searchpaths, $this->opencontainers, $this->page, $this);
+ return new template_renderer($copiedclass, $searchpaths, $page);
}
}
* @param $opencontainers the xhtml_container_stack to use.
* @param moodle_page $page the page we are doing output for.
*/
- public function __construct($opencontainers, $page) {
- $this->opencontainers = $opencontainers;
+ public function __construct($page) {
+ $this->opencontainers = $page->opencontainers;
$this->page = $page;
}
* @param $searchpaths a list of folders to search for templates in.
* @param $opencontainers the xhtml_container_stack to use.
* @param moodle_page $page the page we are doing output for.
- * @param renderer_factory $rendererfactory the renderer factory that created us.
*/
- public function __construct($copiedclass, $searchpaths, $opencontainers, $page, $rendererfactory) {
- parent::__construct($opencontainers, $page);
+ public function __construct($copiedclass, $searchpaths, $page) {
+ parent::__construct($page);
$this->copiedclass = new ReflectionClass($copiedclass);
$this->searchpaths = $searchpaths;
- $this->rendererfactory = $rendererfactory;
- }
-
- /**
- * Get a renderer for another part of Moodle.
- * @param $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
- * @return object an object implementing the requested renderer interface.
- */
- public function get_other_renderer($module) {
- $this->rendererfactory->get_renderer($module);
}
/* PHP magic method implementation. */
const END_HTML_TOKEN = '%%ENDHTML%%';
const MAIN_CONTENT_TOKEN = '[MAIN CONTENT GOES HERE]';
protected $contenttype;
- protected $rendererfactory;
protected $metarefreshtag = '';
- /**
- * Constructor
- * @param $opencontainers the xhtml_container_stack to use.
- * @param moodle_page $page the page we are doing output for.
- * @param renderer_factory $rendererfactory the renderer factory that created us.
- */
- public function __construct($opencontainers, $page, $rendererfactory) {
- parent::__construct($opencontainers, $page);
- $this->rendererfactory = $rendererfactory;
- }
-
- /**
- * Get a renderer for another part of Moodle.
- * @param $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
- * @return object an object implementing the requested renderer interface.
- */
- public function get_other_renderer($module) {
- $this->rendererfactory->get_renderer($module);
- }
public function doctype() {
global $CFG;
/// added a block when anther theme was selected).
////////////////////////////////////////////////////////////////////////////////
- /**
- * @var string the name of this theme. Set automatically.
- */
+ /** @var string the name of this theme. Set automatically. */
public $name;
- /**
- * @var string the folder where this themes fiels are stored. $CFG->themedir . '/' . $this->name
- */
+ /** @var string the folder where this themes fiels are stored. $CFG->themedir . '/' . $this->name */
public $dir;
+ /** @var string Name of the renderer factory class to use. */
public $rendererfactory = 'standard_renderer_factory';
+ /** @var renderer_factory Instance of the renderer_factory class. */
+ protected $rf = null;
/**
* If you want to do custom processing on the CSS before it is output (for
* @return theme_config an instance of this class.
*/
public static function load($themename) {
- global $CFG, $PAGE;
+ global $CFG;
// We have to use the variable name $THEME (upper case) becuase that
// is what is used in theme config.php files.
return $THEME;
}
+ /**
+ * Get the renderer for a part of Moodle for this theme.
+ * @param string $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
+ * @param moodle_page $page the page we are rendering
+ * @return moodle_renderer_base the requested renderer.
+ */
+ public function get_renderer($module, $page) {
+ if (is_null($this->rf)) {
+ if (CLI_SCRIPT) {
+ $classname = 'cli_renderer_factory';
+ } else {
+ $classname = $this->rendererfactory;
+ }
+ $this->rf = new $classname($this);
+ }
+
+ return $this->rf->get_renderer($module, $page);
+ }
+
/**
* Set the variable $CFG->pixpath and $CFG->modpixpath to be the right
* ones for this theme.
public $createcalls = array();
public function __construct() {
- parent::__construct(null, null);
+ parent::__construct(null);
}
- public function create_renderer($module) {
+ public function get_renderer($module, $page) {
$this->createcalls[] = $module;
- return new moodle_core_renderer(new xhtml_container_stack(), null, null);
+ return new moodle_core_renderer($page);
}
public function standard_renderer_class_for_module($module) {
// Set up.
$factory = new testable_renderer_factory();
// Exercise SUT.
- $renderer = $factory->get_renderer('modulename');
+ $renderer = $factory->get_renderer('modulename', new moodle_page);
// Verify outcome
$this->assertEqual(array('modulename'), $factory->createcalls);
}
- public function test_get_caches_repeat_calls() {
- // Set up.
- $factory = new testable_renderer_factory();
- // Exercise SUT.
- $renderer1 = $factory->get_renderer('modulename');
- $renderer2 = $factory->get_renderer('modulename');
- // Verify outcome
- $this->assertEqual(array('modulename'), $factory->createcalls);
- $this->assertIdentical($renderer1, $renderer2);
- }
-
public function test_standard_renderer_class_for_module_core() {
// Set up.
$factory = new testable_renderer_factory();
}
public function test_get_core_renderer() {
- $renderer = $this->factory->get_renderer('core');
+ $renderer = $this->factory->get_renderer('core', new moodle_page);
$this->assertIsA($renderer, 'moodle_core_renderer');
}
public function test_get_test_renderer() {
- $renderer = $this->factory->get_renderer('test');
+ $renderer = $this->factory->get_renderer('test', new moodle_page);
$this->assertIsA($renderer, 'moodle_test_renderer');
}
}
}
public function test_get_core_renderer() {
- $renderer = $this->factory->get_renderer('core');
+ $renderer = $this->factory->get_renderer('core', new moodle_page);
$this->assertIsA($renderer, 'custom_corners_core_renderer');
}
public function test_get_test_renderer() {
- $renderer = $this->factory->get_renderer('test');
+ $renderer = $this->factory->get_renderer('test', new moodle_page);
$this->assertIsA($renderer, 'moodle_test_renderer');
}
}
$factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
// Exercise SUT.
- $renderer = $factory->get_renderer('test');
+ $renderer = $factory->get_renderer('test', new moodle_page);
// Verify outcome
$this->assertIsA($renderer, 'moodle_test_renderer');
$factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
// Exercise SUT.
- $renderer = $factory->get_renderer('test');
+ $renderer = $factory->get_renderer('test', new moodle_page);
// Verify outcome
$this->assertIsA($renderer, 'testrenderertheme_test_renderer');
$factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
// Exercise SUT.
- $renderer = $factory->get_renderer('core');
+ $renderer = $factory->get_renderer('core', new moodle_page);
// Verify outcome
$this->assertIsA($renderer, 'parentrenderertheme_core_renderer');
$factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
// Exercise SUT.
- $renderer = $factory->get_renderer('core');
+ $renderer = $factory->get_renderer('core', new moodle_page);
// Verify outcome
$this->assertIsA($renderer, 'ctheme_core_renderer');
$CFG->themedir = $CFG->dataroot . '/' . $this->workspace;
$this->foldertocleanup = $CFG->themedir;
- $this->page = new stdClass;
+ $this->page = new moodle_page;
}
public function tearDown() {
$this->make_theme_template_dir('mytheme', 'core');
$this->make_theme_template_dir('parenttheme', 'test');
$this->make_theme_template_dir('standardtemplate', 'test');
- $factory = new testable_template_renderer_factory($theme, $this->page);
+ $factory = new testable_template_renderer_factory($theme);
// Exercise SUT.
- $renderer = $factory->get_renderer('test');
+ $renderer = $factory->get_renderer('test', $this->page);
// Verify outcome
$this->assertEqual('moodle_test_renderer', $renderer->get_copied_class());
parent::setUp();
$this->templatefolder = $CFG->dataroot . '/temp/template_renderer_fixtures/test';
make_upload_directory('temp/template_renderer_fixtures/test');
- $page = new stdClass;
- $page->course = new stdClass;
+ $page = new moodle_page;
$this->renderer = new template_renderer('moodle_test_renderer',
- array($this->templatefolder), new xhtml_container_stack(), $page, null);
- $this->savedtemplates = array();
+ array($this->templatefolder), $page);
}
public function tearDown() {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_core_renderer_test extends UnitTestCase {
- protected $containerstack;
protected $renderer;
public function setUp() {
parent::setUp();
- $this->containerstack = new xhtml_container_stack();
- $this->renderer = new moodle_core_renderer($this->containerstack, null, null);
+ $this->renderer = new moodle_core_renderer(new moodle_page);
}
public function test_select_menu_simple() {