diff --git a/Classes/Controller/StyleguideController.php b/Classes/Controller/StyleguideController.php index c05c890..6649046 100644 --- a/Classes/Controller/StyleguideController.php +++ b/Classes/Controller/StyleguideController.php @@ -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; @@ -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 diff --git a/Classes/Domain/Model/Component.php b/Classes/Domain/Model/Component.php index 55f2472..190903f 100644 --- a/Classes/Domain/Model/Component.php +++ b/Classes/Domain/Model/Component.php @@ -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|null */ protected ?array $fixtures = null; protected ?string $documentation; + /** @var array|null */ protected ?array $arguments = null; public function __construct( @@ -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() + ]; + } } diff --git a/Classes/Domain/Model/ComponentFixture.php b/Classes/Domain/Model/ComponentFixture.php index 27141b8..d136e8f 100644 --- a/Classes/Domain/Model/ComponentFixture.php +++ b/Classes/Domain/Model/ComponentFixture.php @@ -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 @@ -26,4 +28,13 @@ public function getData(): array { return $this->data; } + + function jsonSerialize():array + { + return [ + 'filePath' => $this->getFilePath(), + 'name' => $this->getName(), + 'data' => $this->getData(), + ]; + } } diff --git a/Classes/Domain/Repository/ComponentRepository.php b/Classes/Domain/Repository/ComponentRepository.php index 72d60a9..2314b47 100644 --- a/Classes/Domain/Repository/ComponentRepository.php +++ b/Classes/Domain/Repository/ComponentRepository.php @@ -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 */ public function findWithFixtures(): array {