Skip to content

Commit 2bf8c32

Browse files
committed
fix: dependency injection issue
-- Failed to instantiate class for action params -- It occured due mismatched param type -- Eloquent model constructor accepts array but it passes scalar value -- Now, if not possible to create object, will return original value
1 parent c1b24a2 commit 2bf8c32

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/Http/Router/AjaxRouter.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,16 @@ public function registerRoutes()
2727

2828
public function addRoute(RouteRegister $route)
2929
{
30-
3130
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? sanitize_text_field($_SERVER['REQUEST_METHOD']) : '';
32-
$action = isset($_REQUEST['action']) ? sanitize_text_field($_REQUEST['action']) : '';
31+
$action = isset($_REQUEST['action']) ? sanitize_text_field($_REQUEST['action']) : '';
3332

34-
if (strpos($action, $this->_router->getAjaxPrefix()) === false
33+
if (strpos($action, $route->getRouter()->getAjaxPrefix()) === false
3534
|| !\in_array(strtoupper($requestMethod), $route->getMethods())
3635
) {
3736
return;
3837
}
3938

40-
$requestPath = str_replace($this->_router->getAjaxPrefix(), '', $action);
39+
$requestPath = str_replace($route->getRouter()->getAjaxPrefix(), '', $action);
4140
if (!$this->isRouteMatched($route, $requestPath)) {
4241
return;
4342
}
@@ -47,15 +46,15 @@ public function addRoute(RouteRegister $route)
4746
Hooks::addAction('wp_ajax_nopriv_' . $action, [$route, 'handleRequest']);
4847
}
4948

50-
$this->_router->addRegisteredRoute($this->currentRouteName(), $route);
49+
$route->getRouter()->addRegisteredRoute($this->currentRouteName(), $route);
5150
}
5251

5352
public function currentRouteName()
5453
{
5554
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? sanitize_text_field($_SERVER['REQUEST_METHOD']) : '';
56-
$action = isset($_REQUEST['action']) ? sanitize_text_field($_REQUEST['action']) : '';
57-
58-
return $requestMethod. $action;
55+
$action = isset($_REQUEST['action']) ? sanitize_text_field($_REQUEST['action']) : '';
56+
57+
return $requestMethod . $action;
5958
}
6059

6160
/**

src/Http/Router/RouteRegister.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,15 @@ public function getParamValue(ReflectionParameter $param)
250250
$this->setRequest($type);
251251
$value = $this->getRequest();
252252
} elseif ($isRouteParam && $value === $isRouteParam && method_exists($type, '__construct')) {
253-
$value = new $type($value);
253+
$constructor = new ReflectionMethod($type, '__construct');
254+
if ($constructor->getNumberOfParameters() === 1) {
255+
$parameter = $constructor->getParameters()[0];
256+
if ($parameter->getType()->getName() != 'array') {
257+
$value = new $type($value);
258+
} elseif (method_exists($type, 'query')) {
259+
$value = $type::query()->find($value);
260+
}
261+
}
254262
} elseif (!$param->isOptional()) {
255263
$value = new $type();
256264
}

src/Http/Router/Router.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public static function instance($type = 'ajax', $namespace = null, $version = nu
8888
return self::$_instance;
8989
}
9090

91+
public function registerFile($routeFile)
92+
{
93+
self::$_instance = $this;
94+
95+
include_once $routeFile;
96+
}
97+
9198
public function register()
9299
{
93100
if ($this->getRequestType() === 'ajax') {

0 commit comments

Comments
 (0)