Skip to content

Commit 62d4c1d

Browse files
committed
Singleton has been implemented to avoid multiple instances.
1 parent bfd2650 commit 62d4c1d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/LionRoute/Route.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
use Phroute\Phroute\{ RouteCollector, RouteParser, Dispatcher };
66
use Phroute\Phroute\Exception\{ HttpRouteNotFoundException, HttpMethodNotAllowedException };
77
use LionRoute\Middleware;
8+
use LionRoute\Singleton;
89

910
class Route {
1011

12+
use Singleton;
13+
1114
private static RouteCollector $router;
1215
private static array $addMiddleware = [];
13-
14-
public function __construct() {
15-
16-
}
1716

1817
public static function init(): Route {
1918
self::$router = new RouteCollector(new RouteParser());
20-
return new Route();
19+
return self::getInstance();
2120
}
2221

2322
public static function getRoutes(): array {

src/LionRoute/Singleton.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace LionRoute;
4+
5+
trait Singleton {
6+
7+
private static $singleton = false;
8+
9+
final private function __construct() {
10+
11+
}
12+
13+
final public static function getInstance() {
14+
if (self::$singleton === false) {
15+
self::$singleton = new self();
16+
}
17+
18+
return self::$singleton;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)