Skip to content
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ $invoker->call(function ($name = 'world') {

// Invoke any PHP callable
$invoker->call(['MyClass', 'myStaticMethod']);

// Using Class::method syntax
$invoker->call('MyClass::myStaticMethod');
```

Dependency injection in parameters is supported but needs to be configured with your container. Read on or jump to [*Built-in support for dependency injection*](#built-in-support-for-dependency-injection) if you are impatient.
Expand Down Expand Up @@ -222,6 +225,8 @@ $invoker->call(['WelcomeController', 'home']);
$invoker = new Invoker\Invoker(null, $container);
// Now 'WelcomeController' is resolved using the container!
$invoker->call(['WelcomeController', 'home']);
// Alternatively we can use the Class::method syntax
$invoker->call('WelcomeController::home');
```

That feature can be used as the base building block for a framework's dispatcher.
Expand Down
4 changes: 4 additions & 0 deletions src/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function __construct(ParameterResolver $parameterResolver = null, Contain
*/
public function call($callable, array $parameters = array())
{
if (is_string($callable) && strpos($callable, '::') !== false) {
$callable = explode('::', $callable, 2);
}

if ($this->container) {
$callable = $this->resolveCallableFromContainer($callable);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public function should_invoke_static_method()
$this->assertEquals('bar', $result);
}

/**
* @test
*/
public function should_invoke_static_method_with_scope_resolution_syntax()
{
$result = $this->invoker->call('Invoker\Test\InvokerTestStaticFixture::foo');

$this->assertEquals('bar', $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -208,6 +218,20 @@ public function should_resolve_array_callable_from_container()
$this->assertTrue($fixture->wasCalled);
}

/**
* @test
*/
public function should_resolve_callable_from_container_with_scope_resolution_syntax()
{
$fixture = new InvokerTestFixture;
$this->container->set('thing-to-call', $fixture);

$result = $this->invoker->call('thing-to-call::foo');

$this->assertEquals('bar', $result);
$this->assertTrue($fixture->wasCalled);
}

/**
* @test
*/
Expand All @@ -222,6 +246,20 @@ public function should_resolve_array_callable_from_container_with_class_name()
$this->assertTrue($fixture->wasCalled);
}

/**
* @test
*/
public function should_resolve_callable_from_container_with_class_name_in_scope_resolution_syntax()
{
$fixture = new InvokerTestFixture;
$this->container->set('Invoker\Test\InvokerTestFixture', $fixture);

$result = $this->invoker->call('Invoker\Test\InvokerTestFixture::foo');

$this->assertEquals('bar', $result);
$this->assertTrue($fixture->wasCalled);
}

/**
* @test
* @expectedException \Invoker\Exception\NotCallableException
Expand Down