on-demand release 4.4dev+
[moodle.git] / h5p / classes / factory.php
blobcc5db1ed15f2fa80e27e2302d961e02a8503ca42
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * H5P factory class.
19 * This class is used to decouple the construction of H5P related objects.
21 * @package core_h5p
22 * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core_h5p;
28 defined('MOODLE_INTERNAL') || die();
30 use core_h5p\local\library\autoloader;
31 use Moodle\H5PContentValidator as content_validator;
32 use Moodle\H5peditor;
33 use Moodle\H5PStorage as storage;
34 use Moodle\H5PValidator as validator;
36 /**
37 * H5P factory class.
38 * This class is used to decouple the construction of H5P related objects.
40 * @package core_h5p
41 * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class factory {
46 /** @var \core_h5p\local\library\autoloader The autoloader */
47 protected $autoloader;
49 /** @var \core_h5p\core The Moodle H5PCore implementation */
50 protected $core;
52 /** @var \core_h5p\framework The Moodle H5PFramework implementation */
53 protected $framework;
55 /** @var \core_h5p\file_storage The Moodle H5PStorage implementation */
56 protected $storage;
58 /** @var validator The Moodle H5PValidator implementation */
59 protected $validator;
61 /** @var content_validator The Moodle H5PContentValidator implementation */
62 protected $content_validator;
64 /** @var editor_framework The Moodle H5peditorStorage implementation */
65 protected $editorframework;
67 /** @var H5peditor */
68 protected $editor;
70 /** @var editor_ajax The Moodle H5PEditorAjaxInterface implementation */
71 protected $editorajaxinterface;
73 /**
74 * factory constructor.
76 public function __construct() {
77 // Loading classes we need from H5P third party library.
78 $this->autoloader = new autoloader();
79 autoloader::register();
82 /**
83 * Returns an instance of the \core_h5p\local\library\autoloader class.
85 * @return \core_h5p\local\library\autoloader
87 public function get_autoloader(): autoloader {
88 return $this->autoloader;
91 /**
92 * Returns an instance of the \core_h5p\framework class.
94 * @return \core_h5p\framework
96 public function get_framework(): framework {
97 if (null === $this->framework) {
98 $this->framework = new framework();
101 return $this->framework;
105 * Returns an instance of the \core_h5p\core class.
107 * @return \core_h5p\core
109 public function get_core(): core {
110 if (null === $this->core) {
111 $fs = new \core_h5p\file_storage();
112 $language = \core_h5p\framework::get_language();
113 $context = \context_system::instance();
115 $url = \moodle_url::make_pluginfile_url($context->id, 'core_h5p', '', null,
116 '', '')->out();
118 $this->core = new core($this->get_framework(), $fs, $url, $language, true);
121 return $this->core;
125 * Returns an instance of the H5PStorage class.
127 * @return \Moodle\H5PStorage
129 public function get_storage(): storage {
130 if (null === $this->storage) {
131 $this->storage = new storage($this->get_framework(), $this->get_core());
134 return $this->storage;
138 * Returns an instance of the H5PValidator class.
140 * @return \Moodle\H5PValidator
142 public function get_validator(): validator {
143 if (null === $this->validator) {
144 $this->validator = new validator($this->get_framework(), $this->get_core());
147 return $this->validator;
151 * Returns an instance of the H5PContentValidator class.
153 * @return Moodle\H5PContentValidator
155 public function get_content_validator(): content_validator {
156 if (null === $this->content_validator) {
157 $this->content_validator = new content_validator($this->get_framework(), $this->get_core());
160 return $this->content_validator;
164 * Returns an instance of H5Peditor class.
166 * @return H5peditor
168 public function get_editor(): H5peditor {
169 if (null === $this->editor) {
170 if (empty($this->editorframework)) {
171 $this->editorframework = new editor_framework();
174 if (empty($this->editorajaxinterface)) {
175 $this->editorajaxinterface = new editor_ajax();
178 if (empty($this->editor)) {
179 $this->editor = new H5peditor($this->get_core(), $this->editorframework, $this->editorajaxinterface);
183 return $this->editor;