-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4425 from neos/feature/customDefaultContextVariab…
…lesFusionRuntime !!! FEATURE: Global variables for the Fusion runtime `FusionGlobals`
- Loading branch information
Showing
12 changed files
with
339 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Fusion\Core; | ||
|
||
use Neos\Utility\Arrays; | ||
|
||
/** | ||
* Fusion allows to add variable to the context either via | ||
* \@context.foo = "bar" or by leveraging the php api {@see Runtime::pushContext()}. | ||
* | ||
* Those approaches are highly dynamic and don't guarantee the existence of variables, | ||
* as they have to be explicitly preserved in uncached \@cache segments, | ||
* or might accidentally be popped from the stack. | ||
* | ||
* The Fusion runtime is instantiated with a set of global variables which contain the EEL helper definitions | ||
* or functions like FlowQuery. Also, variables like "request" are made available via it. | ||
* | ||
* The "${request}" special case: To make the request available in uncached segments, it would need to be serialized, | ||
* but we don't allow this currently and despite that, it would be absurd to cache a random request. | ||
* This is avoided by always exposing the current action request via the global variable. | ||
* | ||
* Overriding Fusion globals is disallowed via \@context and {@see Runtime::pushContext()}. | ||
*/ | ||
final readonly class FusionGlobals | ||
{ | ||
private function __construct( | ||
public array $value | ||
) { | ||
} | ||
|
||
public static function empty(): self | ||
{ | ||
return new self([]); | ||
} | ||
|
||
public static function fromArray(array $variables): self | ||
{ | ||
return new self($variables); | ||
} | ||
|
||
/** | ||
* You can access the current request like via this getter: | ||
* `$runtime->fusionGlobals->get('request')` | ||
*/ | ||
public function get(string $name): mixed | ||
{ | ||
return $this->value[$name] ?? null; | ||
} | ||
|
||
public function has(string $name): bool | ||
{ | ||
return array_key_exists($name, $this->value); | ||
} | ||
|
||
public function merge(FusionGlobals $other): self | ||
{ | ||
return new self( | ||
Arrays::arrayMergeRecursiveOverrule($this->value, $other->value) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.