MDL-57316 media: create different instances for different pages

This commit is contained in:
Marina Glancy 2017-02-07 09:35:46 +08:00
parent 1abd43763c
commit 63c102e095
2 changed files with 30 additions and 10 deletions

View File

@ -457,6 +457,23 @@ class core_medialib_testcase extends advanced_testcase {
$this->assertContains('<source src="http://example.org/test.webm"', $t);
$this->assertNotContains('<source src="http://example.org/test.flv"', $t);
}
/**
* Make sure the instance() method returns singleton for the same page and different object for another page
*/
public function test_initialise() {
$moodlepage1 = new moodle_page();
$mediamanager1 = core_media_manager::instance($moodlepage1);
$mediamanager2 = core_media_manager::instance($moodlepage1);
$this->assertSame($mediamanager1, $mediamanager2);
$moodlepage3 = new moodle_page();
$mediamanager3 = core_media_manager::instance($moodlepage3);
$this->assertNotSame($mediamanager1, $mediamanager3);
}
}
/**
@ -522,6 +539,7 @@ class core_media_manager_test extends core_media_manager {
* Override the constructor to access it.
*/
public function __construct() {
parent::__construct();
global $PAGE;
parent::__construct($PAGE);
}
}

View File

@ -97,6 +97,9 @@ class core_media_manager {
/** @var core_media_manager caches a singleton instance */
static protected $instance;
/** @var moodle_page page this instance was initialised for */
protected $page;
/**
* Returns a singleton instance of a manager
*
@ -105,7 +108,12 @@ class core_media_manager {
* @return core_media_manager
*/
public static function instance($page = null) {
if (self::$instance === null) {
// Use the passed $page if given, otherwise the $PAGE global.
if (!$page) {
global $PAGE;
$page = $PAGE;
}
if (self::$instance === null || ($page && self::$instance->page !== $page)) {
self::$instance = new self($page);
}
return self::$instance;
@ -117,15 +125,9 @@ class core_media_manager {
* @param moodle_page $page The page we are going to add requirements to.
* @see core_media_manager::instance()
*/
protected function __construct($page = null) {
// Use the passed $page if given, otherwise the $PAGE global.
if ($page == null) {
global $PAGE;
if (isset($PAGE)) {
$page = $PAGE;
}
}
protected function __construct($page) {
if ($page) {
$this->page = $page;
$players = $this->get_players();
foreach ($players as $player) {
$player->setup($page);