Skip to content
Merged
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
6 changes: 3 additions & 3 deletions bin/membrane
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ use Symfony\Component\Console\Application;
$application = new Application();

if (class_exists(CacheOpenAPIProcessors::class)) {
$application->add(new CacheOpenAPIProcessors());
$application->addCommand(new CacheOpenAPIProcessors());
}

if (class_exists(CacheOpenAPI::class)) {
$application->add(new CacheOpenAPI());
$application->addCommand(new CacheOpenAPI());
}

if (class_exists(CacheOpenAPIRoutes::class)) {
$application->add(new CacheOpenAPIRoutes());
$application->addCommand(new CacheOpenAPIRoutes());
}

$application->run();
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"psr/log": "^3.0.2",
"symfony/console": "^7.4.19 || ^8.1.7",
"membrane/openapi-reader": "^3.1.1",
"membrane/openapi-router": "0.5.3"
"membrane/openapi-router": "^0.6"
},
"require-dev": {
"phpunit/phpunit": "^13.1.14",
Expand Down
49 changes: 45 additions & 4 deletions src/Console/Command/CacheOpenAPIProcessors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Membrane\Console\Command;

use Membrane\Console\Service;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputArgument, InputInterface, InputOption};
Expand All @@ -30,7 +31,6 @@ protected function configure(): void
getcwd() . '/cache'
);


self::addOption(
'namespace',
null,
Expand All @@ -39,6 +39,22 @@ protected function configure(): void
'Membrane\Cache'
);

self::addOption(
'request-processor-classname',
null,
InputOption::VALUE_OPTIONAL,
'Classname for the generated request processor',
'CachedRequestBuilder'
);

self::addOption(
'response-processor-classname',
null,
InputOption::VALUE_OPTIONAL,
'Classname for the generated response processor',
'CachedResponseBuilder'
);

self::addOption(
'skip-requests',
null,
Expand All @@ -54,6 +70,14 @@ protected function configure(): void
'Skip generation of Response processors',
null,
);

self::addOption(
'route-match',
null,
InputOption::VALUE_NONE,
'Use RouteMatch Builder for multi-file request handling',
null,
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -68,16 +92,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
assert(is_bool($skipResponses));
$skipRequests = $input->getOption('skip-requests');
assert(is_bool($skipRequests));
$routeMatch = $input->getOption('route-match');
assert(is_bool($routeMatch));
$requestName = $input->getOption('request-processor-classname');
assert(is_string($requestName));
$responseName = $input->getOption('response-processor-classname');
assert(is_string($responseName));

$consoleLogger = new ConsoleLogger($output);

if ($skipResponses && $skipRequests) {
$consoleLogger->warning('Skipping both requests and responses, nothing will be generated');
$consoleLogger->warning(
'Skipping both requests and responses, nothing will be generated',
);
}

$cachingService = new \Membrane\Console\Service\CacheOpenAPIProcessors($consoleLogger);
$success = new Service\CacheOpenAPIProcessors($consoleLogger)
->cache(
$openAPIFilePath,
$destination,
$namespace,
!$skipRequests,
!$skipResponses,
$routeMatch,
$requestName,
$responseName,
);

$success = $cachingService->cache($openAPIFilePath, $destination, $namespace, !$skipRequests, !$skipResponses);
return $success ? Command::SUCCESS : Command::FAILURE;
}
}
63 changes: 53 additions & 10 deletions src/Console/Service/CacheOpenAPIProcessors.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

use Atto\CodegenTools\ClassDefinition\PHPClassDefinitionProducer;
use Atto\CodegenTools\CodeGeneration\PHPFilesWriter;
use Membrane\Console\Template;
use Membrane\OpenAPIReader\Exception\CannotRead;
use Membrane\OpenAPIReader\Exception\CannotSupport;
use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\MembraneReader;
use Membrane\OpenAPIReader\OpenAPIVersion;
use Psr\Log\LoggerInterface;

class CacheOpenAPIProcessors
Expand All @@ -25,17 +24,61 @@ public function cache(
string $cacheDestinationFilePath,
string $cacheNamespace,
bool $buildRequests = true,
bool $buildResponses = true
bool $buildResponses = true,
bool $buildRouteMatch = false,
string $requestClassname = 'CachedRequestBuilder',
string $responseClassname = 'CachedResponseBuilder',
): bool {
$yieldsClasses = new YieldsClassDefinitions($this->logger);
$yieldsClasses = new YieldsProcessors(
$this->logger,
$openAPIFilePath,
$cacheNamespace,
$buildRequests,
$buildResponses,
);

$gensClasses = function () use (
$yieldsClasses,
$openAPIFilePath,
$cacheNamespace,
$buildRequests,
$buildResponses,
$buildRouteMatch,
$requestClassname,
$responseClassname,
) {
yield from $yieldsClasses();

if ($buildRequests) {
if ($buildRouteMatch) {
yield new Template\RouteMatchBuilder(
$cacheNamespace,
$requestClassname,
$openAPIFilePath,
array_map(fn($p) => $p['request'], $yieldsClasses->classMap),
);
} else {
yield new Template\RequestBuilder(
$cacheNamespace,
$requestClassname,
$openAPIFilePath,
array_map(fn($p) => $p['request'], $yieldsClasses->classMap),
);
}
}

if ($buildResponses) {
yield new Template\ResponseBuilder(
$cacheNamespace,
$responseClassname,
$openAPIFilePath,
array_map(fn($p) => $p['response'], $yieldsClasses->classMap),
);
}
};

try {
$definitionProducer = new PHPClassDefinitionProducer($yieldsClasses(
$openAPIFilePath,
$cacheNamespace,
$buildRequests,
$buildResponses,
));
$definitionProducer = new PHPClassDefinitionProducer($gensClasses());

$destination = rtrim($cacheDestinationFilePath, '/');
$classWriter = new PHPFilesWriter($destination, $cacheNamespace);
Expand Down
151 changes: 0 additions & 151 deletions src/Console/Service/YieldsClassDefinitions.php

This file was deleted.

Loading
Loading