Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Deprecate OC_Template, add proper template manager instead #51029

Merged
merged 10 commits into from
Mar 10, 2025
5 changes: 3 additions & 2 deletions apps/dav/lib/Files/BrowserErrorPagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
namespace OCA\DAV\Files;

use OC\AppFramework\Http\Request;
use OC_Template;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;
use OCP\Template\ITemplateManager;
use Sabre\DAV\Exception;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function generateBody(int $httpCode) {
$templateName = (string)$httpCode;
}

$content = new OC_Template('core', $templateName, 'guest');
$content = \OCP\Server::get(ITemplateManager::class)->getTemplate('core', $templateName, TemplateResponse::RENDER_AS_GUEST);
$content->assign('title', $this->server->httpResponse->getStatusText());
$content->assign('remoteAddr', $request->getRemoteAddress());
$content->assign('requestID', $request->getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IUser;
use OCP\Template;
use OCP\Template\ITemplate;
use OCP\Template\ITemplateManager;

class BackupCodesProvider implements IDeactivatableByAdmin, IProvidesPersonalSettings {

Expand All @@ -36,6 +37,7 @@ public function __construct(
private IL10N $l10n,
AppManager $appManager,
private IInitialStateService $initialStateService,
private ITemplateManager $templateManager,
) {
$this->appManager = $appManager;
}
Expand Down Expand Up @@ -71,10 +73,10 @@ public function getDescription(): string {
* Get the template for rending the 2FA provider view
*
* @param IUser $user
* @return Template
* @return ITemplate
*/
public function getTemplate(IUser $user): Template {
return new Template('twofactor_backupcodes', 'challenge');
public function getTemplate(IUser $user): ITemplate {
return $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge');
}

/**
Expand Down
9 changes: 6 additions & 3 deletions apps/twofactor_backupcodes/lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\TwoFactorBackupCodes\Settings;

use OCP\Authentication\TwoFactorAuth\IPersonalProviderSettings;
use OCP\Template;
use OCP\Server;
use OCP\Template\ITemplate;
use OCP\Template\ITemplateManager;

class Personal implements IPersonalProviderSettings {
public function getBody(): Template {
return new Template('twofactor_backupcodes', 'personal');
public function getBody(): ITemplate {
return Server::get(ITemplateManager::class)->getTemplate('twofactor_backupcodes', 'personal');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IUser;
use OCP\Template;
use OCP\Server;
use OCP\Template\ITemplateManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class BackupCodesProviderTest extends TestCase {
private string $appName;

/** @var string */
private $appName;
private BackupCodeStorage&MockObject $storage;
private IL10N&MockObject $l10n;
private AppManager&MockObject $appManager;
private IInitialStateService&MockObject $initialState;

/** @var BackupCodeStorage|\PHPUnit\Framework\MockObject\MockObject */
private $storage;

/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
private $l10n;

/** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */
private $appManager;

/** @var IInitialStateService|\PHPUnit\Framework\MockObject\MockObject */
private $initialState;

/** @var BackupCodesProvider */
private $provider;
private ITemplateManager $templateManager;
private BackupCodesProvider $provider;

protected function setUp(): void {
parent::setUp();
Expand All @@ -45,8 +38,16 @@ protected function setUp(): void {
$this->l10n = $this->createMock(IL10N::class);
$this->appManager = $this->createMock(AppManager::class);
$this->initialState = $this->createMock(IInitialStateService::class);

$this->provider = new BackupCodesProvider($this->appName, $this->storage, $this->l10n, $this->appManager, $this->initialState);
$this->templateManager = Server::get(ITemplateManager::class);

$this->provider = new BackupCodesProvider(
$this->appName,
$this->storage,
$this->l10n,
$this->appManager,
$this->initialState,
$this->templateManager,
);
}

public function testGetId(): void {
Expand All @@ -71,7 +72,7 @@ public function testGetDescription(): void {

public function testGetTempalte(): void {
$user = $this->getMockBuilder(IUser::class)->getMock();
$expected = new Template('twofactor_backupcodes', 'challenge');
$expected = $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge');

$this->assertEquals($expected, $this->provider->getTemplate($user));
}
Expand Down
11 changes: 5 additions & 6 deletions apps/user_ldap/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
use OCP\IL10N;
use OCP\Server;
use OCP\Settings\IDelegatedSettings;
use OCP\Template;
use OCP\Template\ITemplateManager;

class Admin implements IDelegatedSettings {
/**
* @param IL10N $l
*/
public function __construct(
private IL10N $l,
private ITemplateManager $templateManager,
) {
}

Expand All @@ -40,11 +38,12 @@ public function getForm() {

$hosts = $helper->getServerConfigurationHosts();

$wControls = new Template('user_ldap', 'part.wizardcontrols');
$wControls = $this->templateManager->getTemplate('user_ldap', 'part.wizardcontrols');
$wControls = $wControls->fetchPage();
$sControls = new Template('user_ldap', 'part.settingcontrols');
$sControls = $this->templateManager->getTemplate('user_ldap', 'part.settingcontrols');
$sControls = $sControls->fetchPage();

$parameters = [];
$parameters['serverConfigurationPrefixes'] = $prefixes;
$parameters['serverConfigurationHosts'] = $hosts;
$parameters['settingControls'] = $sControls;
Expand Down
21 changes: 13 additions & 8 deletions apps/user_ldap/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@
use OCA\User_LDAP\Settings\Admin;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IL10N;
use OCP\Template;
use OCP\Server;
use OCP\Template\ITemplateManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* @group DB
* @package OCA\User_LDAP\Tests\Settings
*/
class AdminTest extends TestCase {
/** @var Admin */
private $admin;
/** @var IL10N */
private $l10n;
private IL10N&MockObject $l10n;

private ITemplateManager $templateManager;
private Admin $admin;

protected function setUp(): void {
parent::setUp();
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
$this->templateManager = Server::get(ITemplateManager::class);

$this->admin = new Admin(
$this->l10n
$this->l10n,
$this->templateManager,
);
}

Expand All @@ -38,11 +42,12 @@ public function testGetForm(): void {
$prefixes = ['s01'];
$hosts = ['s01' => ''];

$wControls = new Template('user_ldap', 'part.wizardcontrols');
$wControls = $this->templateManager->getTemplate('user_ldap', 'part.wizardcontrols');
$wControls = $wControls->fetchPage();
$sControls = new Template('user_ldap', 'part.settingcontrols');
$sControls = $this->templateManager->getTemplate('user_ldap', 'part.settingcontrols');
$sControls = $sControls->fetchPage();

$parameters = [];
$parameters['serverConfigurationPrefixes'] = $prefixes;
$parameters['serverConfigurationHosts'] = $hosts;
$parameters['settingControls'] = $sControls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\WorkflowEngine\Listener;

use OCA\WorkflowEngine\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Template;
use OCP\Util;
use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
use function class_exists;
use function function_exists;

/** @template-implements IEventListener<LoadSettingsScriptsEvent> */
class LoadAdditionalSettingsScriptsListener implements IEventListener {
public function handle(Event $event): void {
if (!function_exists('style')) {
// This is hacky, but we need to load the template class
class_exists(Template::class, true);
}

Util::addScript('core', 'files_fileinfo');
Util::addScript('core', 'files_client');
Util::addScript('core', 'systemtags');
Expand Down
5 changes: 0 additions & 5 deletions build/psalm-baseline-ocp.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="lib/private/legacy/OC_Template.php">
<UndefinedClass>
<code><![CDATA[OC]]></code>
</UndefinedClass>
</file>
<file src="lib/public/AppFramework/ApiController.php">
<NoInterfaceProperties>
<code><![CDATA[$this->request->server]]></code>
Expand Down
5 changes: 0 additions & 5 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2601,11 +2601,6 @@
<code><![CDATA[getQuota]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/legacy/OC_Template.php">
<InvalidReturnType>
<code><![CDATA[bool|string]]></code>
</InvalidReturnType>
</file>
<file src="lib/private/legacy/OC_User.php">
<UndefinedClass>
<code><![CDATA[\Test\Util\User\Dummy]]></code>
Expand Down
10 changes: 6 additions & 4 deletions core/Controller/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OC\Core\Controller;

use OC\Setup;
use OCP\Template\ITemplateManager;
use OCP\Util;
use Psr\Log\LoggerInterface;

Expand All @@ -17,6 +18,7 @@ class SetupController {
public function __construct(
protected Setup $setupHelper,
protected LoggerInterface $logger,
protected ITemplateManager $templateManager,
) {
$this->autoConfigFile = \OC::$configDir . 'autoconfig.php';
}
Expand Down Expand Up @@ -57,10 +59,10 @@ public function run(array $post): void {
}

private function displaySetupForbidden(): void {
\OC_Template::printGuestPage('', 'installation_forbidden');
$this->templateManager->printGuestPage('', 'installation_forbidden');
}

public function display($post): void {
public function display(array $post): void {
$defaults = [
'adminlogin' => '',
'adminpass' => '',
Expand All @@ -80,7 +82,7 @@ public function display($post): void {
Util::addScript('core', 'main');
Util::addTranslations('core');

\OC_Template::printGuestPage('', 'installation', $parameters);
$this->templateManager->printGuestPage('', 'installation', $parameters);
}

private function finishSetup(): void {
Expand All @@ -90,7 +92,7 @@ private function finishSetup(): void {
\OC::$server->getIntegrityCodeChecker()->runInstanceVerification();

if ($this->setupHelper->shouldRemoveCanInstallFile()) {
\OC_Template::printGuestPage('', 'installation_incomplete');
$this->templateManager->printGuestPage('', 'installation_incomplete');
}

header('Location: ' . \OC::$server->getURLGenerator()->getAbsoluteURL('index.php/core/apps/recommended'));
Expand Down
15 changes: 8 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCP\IRequest;
use OCP\Security\Bruteforce\MaxDelayReached;
use OCP\Server;
use OCP\Template\ITemplateManager;
use Psr\Log\LoggerInterface;

try {
Expand All @@ -29,10 +30,10 @@
]);

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 503);
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 503);
} catch (HintException $ex) {
try {
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503);
Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getHint(), 503);
} catch (Exception $ex2) {
try {
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
Expand All @@ -48,7 +49,7 @@
}

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 500);
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
}
} catch (LoginException $ex) {
$request = Server::get(IRequest::class);
Expand All @@ -63,7 +64,7 @@
echo json_encode(['message' => $ex->getMessage()]);
exit();
}
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
} catch (MaxDelayReached $ex) {
$request = Server::get(IRequest::class);
/**
Expand All @@ -78,15 +79,15 @@
exit();
}
http_response_code(429);
OC_Template::printGuestPage('core', '429');
Server::get(ITemplateManager::class)->printGuestPage('core', '429');
} catch (Exception $ex) {
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
'app' => 'index',
'exception' => $ex,
]);

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 500);
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
} catch (Error $ex) {
try {
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
Expand All @@ -103,5 +104,5 @@

throw $ex;
}
OC_Template::printExceptionErrorPage($ex, 500);
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
}
Loading
Loading