Skip to content

Instantly share code, notes, and snippets.

@adamlundrigan
Last active August 29, 2015 14:05
Show Gist options
  • Save adamlundrigan/e185f654a04e782a11a6 to your computer and use it in GitHub Desktop.
Save adamlundrigan/e185f654a04e782a11a6 to your computer and use it in GitHub Desktop.
PoC of Zend\ServiceManager bug
*
!.gitignore
!run.php
!composer.json
!result.txt
{
"name": "adamlundrigan/sm_bug_poc",
"authors": [
{
"name": "Adam Lundrigan",
"email": "adam@lundrigan.ca"
}
],
"require": {
"zendframework/zendframework": "dev-master@dev"
}
}
adam@adampc:/var/www/zf2/sm_bug_poc$ php run.php
PHP Warning: assert(): Assertion failed in /var/www/zf2/sm_bug_poc/run.php on line 42
PHP Stack trace:
PHP 1. {main}() /var/www/zf2/sm_bug_poc/run.php:0
PHP 2. assert() /var/www/zf2/sm_bug_poc/run.php:42
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config;
class MyTestService {}
$mainManager = new ServiceManager(
new Config(
array(
'invokables' => array( 'foo_service' => 'MyTestService' ),
'shared' => array( 'foo_service' => false ),
)
)
);
// Test #1 - Retrieve non-shared service from main SM twice
$obj1 = $mainManager->get('foo_service');
$obj2 = $mainManager->get('foo_service');
assert($obj1 !== $obj2);
// Test #2 - Retrieve non-shared service from child SM twice when peer SM is checked first
$childManager = new ServiceManager(new Config());
$childManager->addPeeringServiceManager($mainManager);
$childManager->setRetrieveFromPeeringManagerFirst(true);
$obj1 = $childManager->get('foo_service');
$obj2 = $childManager->get('foo_service');
assert($obj1 !== $obj2);
// Test #3 - Retrieve non-shared service from child SM twice when peer SM is not checked first
$childManager = new ServiceManager(new Config());
$childManager->addPeeringServiceManager($mainManager);
$childManager->setRetrieveFromPeeringManagerFirst(false);
$obj1 = $childManager->get('foo_service');
$obj2 = $childManager->get('foo_service');
assert($obj1 !== $obj2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment