From 00f58db131cc204cfd1e7e06ebc3a805848c1a37 Mon Sep 17 00:00:00 2001
From: Mert Erdem <50269917+Venloress@users.noreply.github.com>
Date: Wed, 14 Jul 2021 17:11:18 +0300
Subject: [PATCH 1/7] Group specific error pages and prefix error pages

- Added creating custom error pages for groups
- Added route prefix for error function
- `class@method` is now available in error function

You can browse the test folder for usage examples of the added features.
=> https://github.com/venloress/php-router/tree/venlo/tests
---
 src/Router.php | 52 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 47 insertions(+), 5 deletions(-)

diff --git a/src/Router.php b/src/Router.php
index fce89e2..b45009c 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -57,6 +57,16 @@ class Router
      */
     protected $groups = [];
 
+    /**
+     * @var array List of error group routes
+     */
+    protected $errorGroupNames = [];
+
+    /**
+     * @var string Error group of requested page
+     */
+    protected $currentErrorGroupName = '/';
+
     /**
      * @var array $patterns Pattern definitions for parameters of Route
      */
@@ -128,7 +138,7 @@ class Router
     /**
      * @var RouterRequest
      */
-    private $request;
+    public $request;
 
     /**
      * Router constructor method.
@@ -324,7 +334,14 @@ public function run(): void
                     return $this->exception('Looks like page not found or something went wrong. Please try again.');
                 };
             }
-            call_user_func($this->errorCallback);
+
+            foreach ($this->errorGroupNames as $key => $item) {
+                if (str_starts_with($this->getRequestUri(), $item))
+                    $groupKey = $key;
+            }
+            if (isset($groupKey))
+                $this->currentErrorGroupName = $this->errorGroupNames[$groupKey];
+            $this->routerCommand()->runRoute($this->errorCallback[$this->currentErrorGroupName]);
         }
     }
 
@@ -349,6 +366,8 @@ public function group(string $prefix, Closure $callback, array $options = []): b
         $group['after'] = $this->calculateMiddleware($options['after'] ?? []);
 
         array_push($this->groups, $group);
+        
+        $this->getErrorGroupNames();
 
         if (is_object($callback)) {
             call_user_func_array($callback, [$this]);
@@ -359,6 +378,19 @@ public function group(string $prefix, Closure $callback, array $options = []): b
         return true;
     }
 
+    /**
+     * Gets errors generated under group
+     *
+     */
+    public function getErrorGroupNames()
+    {
+        $group = '';
+        foreach ($this->groups as $item) {
+            $group .= $item['route'];
+        }
+        $this->errorGroupNames[] = $group.'/';
+    }
+    
     /**
      * Added route from methods of Controller file.
      *
@@ -428,13 +460,23 @@ public function controller(string $route, string $controller, array $options = [
     /**
      * Routes error function.
      *
-     * @param Closure $callback
+     * @param $callback
+     * @param array $options
      *
      * @return void
      */
