-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnvironmentFactory.php
More file actions
176 lines (137 loc) · 4.76 KB
/
EnvironmentFactory.php
File metadata and controls
176 lines (137 loc) · 4.76 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Keepsuit\Liquid;
use Keepsuit\Liquid\Contracts\LiquidErrorHandler;
use Keepsuit\Liquid\Contracts\LiquidExtension;
use Keepsuit\Liquid\Contracts\LiquidFileSystem;
use Keepsuit\Liquid\Contracts\LiquidTemplatesCache;
use Keepsuit\Liquid\ErrorHandlers\DefaultErrorHandler;
use Keepsuit\Liquid\Extensions\StandardExtension;
use Keepsuit\Liquid\FileSystems\BlankFileSystem;
use Keepsuit\Liquid\Filters\FiltersProvider;
use Keepsuit\Liquid\Render\RenderContextOptions;
use Keepsuit\Liquid\Render\ResourceLimits;
use Keepsuit\Liquid\TemplatesCache\MemoryTemplatesCache;
final class EnvironmentFactory
{
protected LiquidFileSystem $fileSystem;
protected LiquidErrorHandler $errorHandler;
protected LiquidTemplatesCache $templatesCache;
protected ResourceLimits $resourceLimits;
protected RenderContextOptions $defaultRenderContextOptions;
/**
* @var array<class-string<LiquidExtension>, LiquidExtension>
*/
protected array $extensions = [];
/**
* @var array<class-string<Tag>>
*/
protected array $tags = [];
/**
* @var array<class-string<FiltersProvider>>
*/
protected array $filters = [];
public function __construct()
{
$this->fileSystem = new BlankFileSystem;
$this->errorHandler = new DefaultErrorHandler;
$this->templatesCache = new MemoryTemplatesCache;
$this->resourceLimits = new ResourceLimits;
$this->defaultRenderContextOptions = new RenderContextOptions;
$this->addExtension(new StandardExtension);
}
public static function new(): EnvironmentFactory
{
return new self;
}
public function setFilesystem(LiquidFileSystem $fileSystem): EnvironmentFactory
{
$this->fileSystem = $fileSystem;
return $this;
}
public function setErrorHandler(LiquidErrorHandler $errorHandler): EnvironmentFactory
{
$this->errorHandler = $errorHandler;
return $this;
}
public function setTemplatesCache(LiquidTemplatesCache $templatesCache): EnvironmentFactory
{
$this->templatesCache = $templatesCache;
return $this;
}
public function setResourceLimits(ResourceLimits $resourceLimits): EnvironmentFactory
{
$this->resourceLimits = $resourceLimits;
return $this;
}
public function setRethrowErrors(bool $rethrowErrors = true): EnvironmentFactory
{
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
rethrowErrors: $rethrowErrors,
);
return $this;
}
public function setStrictVariables(bool $strictVariables = true): EnvironmentFactory
{
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
strictVariables: $strictVariables,
);
return $this;
}
public function setStrictFilters(bool $strictFilters = true): EnvironmentFactory
{
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
strictFilters: $strictFilters,
);
return $this;
}
public function setLazyParsing(bool $lazyParsing = true): EnvironmentFactory
{
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
lazyParsing: $lazyParsing,
);
return $this;
}
/**
* @param class-string<Tag> $tag
*/
public function registerTag(string $tag): EnvironmentFactory
{
if (! in_array($tag, $this->tags, true)) {
$this->tags[] = $tag;
}
return $this;
}
/**
* @param class-string<FiltersProvider> $filtersProvider
*/
public function registerFilters(string $filtersProvider): EnvironmentFactory
{
if (! in_array($filtersProvider, $this->filters, true)) {
$this->filters[] = $filtersProvider;
}
return $this;
}
public function addExtension(LiquidExtension $extension): EnvironmentFactory
{
$this->extensions[$extension::class] = $extension;
return $this;
}
public function build(): Environment
{
$environment = new Environment(
fileSystem: $this->fileSystem,
errorHandler: $this->errorHandler,
templatesCache: $this->templatesCache,
defaultResourceLimits: $this->resourceLimits,
defaultRenderContextOptions: $this->defaultRenderContextOptions,
extensions: array_values($this->extensions),
);
foreach ($this->tags as $tag) {
$environment->tagRegistry->register($tag);
}
foreach ($this->filters as $filtersProvider) {
$environment->filterRegistry->register($filtersProvider);
}
return $environment;
}
}