Skip to content

Commit

Permalink
Remove trigger_error from Template class
Browse files Browse the repository at this point in the history
This will remove the error:

> Error while working with template cache: Unable to create the cache directory

This does not fix the cache issue, but removes the warning.

The current behavior is to disable the cache when there's an issue with
it. However this has bad performance.

The issue is that sometimes Twig generates a different cache key than
the warmed up one and tries to write a new cache, but that's not the
desired behavior.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Feb 23, 2024
1 parent cc62444 commit e0d42d2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14425,11 +14425,6 @@ parameters:
count: 1
path: src/Table/Table.php

-
message: "#^Dead catch \\- RuntimeException is never thrown in the try block\\.$#"
count: 1
path: src/Template.php

-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
count: 1
Expand Down
24 changes: 4 additions & 20 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
use Twig\RuntimeLoader\ContainerRuntimeLoader;
use Twig\TemplateWrapper;

use function __;
use function sprintf;
use function trigger_error;

use const E_USER_WARNING;

/**
* Handle front end templating
*/
Expand Down Expand Up @@ -106,23 +100,13 @@ private function load(string $templateName): TemplateWrapper
}

try {
$template = static::$twig->load($templateName . '.twig');
} catch (RuntimeException $e) {
return static::$twig->load($templateName . '.twig');
} catch (RuntimeException) { // @phpstan-ignore-line thrown by Twig\Cache\FilesystemCache
/* Retry with disabled cache */
static::$twig->setCache(false);
$template = static::$twig->load($templateName . '.twig');
// The trigger error is intentionally after second load
// to avoid triggering error when disabling cache does not solve it.
trigger_error(
sprintf(
__('Error while working with template cache: %s'),
$e->getMessage(),
),
E_USER_WARNING,
);
}

return $template;
return static::$twig->load($templateName . '.twig');
}
}

/**
Expand Down

0 comments on commit e0d42d2

Please sign in to comment.