diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 01d0e74..ae81fc9 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -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 @@ -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 }} @@ -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 @@ -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 }} diff --git a/Command/DumpRoutes.php b/Command/DumpRoutes.php new file mode 100644 index 0000000..148c264 --- /dev/null +++ b/Command/DumpRoutes.php @@ -0,0 +1,97 @@ +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; + } +} diff --git a/README.md b/README.md index 7d3f53d..49d1f88 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Resources/config/routing.xml b/Resources/config/routing.xml index 7f9a879..4304dce 100644 --- a/Resources/config/routing.xml +++ b/Resources/config/routing.xml @@ -53,6 +53,11 @@ + + + + + diff --git a/composer.json b/composer.json index 9dfb1b9..1f9f74a 100644 --- a/composer.json +++ b/composer.json @@ -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",