Skip to content

[FEATURE] add json response to list action, now you can automate your tests #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Classes/Controller/StyleguideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use SMS\FluidComponents\Utility\ComponentLoader;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\ImmediateResponseException;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand All @@ -40,6 +42,12 @@ public function listAction(): ResponseInterface
$allComponents = $this->componentRepository->findWithFixtures();
$componentPackages = $this->groupComponentsByPackage($allComponents);

// if json request, return JSON response
$accept = $this->request->getHeaderLine('Accept');
if (str_starts_with($accept, 'application/json')) {
throw new ImmediateResponseException(new JsonResponse($allComponents));
}

$this->view->assignMultiple([
'navigation' => $allComponents,
'packages' => $componentPackages
Expand Down
37 changes: 31 additions & 6 deletions Classes/Domain/Model/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

namespace Sitegeist\FluidStyleguide\Domain\Model;

use Sitegeist\FluidStyleguide\Domain\Model\ComponentName;
use Sitegeist\FluidStyleguide\Domain\Model\ComponentLocation;
use Sitegeist\FluidStyleguide\Domain\Model\ComponentFixture;
use JsonSerializable;
use SMS\FluidComponents\Fluid\ViewHelper\ComponentRenderer;
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
use SMS\FluidComponents\Fluid\ViewHelper\ComponentRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\ArgumentDefinition;

class Component
class Component implements JsonSerializable
{
/** @var array<string, ComponentFixture>|null */
protected ?array $fixtures = null;
protected ?string $documentation;
/** @var array<string, ArgumentDefinition>|null */
protected ?array $arguments = null;

public function __construct(
Expand Down Expand Up @@ -180,4 +181,28 @@ protected function getComponentRenderer(): ComponentRenderer
$componentRenderer->setComponentNamespace($this->name->getIdentifier());
return $componentRenderer;
}

function jsonSerialize(): array
{
$arguments = [];
foreach ($this->getArguments() as $key => $argument) {
$arguments[$key] = [
'name' => $argument->getName(),
'type' => $argument->getType(),
'description' => $argument->getDescription(),
'required' => $argument->isRequired(),
'defaultValue' => $argument->getDefaultValue(),
'escape' => $argument->getEscape(),
];
}
return [
'identifier' => $this->name->getIdentifier(),
'location' => $this->location->getFilePath(),
'fixtures' => $this->getFixtures(),
'documentation' => $this->getDocumentation(),
'arguments' => $arguments,
'defaultValues' => $this->getDefaultValues(),
'codeQualityConfiguration' => $this->getCodeQualityConfiguration()
];
}
}
13 changes: 12 additions & 1 deletion Classes/Domain/Model/ComponentFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Sitegeist\FluidStyleguide\Domain\Model;

class ComponentFixture
use JsonSerializable;

class ComponentFixture implements JsonSerializable
{
public function __construct(
protected string $filePath, // Absolute path to the fixture file. Note that this file contains multiple fixtures
Expand All @@ -26,4 +28,13 @@ public function getData(): array
{
return $this->data;
}

function jsonSerialize():array
{
return [
'filePath' => $this->getFilePath(),
'name' => $this->getName(),
'data' => $this->getData(),
];
}
}
1 change: 1 addition & 0 deletions Classes/Domain/Repository/ComponentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
/**
* Returns a list of all components in the current TYPO3 installation that have
* a fixture file and thus can be displayed in the styleguide
* @return list<Component>
*/
public function findWithFixtures(): array
{
Expand Down