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:
commit
cae849bcf9
|
@ -48,6 +48,7 @@ use OC\AppFramework\Http\Request;
|
||||||
use OC\Template\JSCombiner;
|
use OC\Template\JSCombiner;
|
||||||
use OC\Template\JSConfigHelper;
|
use OC\Template\JSConfigHelper;
|
||||||
use OC\Template\SCSSCacher;
|
use OC\Template\SCSSCacher;
|
||||||
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\Defaults;
|
use OCP\Defaults;
|
||||||
use OCP\IInitialStateService;
|
use OCP\IInitialStateService;
|
||||||
use OCP\Support\Subscription\IRegistry;
|
use OCP\Support\Subscription\IRegistry;
|
||||||
|
@ -74,7 +75,7 @@ class TemplateLayout extends \OC_Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decide which page we show
|
// Decide which page we show
|
||||||
if ($renderAs === 'user') {
|
if ($renderAs === TemplateResponse::RENDER_AS_USER) {
|
||||||
parent::__construct('core', 'layout.user');
|
parent::__construct('core', 'layout.user');
|
||||||
if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
|
if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
|
||||||
$this->assign('bodyid', 'body-settings');
|
$this->assign('bodyid', 'body-settings');
|
||||||
|
@ -123,12 +124,12 @@ class TemplateLayout extends \OC_Template {
|
||||||
} catch (\OCP\AutoloadNotAllowedException $e) {
|
} catch (\OCP\AutoloadNotAllowedException $e) {
|
||||||
$this->assign('themingInvertMenu', false);
|
$this->assign('themingInvertMenu', false);
|
||||||
}
|
}
|
||||||
} elseif ($renderAs === 'error') {
|
} elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
|
||||||
parent::__construct('core', 'layout.guest', '', false);
|
parent::__construct('core', 'layout.guest', '', false);
|
||||||
$this->assign('bodyid', 'body-login');
|
$this->assign('bodyid', 'body-login');
|
||||||
$this->assign('user_displayname', '');
|
$this->assign('user_displayname', '');
|
||||||
$this->assign('user_uid', '');
|
$this->assign('user_uid', '');
|
||||||
} elseif ($renderAs === 'guest') {
|
} elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
|
||||||
parent::__construct('core', 'layout.guest');
|
parent::__construct('core', 'layout.guest');
|
||||||
\OC_Util::addStyle('guest');
|
\OC_Util::addStyle('guest');
|
||||||
$this->assign('bodyid', 'body-login');
|
$this->assign('bodyid', 'body-login');
|
||||||
|
@ -136,7 +137,7 @@ class TemplateLayout extends \OC_Template {
|
||||||
$userDisplayName = \OC_User::getDisplayName();
|
$userDisplayName = \OC_User::getDisplayName();
|
||||||
$this->assign('user_displayname', $userDisplayName);
|
$this->assign('user_displayname', $userDisplayName);
|
||||||
$this->assign('user_uid', \OC_User::getUser());
|
$this->assign('user_uid', \OC_User::getUser());
|
||||||
} elseif ($renderAs === 'public') {
|
} elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
|
||||||
parent::__construct('core', 'layout.public');
|
parent::__construct('core', 'layout.public');
|
||||||
$this->assign('appid', $appId);
|
$this->assign('appid', $appId);
|
||||||
$this->assign('bodyid', 'body-public');
|
$this->assign('bodyid', 'body-public');
|
||||||
|
@ -172,7 +173,7 @@ class TemplateLayout extends \OC_Template {
|
||||||
// Add the js files
|
// Add the js files
|
||||||
$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
|
$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
|
||||||
$this->assign('jsfiles', []);
|
$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()) {
|
if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
|
||||||
$jsConfigHelper = new JSConfigHelper(
|
$jsConfigHelper = new JSConfigHelper(
|
||||||
\OC::$server->getL10N('lib'),
|
\OC::$server->getL10N('lib'),
|
||||||
|
@ -210,7 +211,7 @@ class TemplateLayout extends \OC_Template {
|
||||||
&& !\OCP\Util::needUpgrade()
|
&& !\OCP\Util::needUpgrade()
|
||||||
&& $pathInfo !== ''
|
&& $pathInfo !== ''
|
||||||
&& !preg_match('/^\/login/', $pathInfo)
|
&& !preg_match('/^\/login/', $pathInfo)
|
||||||
&& $renderAs !== 'error'
|
&& $renderAs !== TemplateResponse::RENDER_AS_ERROR
|
||||||
) {
|
) {
|
||||||
$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
|
$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use OC\TemplateLayout;
|
use OC\TemplateLayout;
|
||||||
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
|
|
||||||
require_once __DIR__.'/template/functions.php';
|
require_once __DIR__.'/template/functions.php';
|
||||||
|
|
||||||
|
@ -72,7 +73,7 @@ class OC_Template extends \OC\Template\Base {
|
||||||
* "admin".
|
* "admin".
|
||||||
* @param bool $registerCall = true
|
* @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
|
// Read the selected theme from the config file
|
||||||
self::initTemplateEngine($renderAs);
|
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
|
//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
|
//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
|
//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') {
|
if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
|
||||||
OC_Util::addScript('backgroundjobs', null, true);
|
OC_Util::addScript('backgroundjobs', null, true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,27 @@ namespace OCP\AppFramework\Http;
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
class TemplateResponse extends Response {
|
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
|
* @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
|
* @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
|
||||||
*/
|
*/
|
||||||
public function __construct($appName, $templateName, array $params=[],
|
public function __construct($appName, $templateName, array $params=[],
|
||||||
$renderAs='user') {
|
$renderAs = self::RENDER_AS_USER) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->templateName = $templateName;
|
$this->templateName = $templateName;
|
||||||
|
@ -160,8 +181,18 @@ class TemplateResponse extends Response {
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public function render() {
|
public function render() {
|
||||||
// \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
|
$renderAs = self::RENDER_AS_USER;
|
||||||
$renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs;
|
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']);
|
\OCP\Util::addHeader('meta', ['name' => 'robots', 'content' => 'noindex, nofollow']);
|
||||||
$template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
|
$template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
|
||||||
|
|
Loading…
Reference in New Issue