Commit df50636e authored by Oliver Bartsch's avatar Oliver Bartsch Committed by Benni Mack

[FEATURE] Add Middleware for composer manifest generation

parent 309bfaf7
This diff is collapsed.
<?php
declare(strict_types = 1);
/*
* This file is part of TYPO3 CMS-extension "ter", created by Oliver Bartsch.
*
* 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.
*/
namespace T3o\Ter\Controller;
use B13\Typo3Composerize\Utilities\ComposerManifestCreator;
use B13\Typo3Composerize\Utilities\ExtensionKeyMap;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Request handler for `/composerize` endpoint
*/
class ComposerizeController
{
public function createExtensionComposerMapAction(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse($this->getExtensionComposerMap(), 200);
}
public function createComposerManifestAction(ServerRequestInterface $request): ResponseInterface
{
$extensionKey = $request->getAttribute('routeResult')['key'] ?? '';
$emConf = $request->getBody()->getContents();
if ($extensionKey === '' || $emConf === '') {
return new JsonResponse([], 400);
}
try {
$emConf = json_decode($emConf, true, 512, JSON_THROW_ON_ERROR);
return new JsonResponse((new ComposerManifestCreator(
new ExtensionKeyMap($this->getExtensionComposerMap())
))->createComposerManifest($extensionKey, $emConf), 200);
} catch (\JsonException $e) {
return new JsonResponse([], 400);
}
}
protected function getExtensionComposerMap(): array
{
$extensionComposerMap = [];
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_terfe2_domain_model_extension');
$result = $queryBuilder
->select('ext_key', 'composer_name')
->from('tx_terfe2_domain_model_extension')
->where($queryBuilder->expr()->neq('composer_name', $queryBuilder->createNamedParameter('')))
->execute();
while ($row = $result->fetch()) {
$extensionComposerMap[$row['ext_key']] = [
'composer_name' => $row['composer_name']
];
}
return $extensionComposerMap;
}
}
<?php
declare(strict_types = 1);
/*
* This file is part of TYPO3 CMS-extension "ter", created by Oliver Bartsch.
*
* 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.
*/
namespace T3o\Ter\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use T3o\Ter\Controller\ComposerizeController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This middleware handles requests to the /composerize/* endpoints
*/
class ComposerizeEndpoint implements MiddlewareInterface
{
protected string $base = '/composerize';
protected array $routes = [
'createExtensionComposerMap' => ['endpoint' => '', 'method' => 'GET'],
'createComposerManifest' => ['endpoint' => '/{key}', 'method' => 'POST']
];
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!$this->canHandle($request)) {
return $handler->handle($request);
}
try {
$routeResult = (new UrlMatcher(
$this->getRouteCollection(),
$this->getRequestContext($request)
))->match($request->getUri()->getPath());
} catch (\Exception $e) {
return $handler->handle($request);
}
if (!isset($routeResult['_controller'], $routeResult['_route'])) {
return $handler->handle($request);
}
$controller = GeneralUtility::makeInstance($routeResult['_controller']);
$action = $routeResult['_route'] . 'Action';
if (is_callable([$controller, $action])) {
return $controller->{$action}(
$request->withAttribute('routeResult', array_filter($routeResult, static function ($_, $k) {
return strpos($k, '_') !== 0;
}, ARRAY_FILTER_USE_BOTH)),
);
}
return $handler->handle($request);
}
protected function canHandle(ServerRequestInterface $request): bool
{
return strpos($request->getUri()->getPath(), $this->base) === 0
&& isset(array_flip(array_unique(array_column($this->routes, 'method')))[$request->getMethod()]);
}
protected function getRouteCollection(): RouteCollection
{
$routeCollection = new RouteCollection();
foreach ($this->routes as $name => $options) {
$routeCollection->add(
$name,
(new Route($this->base . $options['endpoint']))
->setMethods([$options['method']])
->setDefault('_controller', ComposerizeController::class)
);
}
return $routeCollection;
}
protected function getRequestContext(ServerRequestInterface $request): RequestContext
{
$uri = $request->getUri();
return new RequestContext(
'',
$request->getMethod(),
(string)idn_to_ascii($uri->getHost()),
$uri->getScheme(),
80,
443,
$uri->getPath(),
$uri->getQuery()
);
}
}
<?php
return [
'frontend' => [
't3o/ter/composerize' => [
'target' => \T3o\Ter\Middleware\ComposerizeEndpoint::class,
'before' => [
'typo3/cms-frontend/base-redirect-resolver'
],
'after' => [
'typo3/cms-frontend/maintenance-mode'
]
],
't3o/ter/soap' => [
'target' => \T3o\Ter\Middleware\SoapEndpoint::class,
'after' => [
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment