Merge pull request #21869 from nextcloud/techdebt/noid/add-constants-for-magic-strings

Add constants for the magic strings of template rendering
This commit is contained in:
Joas Schilling 2020-07-16 22:00:57 +02:00 committed by GitHub
commit cae849bcf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 11 deletions

View File

@ -48,6 +48,7 @@ use OC\AppFramework\Http\Request;
use OC\Template\JSCombiner;
use OC\Template\JSConfigHelper;
use OC\Template\SCSSCacher;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults;
use OCP\IInitialStateService;
use OCP\Support\Subscription\IRegistry;
@ -74,7 +75,7 @@ class TemplateLayout extends \OC_Template {
}
// Decide which page we show
if ($renderAs === 'user') {
if ($renderAs === TemplateResponse::RENDER_AS_USER) {
parent::__construct('core', 'layout.user');
if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
$this->assign('bodyid', 'body-settings');
@ -123,12 +124,12 @@ class TemplateLayout extends \OC_Template {
} catch (\OCP\AutoloadNotAllowedException $e) {
$this->assign('themingInvertMenu', false);
}
} elseif ($renderAs === 'error') {
} elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
parent::__construct('core', 'layout.guest', '', false);
$this->assign('bodyid', 'body-login');
$this->assign('user_displayname', '');
$this->assign('user_uid', '');
} elseif ($renderAs === 'guest') {
} elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
parent::__construct('core', 'layout.guest');
\OC_Util::addStyle('guest');
$this->assign('bodyid', 'body-login');
@ -136,7 +137,7 @@ class TemplateLayout extends \OC_Template {
$userDisplayName = \OC_User::getDisplayName();
$this->assign('user_displayname', $userDisplayName);
$this->assign('user_uid', \OC_User::getUser());
} elseif ($renderAs === 'public') {
} elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
parent::__construct('core', 'layout.public');
$this->assign('appid', $appId);
$this->assign('bodyid', 'body-public');
@ -172,7 +173,7 @@ class TemplateLayout extends \OC_Template {
// Add the js files
$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
$this->assign('jsfiles', []);
if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
$jsConfigHelper = new JSConfigHelper(
\OC::$server->getL10N('lib'),
@ -210,7 +211,7 @@ class TemplateLayout extends \OC_Template {
&& !\OCP\Util::needUpgrade()
&& $pathInfo !== ''
&& !preg_match('/^\/login/', $pathInfo)
&& $renderAs !== 'error'
&& $renderAs !== TemplateResponse::RENDER_AS_ERROR
) {
$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
} else {

View File

@ -39,6 +39,7 @@
*/
use OC\TemplateLayout;
use OCP\AppFramework\Http\TemplateResponse;
require_once __DIR__.'/template/functions.php';
@ -72,7 +73,7 @@ class OC_Template extends \OC\Template\Base {
* "admin".
* @param bool $registerCall = true
*/
public function __construct($app, $name, $renderAs = "", $registerCall = true) {
public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS_BLANK, $registerCall = true) {
// Read the selected theme from the config file
self::initTemplateEngine($renderAs);
@ -104,7 +105,7 @@ class OC_Template extends \OC\Template\Base {
//apps that started before the template initialization can load their own scripts/styles
//so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true
//meaning the last script/style in this list will be loaded first
if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== TemplateResponse::RENDER_AS_ERROR && !\OCP\Util::needUpgrade()) {
if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
OC_Util::addScript('backgroundjobs', null, true);
}

View File

@ -38,6 +38,27 @@ namespace OCP\AppFramework\Http;
* @since 6.0.0
*/
class TemplateResponse extends Response {
/**
* @since 20.0.0
*/
public const RENDER_AS_GUEST = 'guest';
/**
* @since 20.0.0
*/
public const RENDER_AS_BLANK = '';
/**
* @since 20.0.0
*/
public const RENDER_AS_USER = 'user';
/**
* @since 20.0.0
*/
public const RENDER_AS_ERROR = 'error';
/**
* @since 20.0.0
*/
public const RENDER_AS_PUBLIC = 'public';
/**
* @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent
*/
@ -81,7 +102,7 @@ class TemplateResponse extends Response {
* @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
*/
public function __construct($appName, $templateName, array $params=[],
$renderAs='user') {
$renderAs = self::RENDER_AS_USER) {
parent::__construct();
$this->templateName = $templateName;
@ -160,8 +181,18 @@ class TemplateResponse extends Response {
* @since 6.0.0
*/
public function render() {
// \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
$renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs;
$renderAs = self::RENDER_AS_USER;
if ($this->renderAs === 'blank') {
// Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
$renderAs = self::RENDER_AS_BLANK;
} elseif (in_array($this->renderAs, [
self::RENDER_AS_GUEST,
self::RENDER_AS_BLANK,
self::RENDER_AS_ERROR,
self::RENDER_AS_PUBLIC,
self::RENDER_AS_USER], true)) {
$renderAs = $this->renderAs;
}
\OCP\Util::addHeader('meta', ['name' => 'robots', 'content' => 'noindex, nofollow']);
$template = new \OCP\Template($this->appName, $this->templateName, $renderAs);