-    public function error(Closure $callback): void
+    public function error($callback,array $options = []): void
     {
-        $this->errorCallback = $callback;
+        if (isset($options['prefix'])) {
+            $prefix = $options['prefix'];
+            $this->errorGroupNames[] = $prefix.'/';
+        } else {
+            $prefix = '';
+            foreach ($this->groups as $item) {
+                $prefix .= $item['route'];
+            }
+        }
+        $this->errorCallback[$prefix.'/'] = $callback;
     }
 
     /**

From 8c8d90109422e9538705b62c6873f31978020b67 Mon Sep 17 00:00:00 2001
From: Mert Erdem <50269917+Venloress@users.noreply.github.com>
Date: Wed, 14 Jul 2021 17:18:16 +0300
Subject: [PATCH 2/7] Examples of newly added features - error function

- Added creating custom error pages for groups
- Added route prefix for error function
- `class@method` is now available in error function

Thanks for your time
---
 tests/error-test.php | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 tests/error-test.php

diff --git a/tests/error-test.php b/tests/error-test.php
new file mode 100644
index 0000000..2210d3b
--- /dev/null
+++ b/tests/error-test.php
@@ -0,0 +1,42 @@
+<?php
+
+$router->group('/test', function ($router) {
+    $router->get('/',function () use ($router) {
+        return 'text page';
+    });
+    $router->get('/blue',function () {
+       echo 'blue page';
+    });
+    $router->group('/testing', function ($rou) {
+        $rou->get('/',function () {
+            echo 'testing page';
+        });
+        $rou->error(function () {
+            echo 'testing error page';
+        });
+    });
+
+
+    $router->error(function () {
+        echo 'test error page';
+    });
+});
+
+$router->group('/test2', function ($router) {
+    $router->get('/',function () {
+        echo 'test2 page';
+    });
+    $router->get('/yellow',function () {
+        echo 'yellow page';
+    });
+    $router->error(function () {
+        die('test2 error page');
+    });
+});
+
+
+$router->error(function () {
+    echo 'main error page';
+});
+
+$router->error('home@notfound',['prefix' => '/test3']);

From cc6c8ce1c0a2fb75fa9bb4caedd639ae9f6d66fc Mon Sep 17 00:00:00 2001
From: Mert Erdem <50269917+Venloress@users.noreply.github.com>
Date: Wed, 14 Jul 2021 17:40:27 +0300
Subject: [PATCH 3/7] Update Router.php

---
 src/Router.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Router.php b/src/Router.php
index b45009c..28d65f2 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -138,7 +138,7 @@ class Router
     /**
      * @var RouterRequest
      */
-    public $request;
+    protected $request;
 
     /**
      * Router constructor method.

From 3d18a9610c56c7e308db4f6cf8ea4f6e02aba134 Mon Sep 17 00:00:00 2001
From: Mert Erdem <50269917+Venloress@users.noreply.github.com>
Date: Wed, 14 Jul 2021 17:41:03 +0300
Subject: [PATCH 4/7] Update Router.php

---
 src/Router.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Router.php b/src/Router.php
index 28d65f2..af83d3d 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -138,7 +138,7 @@ class Router
     /**
      * @var RouterRequest
      */
