Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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: 4 additions & 4 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:

steps:
- name: Checkout project
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
Expand All @@ -96,7 +96,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
id: composer-cache
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:

steps:
- name: Checkout project
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
Expand All @@ -144,7 +144,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
id: composer-cache
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
Expand Down
97 changes: 97 additions & 0 deletions Command/DumpRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace HandcraftedInTheAlps\RestRoutingBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Yaml\Yaml;

class DumpRoutes extends Command
{
private RouterInterface $router;

public function __construct(RouterInterface $router)
{
$this->router = $router;

parent::__construct('fos:rest:routing:dump-symfony-routes');
}

protected function configure(): void
{
$this->addOption(
'name-prefix',
null,
InputOption::VALUE_REQUIRED,
'Only show routes whose name is starting with this string',
);
$this->addOption(
'controller-name',
null,
InputOption::VALUE_REQUIRED,
'Name of the controller to dump',
);
$this->setDescription('This is a list of all routes in the project. Please copy and paste the relevant routes to your config.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$namePrefix = $input->getOption('name-prefix');
$controllerName = $input->getOption('controller-name');

$routes = [];
foreach ($this->router->getRouteCollection() as $name => $route) {
$routeData = $route->__serialize();
// Unset things that are probably defaulted by Symfony
unset($routeData['options']['compiler_class']);
unset($routeData['options']['utf8']);
if (\is_array($routeData['methods'] ?? null) && 1 === \count($routeData['methods'])) {
$routeData['methods'] = $routeData['methods'][0];
}
$routeData['controller'] = $routeData['defaults']['_controller'] ?? '';
unset($routeData['defaults']['_controller']);
$routeData['format'] = $routeData['defaults']['_format'] ?? '';
unset($routeData['defaults']['_controller']);
$defaults = $routeData['defaults'] ?? [];
$requirements = $routeData['requirements'] ?? [];
unset($routeData['defaults']);
unset($routeData['requirements']);
if ([] !== $defaults) {
$routeData['defaults'] = $defaults;
}
if ([] !== $requirements) {
$routeData['requirements'] = $requirements;
}

foreach ($routeData as $key => $value) {
if (!$value) {
unset($routeData[$key]);
}
}

if ($this->filterMatches($name, $routeData, $namePrefix, $controllerName)) {
$routes[$name] = $routeData;
}
}

$output->writeln(Yaml::dump($routes));

return Command::SUCCESS;
}

private function filterMatches(string $name, array $data, ?string $namePrefix, ?string $controllerName): bool
{
if (null !== $namePrefix) {
return 0 === strpos($name, $namePrefix);
}

if (null !== $controllerName) {
return 0 === strpos($data['controller'], $controllerName);
}

return true;
}
}
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ This bundle provides the automatic route generation for the FOSRestBundle 3.0.

All the installation instructions are located in the [documentation](Resources/doc/1-setting_up_the_bundle.rst).

## Opt-out of the `type: rest` routing

You might want to migrate away from this bundle and use normal Symfony routes.
Via the command inside this bundle you can convert all `type: rest` routes to normal Symfony routes:

```bash
bin/console fos:rest:routing:dump-symfony-routes

# filter by a specific controller
bin/console fos:rest:routing:dump-symfony-routes --controller="your.controller.service"

# filter by a specific name prefix
bin/console fos:rest:routing:dump-symfony-routes --name-prefix="your_prefix."
```

Copy the result into a `routing.yaml` file of your choice.

## Switching from FOSRestBundle

If you did before using the FOSRestBundle which removed the auto route generation the switch is easy.
Expand Down
5 changes: 5 additions & 0 deletions Resources/config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
</service>

<service id="handcraftedinthealps_rest_routing.inflector.doctrine" class="HandcraftedInTheAlps\RestRoutingBundle\Inflector\DoctrineInflector" public="false" />

<service id="handcraftedinthealps_rest_routing.command" class="HandcraftedInTheAlps\RestRoutingBundle\Command\DumpRoutes">
<argument type="service" id="router" />
<tag name="console.command" />
</service>
</services>

</container>
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"php": "^7.2 || ^8.0",
"doctrine/annotations": "^1.0|^2.0",
"doctrine/inflector": "^1.4.1|^2.0",
"symfony/console": "^4.4|^5.0|^6.0|^7.0",
"symfony/config": "^4.4|^5.0|^6.0|^7.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0|^7.0",
"symfony/finder": "^4.4|^5.0|^6.0|^7.0",
Expand Down
Loading