Browse Source

Make routes report the number of handles per route

Nathaniel van Diepen 6 years ago
parent
commit
2cbb005003
3 changed files with 23 additions and 8 deletions
  1. 4 1
      App/path.class.php
  2. 9 3
      App/router.class.php
  3. 10 4
      app.class.php

+ 4 - 1
App/path.class.php

@@ -7,7 +7,7 @@
 	require_once('arguments.class.php');
 	use \Juju\Http\{Request, Response};
 
-	class Path implements \JsonSerializable{
+	class Path implements \JsonSerializable, \Countable{
 		private $handles = [];
 		public $path;
 		public function __construct(string $path){
@@ -50,5 +50,8 @@
 		public function args(string $path){
 			return $this->path->args($path);
 		}
+		public function count(){
+			return count($this->handles);
+		}
 	}
 ?>

+ 9 - 3
App/router.class.php

@@ -35,11 +35,17 @@
 					$routes = [];
 					$base = rtrim($this->_base, '/');
 					foreach($this->_paths as $path){
-						$routes[] = $base.$path->path;
+						if(!isset($routes[$base.$path->path])){
+							$routes[$base.$path->path] = 0;
+						}
+						$routes[$base.$path->path] += count($path);
 					}
 					foreach($this->_routers as $router){
-						foreach($router->routes as $route){
-							$routes[] = $base.$route;
+						foreach($router->routes as $route => $count){
+							if(!isset($routes[$base.$route])){
+								$routes[$base.$route] = 0;
+							}
+							$routes[$base.$route] += $count;
 						}
 					}
 					return $routes;

+ 10 - 4
app.class.php

@@ -66,13 +66,19 @@
 				case 'routes':
 					$routes = $this->router->routes;
 					foreach($this->routers as $prefix => $router){
-						foreach($router->routes as $route){
-							$routes[] = $prefix.$route;
+						foreach($router->routes as $route => $count){
+							if(!isset($routes[$prefix.$route])){
+								$routes[$prefix.$route] = 0;
+							}
+							$routes[$prefix.$route] += $count;
 						}
 					}
 					foreach($this->domains as $domain => $router){
-						foreach($router->routes as $route){
-							$routes[] = $domain.$route;
+						foreach($router->routes as $route => $count){
+							if(!isset($routes[$domain.$route])){
+								$routes[$domain.$route] = 0;
+							}
+							$routes[$domain.$route] += $count;
 						}
 					}
 					return $routes;