-    protected $request;
+    private $request;
 
     /**
      * Router constructor method.

From 372322d459ba9b9bce2721e7d8ca298cd1646548 Mon Sep 17 00:00:00 2001
From: Mert Erdem <50269917+Venloress@users.noreply.github.com>
Date: Wed, 14 Jul 2021 18:05:19 +0300
Subject: [PATCH 5/7] Fixed default error page

According to my last edits. Fixed an issue where I couldn't reach the default error page
---
 src/Router.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Router.php b/src/Router.php
index af83d3d..cd9a996 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -326,8 +326,8 @@ public function run(): void
         }
 
         if ($foundRoute === false) {
-            if (!$this->errorCallback) {
-                $this->errorCallback = function () {
+            if (!isset($this->errorCallback[$this->currentErrorGroupName])) {
+                $this->errorCallback['/'] = function () {
                     $this->response()
                         ->setStatusCode(Response::HTTP_NOT_FOUND)
                         ->sendHeaders();

From 8becebd3cab0773fea1c07d4964d659885dec7d1 Mon Sep 17 00:00:00 2001
From: Mert Erdem <50269917+Venloress@users.noreply.github.com>
Date: Thu, 15 Jul 2021 00:09:44 +0300
Subject: [PATCH 6/7] Fixed foreach priority

---
 src/Router.php | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/Router.php b/src/Router.php
index cd9a996..d2e6edd 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -326,6 +326,13 @@ public function run(): void
         }
 
         if ($foundRoute === false) {
+            foreach ($this->errorGroupNames as $key => $item) {
+                if (str_starts_with($this->getRequestUri(), $item))
+                    $groupKey = $key;
+            }
+            if (isset($groupKey))
+                $this->currentErrorGroupName = $this->errorGroupNames[$groupKey];
+
             if (!isset($this->errorCallback[$this->currentErrorGroupName])) {
                 $this->errorCallback['/'] = function () {
                     $this->response()
@@ -333,14 +340,8 @@ public function run(): void
                         ->sendHeaders();
                     return $this->exception('Looks like page not found or something went wrong. Please try again.');
                 };
+                $this->currentErrorGroupName = '/';
             }
-
-            foreach ($this->errorGroupNames as $key => $item) {
-                if (str_starts_with($this->getRequestUri(), $item))
-                    $groupKey = $key;
-            }
-            if (isset($groupKey))
-                $this->currentErrorGroupName = $this->errorGroupNames[$groupKey];
             $this->routerCommand()->runRoute($this->errorCallback[$this->currentErrorGroupName]);
         }
     }

From c8d07d4b2b2917372db367221d25289fab505787 Mon Sep 17 00:00:00 2001
From: Mertcan Erdem <50269917+Venloress@users.noreply.github.com>
Date: Fri, 16 Jul 2021 09:23:39 +0300
Subject: [PATCH 7/7] Update Router.php

---
 src/Router.php | 71 ++++++++++++++++++++++++++++----------------------
 1 file changed, 40 insertions(+), 31 deletions(-)

diff --git a/src/Router.php b/src/Router.php
index d2e6edd..69a9e44 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -326,23 +326,7 @@ public function run(): void
         }
 
         if ($foundRoute === false) {
-            foreach ($this->errorGroupNames as $key => $item) {
-                if (str_starts_with($this->getRequestUri(), $item))
-                    $groupKey = $key;
-            }
-            if (isset($groupKey))
-                $this->currentErrorGroupName = $this->errorGroupNames[$groupKey];
-
-            if (!isset($this->errorCallback[$this->currentErrorGroupName])) {
-                $this->errorCallback['/'] = function () {
-                    $this->response()
-                        ->setStatusCode(Response::HTTP_NOT_FOUND)
-                        ->sendHeaders();
-                    return $this->exception('Looks like page not found or something went wrong. Please try again.');
-                };
-                $this->currentErrorGroupName = '/';
-            }
-            $this->routerCommand()->runRoute($this->errorCallback[$this->currentErrorGroupName]);
+            $this->notFoundRoute();
         }
     }
 
@@ -367,7 +351,7 @@ public function group(string $prefix, Closure $callback, array $options = []): b
         $group['after'] = $this->calculateMiddleware($options['after'] ?? []);
 
         array_push($this->groups, $group);
-        
+
         $this->getErrorGroupNames();
 
         if (is_object($callback)) {
@@ -379,19 +363,6 @@ public function group(string $prefix, Closure $callback, array $options = []): b
         return true;
     }
 
-    /**
-     * Gets errors generated under group
-     *
-     */
-    public function getErrorGroupNames()
-    {
-        $group = '';
-        foreach ($this->groups as $item) {
-            $group .= $item['route'];
-        }
-        $this->errorGroupNames[] = $group.'/';
-    }
-    
     /**
      * Added route from methods of Controller file.
      *
@@ -574,6 +545,44 @@ public function getMiddlewares(): array
         ];
     }
 
+    /**
+     * Gets errors generated under group
+     *
+     * @return void
+     */
+    protected function getErrorGroupNames()
+    {
+        $group = '';
+        foreach ($this->groups as $item) {
+            $group .= $item['route'];
+        }
+        $this->errorGroupNames[] = $group.'/';
+    }
+
+    protected function notFoundRoute()
+    {
+        foreach ($this->errorGroupNames as $key => $item) {
+            if (str_starts_with($this->getRequestUri(), $item))
+                $groupKey = $key;
+        }
+        if (isset($groupKey))
+            $this->currentErrorGroupName = $this->errorGroupNames[$groupKey];
+
+        if (!isset($this->errorCallback[$this->currentErrorGroupName])) {
+            if (!isset($this->errorCallback['/']))
+                $this->errorCallback['/'] = function () {
+                    $this->response()
+                        ->setStatusCode(Response::HTTP_NOT_FOUND)
+                        ->sendHeaders();
+                    return $this->exception('Looks like page not found or something went wrong. Please try again.');
+                };
+            $this->currentErrorGroupName = '/';
+        }
+        
+        $this->response()->setStatusCode(Response::HTTP_NOT_FOUND)->sendHeaders();
+        $this->routerCommand()->runRoute($this->errorCallback[$this->currentErrorGroupName]);
+    }
+
     /**
      * Detect Routes Middleware; before or after
      *