Total Complexity | 40 |
Total Lines | 338 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like OC_Template often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OC_Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class OC_Template extends \OC\Template\Base { |
||
48 | |||
49 | /** @var string */ |
||
50 | private $renderAs; // Create a full page? |
||
51 | |||
52 | /** @var string */ |
||
53 | private $path; // The path to the template |
||
54 | |||
55 | /** @var array */ |
||
56 | private $headers = array(); //custom headers |
||
57 | |||
58 | /** @var string */ |
||
59 | protected $app; // app id |
||
60 | |||
61 | protected static $initTemplateEngineFirstRun = true; |
||
62 | |||
63 | /** |
||
64 | * Constructor |
||
65 | * |
||
66 | * @param string $app app providing the template |
||
67 | * @param string $name of the template file (without suffix) |
||
68 | * @param string $renderAs If $renderAs is set, OC_Template will try to |
||
69 | * produce a full page in the according layout. For |
||
70 | * now, $renderAs can be set to "guest", "user" or |
||
71 | * "admin". |
||
72 | * @param bool $registerCall = true |
||
73 | */ |
||
74 | public function __construct( $app, $name, $renderAs = "", $registerCall = true ) { |
||
75 | // Read the selected theme from the config file |
||
76 | self::initTemplateEngine($renderAs); |
||
77 | |||
78 | $theme = OC_Util::getTheme(); |
||
79 | |||
80 | $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; |
||
81 | |||
82 | $parts = explode('/', $app); // fix translation when app is something like core/lostpassword |
||
83 | $l10n = \OC::$server->getL10N($parts[0]); |
||
84 | /** @var \OCP\Defaults $themeDefaults */ |
||
85 | $themeDefaults = \OC::$server->query(\OCP\Defaults::class); |
||
86 | |||
87 | list($path, $template) = $this->findTemplate($theme, $app, $name); |
||
88 | |||
89 | // Set the private data |
||
90 | $this->renderAs = $renderAs; |
||
91 | $this->path = $path; |
||
92 | $this->app = $app; |
||
93 | |||
94 | parent::__construct($template, $requestToken, $l10n, $themeDefaults); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $renderAs |
||
99 | */ |
||
100 | public static function initTemplateEngine($renderAs) { |
||
101 | if (self::$initTemplateEngineFirstRun){ |
||
102 | |||
103 | //apps that started before the template initialization can load their own scripts/styles |
||
104 | //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true |
||
105 | //meaning the last script/style in this list will be loaded first |
||
106 | if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) { |
||
107 | if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') { |
||
108 | OC_Util::addScript ( 'backgroundjobs', null, true ); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | OC_Util::addStyle('css-variables', null, true); |
||
113 | OC_Util::addStyle('server', null, true); |
||
114 | OC_Util::addStyle('jquery-ui-fixes',null,true); |
||
115 | OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui',null,true); |
||
116 | OC_Util::addVendorStyle('select2/select2', null, true); |
||
117 | OC_Util::addStyle('jquery.ocdialog'); |
||
118 | OC_Util::addTranslations("core", null, true); |
||
119 | OC_Util::addStyle('search', 'results'); |
||
120 | OC_Util::addScript('search', 'search', true); |
||
121 | OC_Util::addScript('search', 'searchprovider'); |
||
122 | OC_Util::addScript('merged-template-prepend', null, true); |
||
123 | OC_Util::addScript('jquery-ui-fixes'); |
||
124 | OC_Util::addScript('files/fileinfo'); |
||
125 | OC_Util::addScript('files/client'); |
||
126 | OC_Util::addScript('contactsmenu'); |
||
127 | OC_Util::addScript('contactsmenu_templates'); |
||
128 | |||
129 | if (\OC::$server->getConfig()->getSystemValue('debug')) { |
||
130 | // Add the stuff we need always |
||
131 | // following logic will import all vendor libraries that are |
||
132 | // specified in core/js/core.json |
||
133 | $fileContent = file_get_contents(OC::$SERVERROOT . '/core/js/core.json'); |
||
134 | if($fileContent !== false) { |
||
135 | $coreDependencies = json_decode($fileContent, true); |
||
136 | foreach(array_reverse($coreDependencies['vendor']) as $vendorLibrary) { |
||
137 | //remove trailing ".js" as addVendorScript will append it |
||
138 | OC_Util::addVendorScript( |
||
139 | substr($vendorLibrary, 0, -3),null,true); |
||
140 | } |
||
141 | } else { |
||
142 | throw new \Exception('Cannot read core/js/core.json'); |
||
143 | } |
||
144 | } else { |
||
145 | // Import all (combined) default vendor libraries |
||
146 | OC_Util::addVendorScript('core', null, true); |
||
147 | } |
||
148 | |||
149 | if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) { |
||
150 | // polyfill for btoa/atob for IE friends |
||
151 | OC_Util::addVendorScript('base64/base64'); |
||
152 | // shim for the davclient.js library |
||
153 | \OCP\Util::addScript('files/iedavclient'); |
||
154 | } |
||
155 | |||
156 | self::$initTemplateEngineFirstRun = false; |
||
157 | } |
||
158 | |||
159 | } |
||
160 | |||
161 | |||
162 | /** |
||
163 | * find the template with the given name |
||
164 | * @param string $name of the template file (without suffix) |
||
165 | * |
||
166 | * Will select the template file for the selected theme. |
||
167 | * Checking all the possible locations. |
||
168 | * @param string $theme |
||
169 | * @param string $app |
||
170 | * @return string[] |
||
171 | */ |
||
172 | protected function findTemplate($theme, $app, $name) { |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Add a custom element to the header |
||
187 | * @param string $tag tag name of the element |
||
188 | * @param array $attributes array of attributes for the element |
||
189 | * @param string $text the text content for the element. If $text is null then the |
||
190 | * element will be written as empty element. So use "" to get a closing tag. |
||
191 | */ |
||
192 | public function addHeader($tag, $attributes, $text=null) { |
||
193 | $this->headers[]= array( |
||
194 | 'tag' => $tag, |
||
195 | 'attributes' => $attributes, |
||
196 | 'text' => $text |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Process the template |
||
202 | * @return boolean|string |
||
203 | * |
||
204 | * This function process the template. If $this->renderAs is set, it |
||
205 | * will produce a full page. |
||
206 | */ |
||
207 | public function fetchPage($additionalParams = null) { |
||
208 | $data = parent::fetchPage($additionalParams); |
||
209 | |||
210 | if( $this->renderAs ) { |
||
211 | $page = new TemplateLayout($this->renderAs, $this->app); |
||
212 | |||
213 | if(is_array($additionalParams)) { |
||
214 | foreach ($additionalParams as $key => $value) { |
||
215 | $page->assign($key, $value); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | // Add custom headers |
||
220 | $headers = ''; |
||
221 | foreach(OC_Util::$headers as $header) { |
||
222 | $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']); |
||
|
|||
223 | if ( strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes']))) ) { |
||
224 | $headers .= ' defer'; |
||
225 | } |
||
226 | foreach($header['attributes'] as $name=>$value) { |
||
227 | $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"'; |
||
228 | } |
||
229 | if ($header['text'] !== null) { |
||
230 | $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>'; |
||
231 | } else { |
||
232 | $headers .= '/>'; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | $page->assign('headers', $headers); |
||
237 | |||
238 | $page->assign('content', $data); |
||
239 | return $page->fetchPage($additionalParams); |
||
240 | } |
||
241 | |||
242 | return $data; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Include template |
||
247 | * |
||
248 | * @param string $file |
||
249 | * @param array|null $additionalParams |
||
250 | * @return string returns content of included template |
||
251 | * |
||
252 | * Includes another template. use <?php echo $this->inc('template'); ?> to |
||
253 | * do this. |
||
254 | */ |
||
255 | public function inc( $file, $additionalParams = null ) { |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Shortcut to print a simple page for users |
||
261 | * @param string $application The application we render the template for |
||
262 | * @param string $name Name of the template |
||
263 | * @param array $parameters Parameters for the template |
||
264 | * @return boolean|null |
||
265 | */ |
||
266 | public static function printUserPage( $application, $name, $parameters = array() ) { |
||
267 | $content = new OC_Template( $application, $name, "user" ); |
||
268 | foreach( $parameters as $key => $value ) { |
||
269 | $content->assign( $key, $value ); |
||
270 | } |
||
271 | print $content->printPage(); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Shortcut to print a simple page for admins |
||
276 | * @param string $application The application we render the template for |
||
277 | * @param string $name Name of the template |
||
278 | * @param array $parameters Parameters for the template |
||
279 | * @return bool |
||
280 | */ |
||
281 | public static function printAdminPage( $application, $name, $parameters = array() ) { |
||
282 | $content = new OC_Template( $application, $name, "admin" ); |
||
283 | foreach( $parameters as $key => $value ) { |
||
284 | $content->assign( $key, $value ); |
||
285 | } |
||
286 | return $content->printPage(); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Shortcut to print a simple page for guests |
||
291 | * @param string $application The application we render the template for |
||
292 | * @param string $name Name of the template |
||
293 | * @param array|string $parameters Parameters for the template |
||
294 | * @return bool |
||
295 | */ |
||
296 | public static function printGuestPage( $application, $name, $parameters = array() ) { |
||
297 | $content = new OC_Template( $application, $name, "guest" ); |
||
298 | foreach( $parameters as $key => $value ) { |
||
299 | $content->assign( $key, $value ); |
||
300 | } |
||
301 | return $content->printPage(); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Print a fatal error page and terminates the script |
||
306 | * @param string $error_msg The error message to show |
||
307 | * @param string $hint An optional hint message - needs to be properly escape |
||
308 | * @param int $statusCode |
||
309 | * @suppress PhanAccessMethodInternal |
||
310 | */ |
||
311 | public static function printErrorPage( $error_msg, $hint = '', $statusCode = 500) { |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * print error page using Exception details |
||
341 | * @param Exception|Throwable $exception |
||
342 | * @param int $statusCode |
||
343 | * @return bool|string |
||
344 | * @suppress PhanAccessMethodInternal |
||
345 | */ |
||
346 | public static function printExceptionErrorPage($exception, $statusCode = 503) { |
||
385 | } |
||
386 | } |
||
387 |