-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add possibility to assign instructions to InternalRequest
* TypoScriptInstruction is applied to global TypoScript template * ArrayValueInstruction is can be used by any consuming renderer
- Loading branch information
Showing
5 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
Classes/Core/Functional/Framework/Frontend/Hook/TypoScriptInstructionModifier.php
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,122 @@ | ||
<?php | ||
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Hook; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use TYPO3\CMS\Core\SingletonInterface; | ||
use TYPO3\CMS\Core\TypoScript\TemplateService; | ||
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction; | ||
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\RequestBootstrap; | ||
|
||
/** | ||
* Modifier for global TypoScript dynamically provided by test-case. | ||
*/ | ||
class TypoScriptInstructionModifier implements \TYPO3\CMS\Core\SingletonInterface | ||
{ | ||
/** | ||
* @param array $parameters | ||
* @param TemplateService $service | ||
*/ | ||
public function apply(array $parameters, TemplateService $service) | ||
{ | ||
$instruction = RequestBootstrap::getInternalRequest() | ||
->getInstruction(TemplateService::class); | ||
if (!$instruction instanceof TypoScriptInstruction) { | ||
return; | ||
} | ||
|
||
$this->applyConstants($instruction, $service); | ||
$this->applyTypoScript($instruction, $service); | ||
} | ||
|
||
/** | ||
* @param TypoScriptInstruction $instruction | ||
* @param TemplateService $service | ||
*/ | ||
private function applyConstants( | ||
TypoScriptInstruction $instruction, | ||
TemplateService $service | ||
) | ||
{ | ||
if (empty($instruction->getConstants())) { | ||
return; | ||
} | ||
$service->constants[] = $this->compileAssignments( | ||
$instruction->getConstants() | ||
); | ||
} | ||
|
||
/** | ||
* @param TypoScriptInstruction $instruction | ||
* @param TemplateService $service | ||
*/ | ||
private function applyTypoScript( | ||
TypoScriptInstruction $instruction, | ||
TemplateService $service | ||
) | ||
{ | ||
if (empty($instruction->getTypoScript())) { | ||
return; | ||
} | ||
$service->config[] = $this->compileAssignments( | ||
$instruction->getTypoScript() | ||
); | ||
} | ||
|
||
/** | ||
* + input: ['a' => ['b' => 'c']] | ||
* + output: 'a.b = c' | ||
* | ||
* @param array $nestedArray | ||
* @return string | ||
*/ | ||
private function compileAssignments(array $nestedArray): string | ||
{ | ||
$assingments = $this->flatten($nestedArray); | ||
array_walk( | ||
$assingments, | ||
function (&$value, $key) { | ||
$value = sprintf('%s = %s', $key, $value); | ||
} | ||
); | ||
return implode("\n", $assingments); | ||
} | ||
|
||
/** | ||
* + input: ['a' => ['b' => 'c']] | ||
* + output: ['a.b' => 'c'] | ||
* | ||
* @param array $array | ||
* @param string $prefix | ||
* @return array | ||
*/ | ||
private function flatten(array $array, string $prefix = ''): array | ||
{ | ||
$result = []; | ||
foreach ($array as $key => $value) { | ||
if (is_array($value)) { | ||
$result = array_merge( | ||
$result, | ||
$this->flatten( | ||
$value, | ||
$prefix . rtrim($key, '.') . '.' | ||
) | ||
); | ||
} else { | ||
$result[$prefix . $key] = $value; | ||
} | ||
} | ||
return $result; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
Classes/Core/Functional/Framework/Frontend/Internal/AbstractInstruction.php
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,94 @@ | ||
<?php | ||
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use TYPO3\TestingFramework\Core\Functional\Framework\AssignablePropertyTrait; | ||
|
||
/** | ||
* Model of instruction | ||
*/ | ||
abstract class AbstractInstruction implements \JsonSerializable | ||
{ | ||
use AssignablePropertyTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $identifier; | ||
|
||
/** | ||
* @param array $data | ||
* @return static | ||
*/ | ||
public static function fromArray(array $data): self | ||
{ | ||
if (empty($data['__type'])) { | ||
throw new \LogicException( | ||
'Missing internal "__type" reference', | ||
1534516564 | ||
); | ||
} | ||
if (!is_a($data['__type'], self::class, true)) { | ||
throw new \LogicException( | ||
sprintf( | ||
'Class "%s" does not inherit from "%s"', | ||
$data['__type'], | ||
self::class | ||
), | ||
1534516565 | ||
); | ||
} | ||
if (empty($data['identifier'])) { | ||
throw new \LogicException( | ||
'Missing identifier', | ||
1534516566 | ||
); | ||
} | ||
|
||
if (self::class === static::class) { | ||
return $data['__type']::fromArray($data); | ||
} | ||
$target = new static($data['identifier']); | ||
unset($data['__type'], $data['identifier']); | ||
return $target->with($data); | ||
} | ||
|
||
/** | ||
* @param string $identifier | ||
*/ | ||
public function __construct(string $identifier) | ||
{ | ||
$this->identifier = $identifier; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return array_merge( | ||
get_object_vars($this), | ||
['__type' => get_class($this)] | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getIdentifier(): string | ||
{ | ||
return $this->identifier; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Classes/Core/Functional/Framework/Frontend/Internal/ArrayValueInstruction.php
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,47 @@ | ||
<?php | ||
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use TYPO3\TestingFramework\Core\Functional\Framework\AssignablePropertyTrait; | ||
|
||
/** | ||
* Model of arbitrary array value instruction | ||
*/ | ||
class ArrayValueInstruction extends AbstractInstruction | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $array = []; | ||
|
||
/** | ||
* @param array $typoScript | ||
* @return static | ||
*/ | ||
public function withArray(array $array): self | ||
{ | ||
$target = clone $this; | ||
$target->array = $array; | ||
return $target; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getArray(): array | ||
{ | ||
return $this->array; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Classes/Core/Functional/Framework/Frontend/Internal/TypoScriptInstruction.php
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,71 @@ | ||
<?php | ||
namespace TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use TYPO3\TestingFramework\Core\Functional\Framework\AssignablePropertyTrait; | ||
|
||
/** | ||
* Model of TypoScript instruction | ||
*/ | ||
class TypoScriptInstruction extends AbstractInstruction | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $constants; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $typoScript; | ||
|
||
/** | ||
* @param array $typoScript | ||
* @return static | ||
*/ | ||
public function withConstants(array $constants): self | ||
{ | ||
$target = clone $this; | ||
$target->constants = $constants; | ||
return $target; | ||
} | ||
|
||
/** | ||
* @param array $typoScript | ||
* @return static | ||
*/ | ||
public function withTypoScript(array $typoScript): self | ||
{ | ||
$target = clone $this; | ||
$target->typoScript = $typoScript; | ||
return $target; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getConstants(): ?array | ||
{ | ||
return $this->constants; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getTypoScript(): ?array | ||
{ | ||
return $this->typoScript; | ||
} | ||
} |
Oops, something went wrong.