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

contributte / api-router / 6903388818

17 Nov 2023 11:27AM UTC coverage: 88.71% (+0.5%) from 88.235%
6903388818

push

github

f3l1x
Versions: open 6.1.x

275 of 310 relevant lines covered (88.71%)

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
use Traversable;
11

12
class ApiRoutesResolver
13
{
14

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

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

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

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

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

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

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

65
                foreach (array_reverse($keys) as $key) {
1✔
66
                        unset($router[$key]);
1✔
67
                }
68

69
                return $return;
1✔
70
        }
71

72
}
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