Skip to content

Commit

Permalink
prevent extensions tags & filters override of custom tags/filters
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Dec 30, 2024
1 parent 4784751 commit df0aa80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
6 changes: 2 additions & 4 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ class Environment
protected array $extensions = [];

public function __construct(
?TagRegistry $tagRegistry = null,
?FilterRegistry $filterRegistry = null,
?LiquidFileSystem $fileSystem = null,
?LiquidErrorHandler $errorHandler = null,
?ResourceLimits $defaultResourceLimits = null,
?RenderContextOptions $defaultRenderContextOptions = null,
/** @var LiquidExtension[] $extensions */
array $extensions = [],
) {
$this->tagRegistry = $tagRegistry ?? new TagRegistry;
$this->filterRegistry = $filterRegistry ?? new FilterRegistry;
$this->tagRegistry = new TagRegistry;
$this->filterRegistry = new FilterRegistry;
$this->fileSystem = $fileSystem ?? new BlankFileSystem;
$this->errorHandler = $errorHandler ?? new DefaultErrorHandler;
$this->defaultResourceLimits = $defaultResourceLimits ?? new ResourceLimits;
Expand Down
54 changes: 27 additions & 27 deletions src/EnvironmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@
use Keepsuit\Liquid\Filters\FiltersProvider;
use Keepsuit\Liquid\Render\RenderContextOptions;
use Keepsuit\Liquid\Render\ResourceLimits;
use Keepsuit\Liquid\Support\FilterRegistry;
use Keepsuit\Liquid\Support\TagRegistry;

final class EnvironmentFactory
{
protected TagRegistry $tagRegistry;

protected FilterRegistry $filterRegistry;

protected LiquidFileSystem $fileSystem;

protected LiquidErrorHandler $errorHandler;
Expand All @@ -33,10 +27,18 @@ final class EnvironmentFactory
*/
protected array $extensions = [];

/**
* @var array<class-string<Tag>>
*/
protected array $tags = [];

/**
* @var array<class-string<FiltersProvider>>
*/
protected array $filters = [];

public function __construct()
{
$this->tagRegistry = new TagRegistry;
$this->filterRegistry = new FilterRegistry;
$this->fileSystem = new BlankFileSystem;
$this->errorHandler = new DefaultErrorHandler;
$this->resourceLimits = new ResourceLimits;
Expand All @@ -50,20 +52,6 @@ public static function new(): EnvironmentFactory
return new self;
}

public function setTagRegistry(TagRegistry $tagRegistry): EnvironmentFactory
{
$this->tagRegistry = $tagRegistry;

return $this;
}

public function setFilterRegistry(FilterRegistry $filterRegistry): EnvironmentFactory
{
$this->filterRegistry = $filterRegistry;

return $this;
}

public function setFilesystem(LiquidFileSystem $fileSystem): EnvironmentFactory
{
$this->fileSystem = $fileSystem;
Expand Down Expand Up @@ -123,7 +111,9 @@ public function setStrictFilters(bool $strictFilters = true): EnvironmentFactory
*/
public function registerTag(string $tag): EnvironmentFactory
{
$this->tagRegistry->register($tag);
if (! in_array($tag, $this->tags, true)) {
$this->tags[] = $tag;
}

return $this;
}
Expand All @@ -133,7 +123,9 @@ public function registerTag(string $tag): EnvironmentFactory
*/
public function registerFilters(string $filtersProvider): EnvironmentFactory
{
$this->filterRegistry->register($filtersProvider);
if (! in_array($filtersProvider, $this->filters, true)) {
$this->filters[] = $filtersProvider;
}

return $this;
}
Expand All @@ -147,13 +139,21 @@ public function addExtension(LiquidExtension $extension): EnvironmentFactory

public function build(): Environment
{
return new Environment(
tagRegistry: $this->tagRegistry,
filterRegistry: $this->filterRegistry,
$environment = new Environment(
fileSystem: $this->fileSystem,
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;
}
}

0 comments on commit df0aa80

Please sign in to comment.