-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathTwigEnvironmentResolver.php
55 lines (41 loc) · 1.28 KB
/
TwigEnvironmentResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php declare(strict_types = 1);
namespace PHPStan\Symfony;
use PHPStan\ShouldNotHappenException;
use Twig\Environment;
use function file_exists;
use function is_readable;
use function sprintf;
final class TwigEnvironmentResolver
{
/** @var string|null */
private $twigEnvironmentLoader;
/** @var Environment|null */
private $twigEnvironment;
public function __construct(?string $twigEnvironmentLoader)
{
$this->twigEnvironmentLoader = $twigEnvironmentLoader;
}
private function getTwigEnvironment(): ?Environment
{
if ($this->twigEnvironmentLoader === null) {
return null;
}
if ($this->twigEnvironment !== null) {
return $this->twigEnvironment;
}
if (!file_exists($this->twigEnvironmentLoader)
|| !is_readable($this->twigEnvironmentLoader)
) {
throw new ShouldNotHappenException(sprintf('Cannot load Twig environment. Check the parameters.symfony.twigEnvironmentLoader setting in PHPStan\'s config. The offending value is "%s".', $this->twigEnvironmentLoader));
}
return $this->twigEnvironment = require $this->twigEnvironmentLoader;
}
public function templateExists(string $name): bool
{
$twigEnvironment = $this->getTwigEnvironment();
if ($twigEnvironment === null) {
return true;
}
return $twigEnvironment->getLoader()->exists($name);
}
}