Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow classes to be passed into Injector::inst()->load() #10539

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Core/Injector/InjectionCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ class InjectionCreator implements Factory
{
public function create($class, array $params = [])
{
if (!class_exists($class ?? '')) {
// Allow anonymous classes or other classes to pass without ReflectionClass
if (is_object($class)) {
return $class;
}
elseif (!class_exists($class ?? '')) {
throw new InjectorNotFoundException("Class {$class} does not exist");
}

// Ensure there are no string keys as they cannot be unpacked with the `...` operator
$values = array_values($params ?? []);

return new $class(...$values);
}
}
26 changes: 24 additions & 2 deletions tests/php/Core/Injector/InjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
use SilverStripe\Core\Tests\Injector\InjectorTest\OriginalRequirementsBackend;
use SilverStripe\Core\Tests\Injector\InjectorTest\OtherTestObject;
use SilverStripe\Core\Tests\Injector\InjectorTest\TestObject;
use SilverStripe\Core\Tests\Injector\InjectorTest\TestObjectTwo;
use SilverStripe\Core\Tests\Injector\InjectorTest\TestSetterInjections;
use SilverStripe\Core\Tests\Injector\InjectorTest\TestStaticInjections;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use stdClass;

define('TEST_SERVICES', __DIR__ . '/AopProxyServiceTest');

Expand Down Expand Up @@ -1065,4 +1064,27 @@ public function testNest()
Injector::unnest();
$this->nestingLevel--;
}

public function testAnonymousClass()
{
// load a mock & method override via Injector
Injector::inst()->load([
TestObject::class => [
'class' => new class {
public function foo(): string
{
return TestObject::TEST_BAZ;
}
},
],
]);

$myObject = new TestObjectTwo();

$this->assertEquals(
TestObject::TEST_BAZ,
$myObject->fooViaTestObject(),
'Function return does not match what was mocked above'
);
}
}
10 changes: 10 additions & 0 deletions tests/php/Core/Injector/InjectorTest/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace SilverStripe\Core\Tests\Injector\InjectorTest;

use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Dev\TestOnly;

class TestObject implements TestOnly
{
use Injectable;

const TEST_BAR = 'bar';
const TEST_BAZ = 'baz';

public $sampleService;

Expand All @@ -30,4 +35,9 @@ protected function protectedMethod()
{
$this->methodCalls[] = 'protectedMethod called';
}

public function foo(): string
{
return self::TEST_BAR;
}
}
16 changes: 16 additions & 0 deletions tests/php/Core/Injector/InjectorTest/TestObjectTwo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SilverStripe\Core\Tests\Injector\InjectorTest;

use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Dev\TestOnly;

class TestObjectTwo implements TestOnly
{
public function fooViaTestObject(): string
{
$obj1 = TestObject::create();

return $obj1->foo();
}
}