From a03d213ec8a0435fdf2c8afbbd3af762fb4605d8 Mon Sep 17 00:00:00 2001 From: jdreesen Date: Sat, 12 Sep 2015 01:01:11 +0200 Subject: [PATCH] Support 'Class::method' syntax for callables --- README.md | 5 +++++ src/Invoker.php | 4 ++++ tests/InvokerTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/README.md b/README.md index d947e56..19adfe5 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/src/Invoker.php b/src/Invoker.php index 0222176..0a530db 100644 --- a/src/Invoker.php +++ b/src/Invoker.php @@ -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); } diff --git a/tests/InvokerTest.php b/tests/InvokerTest.php index 4cd2f1f..67e5212 100644 --- a/tests/InvokerTest.php +++ b/tests/InvokerTest.php @@ -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 */ @@ -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 */ @@ -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