Skip to content

Commit 312f39a

Browse files
committed
[FEATURE] Add possibility to assign instructions to InternalRequest
* TypoScriptInstruction is applied to global TypoScript template * ArrayValueInstruction is can be used by any consuming renderer
1 parent bb21d12 commit 312f39a

File tree

5 files changed

+378
-0
lines changed

5 files changed

+378
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Hook;
3+
4+
/*
5+
* This file is part of the TYPO3 CMS project.
6+
*
7+
* It is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, either version 2
9+
* of the License, or any later version.
10+
*
11+
* For the full copyright and license information, please read the
12+
* LICENSE.txt file that was distributed with this source code.
13+
*
14+
* The TYPO3 project - inspiring people to share!
15+
*/
16+
17+
use TYPO3\CMS\Core\SingletonInterface;
18+
use TYPO3\CMS\Core\TypoScript\TemplateService;
19+
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
20+
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\RequestBootstrap;
21+
22+
/**
23+
* Modifier for global TypoScript dynamically provided by test-case.
24+
*/
25+
class TypoScriptInstructionModifier implements \TYPO3\CMS\Core\SingletonInterface
26+
{
27+
/**
28+
* @param array $parameters
29+
* @param TemplateService $service
30+
*/
31+
public function apply(array $parameters, TemplateService $service)
32+
{
33+
$instruction = RequestBootstrap::getInternalRequest()
34+
->getInstruction(TemplateService::class);
35+
if (!$instruction instanceof TypoScriptInstruction) {
36+
return;
37+
}
38+
39+
$this->applyConstants($instruction, $service);
40+
$this->applyTypoScript($instruction, $service);
41+
}
42+
43+
/**
44+
* @param TypoScriptInstruction $instruction
45+
* @param TemplateService $service
46+
*/
47+
private function applyConstants(
48+
TypoScriptInstruction $instruction,
49+
TemplateService $service
50+
)
51+
{
52+
if (empty($instruction->getConstants())) {
53+
return;
54+
}
55+
$service->constants[] = $this->compileAssignments(
56+
$instruction->getConstants()
57+
);
58+
}
59+
60+
/**
61+
* @param TypoScriptInstruction $instruction
62+
* @param TemplateService $service
63+
*/
64+
private function applyTypoScript(
65+
TypoScriptInstruction $instruction,
66+
TemplateService $service
67+
)
68+
{
69+
if (empty($instruction->getTypoScript())) {
70+
return;
71+
}
72+
$service->config[] = $this->compileAssignments(
73+
$instruction->getTypoScript()
74+
);
75+
}
76+
77+
/**
78+
* + input: ['a' => ['b' => 'c']]
79+
* + output: 'a.b = c'
80+
*
81+
* @param array $nestedArray
82+
* @return string
83+
*/
84+
private function compileAssignments(array $nestedArray): string
85+
{
86+
$assingments = $this->flatten($nestedArray);
87+
array_walk(
88+
$assingments,
89+
function (&$value, $key) {
90+
$value = sprintf('%s = %s', $key, $value);
91+
}
92+
);
93+
return implode("\n", $assingments);
94+
}
95+
96+
/**
97+
* + input: ['a' => ['b' => 'c']]
98+
* + output: ['a.b' => 'c']
99+
*
100+
* @param array $array
101+
* @param string $prefix
102+
* @return array
103+
*/
104+
private function flatten(array $array, string $prefix = ''): array
105+
{
106+
$result = [];
107+
foreach ($array as $key => $value) {
108+
if (is_array($value)) {
109+
$result = array_merge(
110+
$result,
111+
$this->flatten(
112+
$value,
113+
$prefix . rtrim($key, '.') . '.'
114+
)
115+
);
116+
} else {
117+
$result[$prefix . $key] = $value;
118+
}
119+
}
120+
return $result;
121+
}
122+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal;
3+
4+
/*
5+
* This file is part of the TYPO3 CMS project.
6+
*
7+
* It is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, either version 2
9+
* of the License, or any later version.
10+
*
11+
* For the full copyright and license information, please read the
12+
* LICENSE.txt file that was distributed with this source code.
13+
*
14+
* The TYPO3 project - inspiring people to share!
15+
*/
16+
17+
use TYPO3\TestingFramework\Core\Functional\Framework\AssignablePropertyTrait;
18+
19+
/**
20+
* Model of instruction
21+
*/
22+
abstract class AbstractInstruction implements \JsonSerializable
23+
{
24+
use AssignablePropertyTrait;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $identifier;
30+
31+
/**
32+
* @param array $data
33+
* @return static
34+
*/
35+
public static function fromArray(array $data): self
36+
{
37+
if (empty($data['__type'])) {
38+
throw new \LogicException(
39+
'Missing internal "__type" reference',
40+
1534516564
41+
);
42+
}
43+
if (!is_a($data['__type'], self::class, true)) {
44+
throw new \LogicException(
45+
sprintf(
46+
'Class "%s" does not inherit from "%s"',
47+
$data['__type'],
48+
self::class
49+
),
50+
1534516565
51+
);
52+
}
53+
if (empty($data['identifier'])) {
54+
throw new \LogicException(
55+
'Missing identifier',
56+
1534516566
57+
);
58+
}
59+
60+
if (self::class === static::class) {
61+
return $data['__type']::fromArray($data);
62+
}
63+
$target = new static($data['identifier']);
64+
unset($data['__type'], $data['identifier']);
65+
return $target->with($data);
66+
}
67+
68+
/**
69+
* @param string $identifier
70+
*/
71+
public function __construct(string $identifier)
72+
{
73+
$this->identifier = $identifier;
74+
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public function jsonSerialize(): array
80+
{
81+
return array_merge(
82+
get_object_vars($this),
83+
['__type' => get_class($this)]
84+
);
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getIdentifier(): string
91+
{
92+
return $this->identifier;
93+
}
94+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal;
3+
4+
/*
5+
* This file is part of the TYPO3 CMS project.
6+
*
7+
* It is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, either version 2
9+
* of the License, or any later version.
10+
*
11+
* For the full copyright and license information, please read the
12+
* LICENSE.txt file that was distributed with this source code.
13+
*
14+
* The TYPO3 project - inspiring people to share!
15+
*/
16+
17+
use TYPO3\TestingFramework\Core\Functional\Framework\AssignablePropertyTrait;
18+
19+
/**
20+
* Model of arbitrary array value instruction
21+
*/
22+
class ArrayValueInstruction extends AbstractInstruction
23+
{
24+
/**
25+
* @var array
26+
*/
27+
protected $array = [];
28+
29+
/**
30+
* @param array $typoScript
31+
* @return static
32+
*/
33+
public function withArray(array $array): self
34+
{
35+
$target = clone $this;
36+
$target->array = $array;
37+
return $target;
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function getArray(): array
44+
{
45+
return $this->array;
46+
}
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal;
3+
4+
/*
5+
* This file is part of the TYPO3 CMS project.
6+
*
7+
* It is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, either version 2
9+
* of the License, or any later version.
10+
*
11+
* For the full copyright and license information, please read the
12+
* LICENSE.txt file that was distributed with this source code.
13+
*
14+
* The TYPO3 project - inspiring people to share!
15+
*/
16+
17+
use TYPO3\TestingFramework\Core\Functional\Framework\AssignablePropertyTrait;
18+
19+
/**
20+
* Model of TypoScript instruction
21+
*/
22+
class TypoScriptInstruction extends AbstractInstruction
23+
{
24+
/**
25+
* @var array
26+
*/
27+
protected $constants;
28+
29+
/**
30+
* @var array
31+
*/
32+
protected $typoScript;
33+
34+
/**
35+
* @param array $typoScript
36+
* @return static
37+
*/
38+
public function withConstants(array $constants): self
39+
{
40+
$target = clone $this;
41+
$target->constants = $constants;
42+
return $target;
43+
}
44+
45+
/**
46+
* @param array $typoScript
47+
* @return static
48+
*/
49+
public function withTypoScript(array $typoScript): self
50+
{
51+
$target = clone $this;
52+
$target->typoScript = $typoScript;
53+
return $target;
54+
}
55+
56+
/**
57+
* @return array
58+
*/
59+
public function getConstants(): ?array
60+
{
61+
return $this->constants;
62+
}
63+
64+
/**
65+
* @return array
66+
*/
67+
public function getTypoScript(): ?array
68+
{
69+
return $this->typoScript;
70+
}
71+
}

0 commit comments

Comments
 (0)