Skip to content

Commit

Permalink
[FEATURE] Add possibility to assign instructions to InternalRequest
Browse files Browse the repository at this point in the history
* TypoScriptInstruction is applied to global TypoScript template
* ArrayValueInstruction is can be used by any consuming renderer
  • Loading branch information
ohader committed Aug 20, 2018
1 parent bb21d12 commit 312f39a
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 0 deletions.
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;
}
}
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;
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit 312f39a

Please sign in to comment.