Skip to content

Commit

Permalink
handle curly routes
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Jan 1, 2025
1 parent 7323ffa commit 77904f8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/Generator/Server/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use PSX\Api\Generator\Server\Dto\File;
use PSX\Api\OperationInterface;
use PSX\Api\Util\Inflection;
use PSX\Schema\ContentType;
use PSX\Schema\Generator;
use PSX\Schema\GeneratorInterface as SchemaGeneratorInterface;
Expand Down Expand Up @@ -169,7 +170,9 @@ protected function generateMethod(string $operationName, OperationInterface $ope
$type = 'Model\\' . $type;
}

$method = ' #[Route(\'' . $operation->getPath() . '\', methods: [\'' . $operation->getMethod() . '\'])]' . "\n";
$path = Inflection::convertPlaceholderToCurly($operation->getPath());

$method = ' #[Route(\'' . $path . '\', methods: [\'' . $operation->getMethod() . '\'])]' . "\n";
$method.= ' #[StatusCode(' . $operation->getReturn()->getCode() . ')]' . "\n";
$method.= ' public function ' . $operationName . '(' . implode(', ', $arguments) . '): ' . $type . "\n";
$method.= ' {' . "\n";
Expand Down
10 changes: 5 additions & 5 deletions tests/Generator/Server/resource/php/src/Controller/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@

class App extends AbstractController
{
#[Route('/foo/:name/:type', methods: ['GET'])]
#[Route('/foo/{name}/{type}', methods: ['GET'])]
#[StatusCode(200)]
public function get(#[Param] string $name, #[Param] string $type, #[Query] int $startIndex, #[Query] float $float, #[Query] bool $boolean, #[Query] \PSX\DateTime\LocalDate $date, #[Query] \PSX\DateTime\LocalDateTime $datetime, #[Query] Model\Entry $args): Model\EntryCollection
{
// @TODO implement method
}

#[Route('/foo/:name/:type', methods: ['POST'])]
#[Route('/foo/{name}/{type}', methods: ['POST'])]
#[StatusCode(201)]
public function create(#[Param] string $name, #[Param] string $type, #[Body] Model\EntryCreate $payload): Model\EntryMessage
{
// @TODO implement method
}

#[Route('/foo/:name/:type', methods: ['PUT'])]
#[Route('/foo/{name}/{type}', methods: ['PUT'])]
#[StatusCode(200)]
public function update(#[Param] string $name, #[Param] string $type, #[Body] \PSX\Record\Record $payload): \PSX\Record\Record
{
// @TODO implement method
}

#[Route('/foo/:name/:type', methods: ['DELETE'])]
#[Route('/foo/{name}/{type}', methods: ['DELETE'])]
#[StatusCode(204)]
public function delete(#[Param] string $name, #[Param] string $type, #[Body] Model\EntryDelete $payload): Model\EntryMessage
{
// @TODO implement method
}

#[Route('/foo/:name/:type', methods: ['PATCH'])]
#[Route('/foo/{name}/{type}', methods: ['PATCH'])]
#[StatusCode(200)]
public function patch(#[Param] string $name, #[Param] string $type, #[Body] array $payload): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

class Bar extends AbstractController
{
#[Route('/bar/:foo', methods: ['GET'])]
#[Route('/bar/{foo}', methods: ['GET'])]
#[StatusCode(200)]
public function find(#[Param] string $foo): Model\EntryCollection
{
// @TODO implement method
}

#[Route('/bar/:foo', methods: ['POST'])]
#[Route('/bar/{foo}', methods: ['POST'])]
#[StatusCode(201)]
public function put(#[Body] Model\EntryCreate $payload): Model\EntryMessage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

class Baz extends AbstractController
{
#[Route('/bar/$year<[0-9]+>', methods: ['GET'])]
#[Route('/bar/{year}', methods: ['GET'])]
#[StatusCode(200)]
public function get(#[Param] string $year): Model\EntryCollection
{
// @TODO implement method
}

#[Route('/bar/$year<[0-9]+>', methods: ['POST'])]
#[Route('/bar/{year}', methods: ['POST'])]
#[StatusCode(201)]
public function create(#[Body] Model\EntryCreate $payload): Model\EntryMessage
{
Expand Down

0 comments on commit 77904f8

Please sign in to comment.