-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix controller action invocation with wrong set of arguments results …
…in 500 error instead of 400
- Loading branch information
1 parent
f1e4c9f
commit bb40142
Showing
6 changed files
with
95 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace yii1tech\di\web; | ||
|
||
use yii1tech\di\DI; | ||
|
||
/** | ||
* RunsActionWithParamsViaDI allows dependency injection at the action method level. | ||
* | ||
* @mixin \CAction | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0.3 | ||
*/ | ||
trait RunsActionWithParamsViaDI | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function runWithParamsInternal($object, $method, $params) | ||
{ | ||
$arguments = []; | ||
foreach ($method->getParameters() as $param) { | ||
$name = $param->getName(); | ||
|
||
if ($param->hasType() && !$param->getType()->isBuiltin()) { | ||
if (DI::has($param->getType()->getName())) { | ||
$arguments[$name] = DI::get($param->getType()->getName()); | ||
|
||
continue; | ||
} | ||
} | ||
|
||
if (isset($params[$name])) { | ||
if (version_compare(PHP_VERSION, '8.0', '>=')) { | ||
$isArray = $param->getType() && $param->getType()->getName() === 'array'; | ||
} else { | ||
$isArray = $param->isArray(); | ||
} | ||
|
||
if ($isArray) { | ||
$arguments[$name] = is_array($params[$name]) ? $params[$name] : array($params[$name]); | ||
} elseif (!is_array($params[$name])) { | ||
$arguments[$name] = $params[$name]; | ||
} else { | ||
return false; | ||
} | ||
} elseif ($param->isDefaultValueAvailable()) { | ||
$arguments[$name] = $param->getDefaultValue(); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
$method->invokeArgs($object, $arguments); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters