Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #206 from demiankatz/static-factory-issue
Browse files Browse the repository at this point in the history
Fix problem with string callables failing in PHP 5.6.
  • Loading branch information
weierophinney committed Nov 27, 2017
2 parents fc81dee + 8d84c79 commit d9e36ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,12 @@ private function getFactory($name)
if ($lazyLoaded) {
$this->factories[$name] = $factory;
}
// PHP 5.6 fails on 'class::method' callables unless we explode them:
if (PHP_MAJOR_VERSION < 7
&& is_string($factory) && strpos($factory, '::') !== false
) {
$factory = explode('::', $factory);
}
return $factory;
}

Expand Down
16 changes: 16 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,20 @@ public function testSetAliasShouldWorkWithRecursiveAlias()
$this->assertSame($service, $alias);
$this->assertSame($service, $headAlias);
}

public static function sampleFactory()
{
return new stdClass();
}

public function testFactoryMayBeStaticMethodDescribedByCallableString()
{
$config = [
'factories' => [
stdClass::class => 'ZendTest\ServiceManager\ServiceManagerTest::sampleFactory',
]
];
$serviceManager = new SimpleServiceManager($config);
$this->assertEquals(stdClass::class, get_class($serviceManager->get(stdClass::class)));
}
}

0 comments on commit d9e36ad

Please sign in to comment.