Skip to content

Commit

Permalink
ENH Faster method for creating injected instances (silverstripe#10265)
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz authored and GuySartorelli committed Jul 6, 2022
1 parent fc4478b commit b5f3544
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/Core/Injector/InjectionCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@

namespace SilverStripe\Core\Injector;

use ReflectionClass;
use ReflectionException;

/**
* A class for creating new objects by the injector.
*/
class InjectionCreator implements Factory
{

public function create($class, array $params = [])
{
try {
$reflector = new ReflectionClass($class);
} catch (ReflectionException $e) {
throw new InjectorNotFoundException($e);
if (!class_exists($class)) {
throw new InjectorNotFoundException("Class {$class} does not exist");
}

if (count($params)) {
// Remove named keys to ensure that PHP7 and PHP8 interpret these the same way
$params = array_values($params);
return $reflector->newInstanceArgs($params);
}

return $reflector->newInstance();
// Ensure there are no string keys as they cannot be unpacked with the `...` operator
$values = array_values($params);
return new $class(...$values);
}
}

0 comments on commit b5f3544

Please sign in to comment.