mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENH Faster method for creating injected instances (#10265)
This commit is contained in:
parent
76bd8a116a
commit
78c9aea61f
@ -2,29 +2,18 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Core\Injector;
|
namespace SilverStripe\Core\Injector;
|
||||||
|
|
||||||
use ReflectionClass;
|
|
||||||
use ReflectionException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class for creating new objects by the injector.
|
* A class for creating new objects by the injector.
|
||||||
*/
|
*/
|
||||||
class InjectionCreator implements Factory
|
class InjectionCreator implements Factory
|
||||||
{
|
{
|
||||||
|
|
||||||
public function create($class, array $params = [])
|
public function create($class, array $params = [])
|
||||||
{
|
{
|
||||||
try {
|
if (!class_exists($class)) {
|
||||||
$reflector = new ReflectionClass($class);
|
throw new InjectorNotFoundException("Class {$class} does not exist");
|
||||||
} catch (ReflectionException $e) {
|
|
||||||
throw new InjectorNotFoundException($e);
|
|
||||||
}
|
}
|
||||||
|
// Ensure there are no string keys as they cannot be unpacked with the `...` operator
|
||||||
if (count($params)) {
|
$values = array_values($params);
|
||||||
// Remove named keys to ensure that PHP7 and PHP8 interpret these the same way
|
return new $class(...$values);
|
||||||
$params = array_values($params);
|
|
||||||
return $reflector->newInstanceArgs($params);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $reflector->newInstance();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user