• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / api-router / 9228273605

24 May 2024 06:17PM UTC coverage: 88.72% (+0.01%) from 88.71%
9228273605

push

github

f3l1x
PHP: require PHP 8.2+

291 of 328 relevant lines covered (88.72%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/DI/ApiRoutesResolver.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\ApiRouter\DI;
4

5
use ArrayAccess;
6
use Contributte\ApiRouter\Exception\ApiRouteWrongRouterException;
7
use Nette\Application\Routers\RouteList;
8
use Nette\Routing\Route;
9
use Nette\Routing\Router;
10

11
class ApiRoutesResolver
12
{
13

14
        /**
15
         * Place REST API routes at the beginnig of all routes
16
         *
17
         * @param Route[] $routes
18
         */
19
        public function prepandRoutes(Router $router, array $routes): void
1✔
20
        {
21
                if ($routes === []) {
1✔
22
                        return;
×
23
                }
24

25
                if (!($router instanceof ArrayAccess)) {
1✔
26
                        throw new ApiRouteWrongRouterException(sprintf(
1✔
27
                                'ApiRoutesResolver can not add ApiRoutes to your router. Use for example %s instead',
1✔
28
                                RouteList::class
1✔
29
                        ));
30
                }
31

32
                $userRoutes = $this->findAndDestroyUserRoutes($router);
1✔
33

34
                /**
35
                 * Add ApiRoutes first
36
                 */
37
                foreach ($routes as $route) {
1✔
38
                        $router[] = $route;
1✔
39
                }
40

41
                /**
42
                 * User routes on second place
43
                 */
44
                foreach ($userRoutes as $route) {
1✔
45
                        $router[] = $route;
1✔
46
                }
47
        }
1✔
48

49
        /**
50
         * @return array<int, Router>
51
         */
52
        public function findAndDestroyUserRoutes(Router $router): array
1✔
53
        {
54
                $keys = [];
1✔
55
                $return = [];
1✔
56

57
                if ($router instanceof RouteList) {
1✔
58
                        foreach ($router->getRouters() as $key => $route) {
1✔
59
                                $return[] = $route;
1✔
60
                                $keys[] = $key;
1✔
61
                        }
62
                }
63

64
                foreach (array_reverse($keys) as $key) {
1✔
65
                        unset($router[$key]); // @phpstan-ignore-line
1✔
66
                }
67

68
                return $return;
1✔
69
        }
70

71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc