Skip to content

Commit 67ad285

Browse files
committed
Merge pull request #3 from jdreesen/paamayim-nekudotayim
Support 'Class::method' syntax for callables
2 parents 2eb8f3a + a03d213 commit 67ad285

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ $invoker->call(function ($name = 'world') {
119119

120120
// Invoke any PHP callable
121121
$invoker->call(['MyClass', 'myStaticMethod']);
122+
123+
// Using Class::method syntax
124+
$invoker->call('MyClass::myStaticMethod');
122125
```
123126

124127
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.
@@ -222,6 +225,8 @@ $invoker->call(['WelcomeController', 'home']);
222225
$invoker = new Invoker\Invoker(null, $container);
223226
// Now 'WelcomeController' is resolved using the container!
224227
$invoker->call(['WelcomeController', 'home']);
228+
// Alternatively we can use the Class::method syntax
229+
$invoker->call('WelcomeController::home');
225230
```
226231

227232
That feature can be used as the base building block for a framework's dispatcher.

src/Invoker.php

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function __construct(ParameterResolver $parameterResolver = null, Contain
4040
*/
4141
public function call($callable, array $parameters = array())
4242
{
43+
if (is_string($callable) && strpos($callable, '::') !== false) {
44+
$callable = explode('::', $callable, 2);
45+
}
46+
4347
if ($this->container) {
4448
$callable = $this->resolveCallableFromContainer($callable);
4549
}

tests/InvokerTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public function should_invoke_static_method()
6161
$this->assertEquals('bar', $result);
6262
}
6363

64+
/**
65+
* @test
66+
*/
67+
public function should_invoke_static_method_with_scope_resolution_syntax()
68+
{
69+
$result = $this->invoker->call('Invoker\Test\InvokerTestStaticFixture::foo');
70+
71+
$this->assertEquals('bar', $result);
72+
}
73+
6474
/**
6575
* @test
6676
*/
@@ -208,6 +218,20 @@ public function should_resolve_array_callable_from_container()
208218
$this->assertTrue($fixture->wasCalled);
209219
}
210220

221+
/**
222+
* @test
223+
*/
224+
public function should_resolve_callable_from_container_with_scope_resolution_syntax()
225+
{
226+
$fixture = new InvokerTestFixture;
227+
$this->container->set('thing-to-call', $fixture);
228+
229+
$result = $this->invoker->call('thing-to-call::foo');
230+
231+
$this->assertEquals('bar', $result);
232+
$this->assertTrue($fixture->wasCalled);
233+
}
234+
211235
/**
212236
* @test
213237
*/
@@ -222,6 +246,20 @@ public function should_resolve_array_callable_from_container_with_class_name()
222246
$this->assertTrue($fixture->wasCalled);
223247
}
224248

249+
/**
250+
* @test
251+
*/
252+
public function should_resolve_callable_from_container_with_class_name_in_scope_resolution_syntax()
253+
{
254+
$fixture = new InvokerTestFixture;
255+
$this->container->set('Invoker\Test\InvokerTestFixture', $fixture);
256+
257+
$result = $this->invoker->call('Invoker\Test\InvokerTestFixture::foo');
258+
259+
$this->assertEquals('bar', $result);
260+
$this->assertTrue($fixture->wasCalled);
261+
}
262+
225263
/**
226264
* @test
227265
* @expectedException \Invoker\Exception\NotCallableException

0 commit comments

Comments
 (0)