fix: Use a dedicated Exception class for when a template is not found

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/51029/head
Côme Chilliet 7 months ago committed by Côme Chilliet
parent cd3a88805b
commit b086f10028
  1. 1
      lib/composer/composer/autoload_classmap.php
  2. 1
      lib/composer/composer/autoload_static.php
  3. 9
      lib/private/Template/Template.php
  4. 29
      lib/private/Template/TemplateFileLocator.php
  5. 2
      lib/private/Template/TemplateManager.php
  6. 1
      lib/public/Template/ITemplateManager.php
  7. 16
      lib/public/Template/TemplateNotFoundException.php

@ -845,6 +845,7 @@ return array(
'OCP\\Template' => $baseDir . '/lib/public/Template.php',
'OCP\\Template\\ITemplate' => $baseDir . '/lib/public/Template/ITemplate.php',
'OCP\\Template\\ITemplateManager' => $baseDir . '/lib/public/Template/ITemplateManager.php',
'OCP\\Template\\TemplateNotFoundException' => $baseDir . '/lib/public/Template/TemplateNotFoundException.php',
'OCP\\TextProcessing\\Events\\AbstractTextProcessingEvent' => $baseDir . '/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php',
'OCP\\TextProcessing\\Events\\TaskFailedEvent' => $baseDir . '/lib/public/TextProcessing/Events/TaskFailedEvent.php',
'OCP\\TextProcessing\\Events\\TaskSuccessfulEvent' => $baseDir . '/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php',

@ -894,6 +894,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Template' => __DIR__ . '/../../..' . '/lib/public/Template.php',
'OCP\\Template\\ITemplate' => __DIR__ . '/../../..' . '/lib/public/Template/ITemplate.php',
'OCP\\Template\\ITemplateManager' => __DIR__ . '/../../..' . '/lib/public/Template/ITemplateManager.php',
'OCP\\Template\\TemplateNotFoundException' => __DIR__ . '/../../..' . '/lib/public/Template/TemplateNotFoundException.php',
'OCP\\TextProcessing\\Events\\AbstractTextProcessingEvent' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php',
'OCP\\TextProcessing\\Events\\TaskFailedEvent' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Events/TaskFailedEvent.php',
'OCP\\TextProcessing\\Events\\TaskSuccessfulEvent' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php',

@ -18,6 +18,7 @@ use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults;
use OCP\Server;
use OCP\Template\ITemplate;
use OCP\Template\TemplateNotFoundException;
use OCP\Util;
require_once __DIR__ . '/../legacy/template/functions.php';
@ -31,6 +32,7 @@ class Template extends Base implements ITemplate {
* @param string $name of the template file (without suffix)
* @param TemplateResponse::RENDER_AS_* $renderAs If $renderAs is set, will try to
* produce a full page in the according layout.
* @throws TemplateNotFoundException
*/
public function __construct(
protected string $app,
@ -68,7 +70,8 @@ class Template extends Base implements ITemplate {
* Checking all the possible locations.
*
* @param string $name of the template file (without suffix)
* @return string[]
* @return array{string,string} Directory path and filename
* @throws TemplateNotFoundException
*/
protected function findTemplate(string $theme, string $app, string $name): array {
// Check if it is a app template or not.
@ -83,9 +86,7 @@ class Template extends Base implements ITemplate {
$dirs = $this->getCoreTemplateDirs($theme, \OC::$SERVERROOT);
}
$locator = new TemplateFileLocator($dirs);
$template = $locator->find($name);
$path = $locator->getPath();
return [$path, $template];
return $locator->find($name);
}
/**

@ -1,29 +1,31 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Template;
class TemplateFileLocator {
protected $dirs;
private $path;
use OCP\Template\TemplateNotFoundException;
class TemplateFileLocator {
/**
* @param string[] $dirs
*/
public function __construct($dirs) {
$this->dirs = $dirs;
public function __construct(
private array $dirs,
) {
}
/**
* @param string $template
* @return string
* @throws \Exception
* @return array{string,string} Directory path and filename
* @throws TemplateNotFoundException
*/
public function find($template) {
public function find(string $template): array {
if ($template === '') {
throw new \InvalidArgumentException('Empty template name');
}
@ -31,14 +33,9 @@ class TemplateFileLocator {
foreach ($this->dirs as $dir) {
$file = $dir . $template . '.php';
if (is_file($file)) {
$this->path = $dir;
return $file;
return [$dir,$file];
}
}
throw new \Exception('template file not found: template:' . $template);
}
public function getPath() {
return $this->path;
throw new TemplateNotFoundException('template file not found: template:' . $template);
}
}

@ -17,6 +17,7 @@ use OCP\IRequest;
use OCP\Server;
use OCP\Template\ITemplate;
use OCP\Template\ITemplateManager;
use OCP\Template\TemplateNotFoundException;
use Psr\Log\LoggerInterface;
class TemplateManager implements ITemplateManager {
@ -28,6 +29,7 @@ class TemplateManager implements ITemplateManager {
/**
* @param TemplateResponse::RENDER_AS_* $renderAs
* @throws TemplateNotFoundException if the template cannot be found
*/
public function getTemplate(string $app, string $name, string $renderAs = TemplateResponse::RENDER_AS_BLANK, bool $registerCall = true): ITemplate {
return new Template($app, $name, $renderAs, $registerCall);

@ -17,6 +17,7 @@ use OCP\AppFramework\Http\TemplateResponse;
interface ITemplateManager {
/**
* @param TemplateResponse::RENDER_AS_* $renderAs
* @throws TemplateNotFoundException if the template cannot be found
* @since 32.0.0
*/
public function getTemplate(string $app, string $name, string $renderAs = TemplateResponse::RENDER_AS_BLANK, bool $registerCall = true): ITemplate;

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Template;
/**
* @since 32.0.0
*/
class TemplateNotFoundException extends \Exception {
}
Loading…
Cancel
Save