本文整理汇总了PHP中Zend\Di\Di::instanceManager方法的典型用法代码示例。如果您正苦于以下问题:PHP Di::instanceManager方法的具体用法?PHP Di::instanceManager怎么用?PHP Di::instanceManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Di\Di
的用法示例。
在下文中一共展示了Di::instanceManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: configureDi
public function configureDi(Di $di)
{
$di->instanceManager()->addSharedInstance($this->configurationReader, get_class($this->configurationReader));
$di->instanceManager()->addSharedInstance($this->classConfigurationReader, get_class($this->classConfigurationReader));
$di->instanceManager()->addSharedInstance($this->environmentConfigurationReader, get_class($this->environmentConfigurationReader));
$di->instanceManager()->addSharedInstance($this, get_class($this));
}
示例2: init
/**
* Set up dependency injection framework
*
* @return ServiceManager
*/
public function init()
{
$options = $this->getOptions();
$doctrineContainer = $this->getBootstrap()->bootstrap('Doctrine')->getResource('Doctrine');
$di = new ZendDi\Di(null, null, new ZendDi\Config($options));
$di->instanceManager()->addSharedInstance(new Db\Adapter\Driver\Pdo\Pdo($doctrineContainer->getEntityManager()->getConnection()->getWrappedConnection()), 'Zend\\Db\\Adapter\\Driver\\Pdo\\Pdo');
$di->instanceManager()->addSharedInstance($doctrineContainer->getEntityManager(), 'Doctrine\\ORM\\EntityManager');
return $di;
}
示例3: render
public function render()
{
$knownClasses = array();
echo 'Definitions' . PHP_EOL . PHP_EOL;
foreach ($this->di->definitions() as $definition) {
$this->renderDefinition($definition);
foreach ($definition->getClasses() as $class) {
$knownClasses[] = $class;
$this->renderClassDefinition($definition, $class);
}
if (count($definition->getClasses()) == 0) {
echo PHP_EOL . ' No Classes Found' . PHP_EOL . PHP_EOL;
}
}
if ($this->runtimeClasses) {
echo ' Runtime classes:' . PHP_EOL;
}
$unknownRuntimeClasses = array_diff($this->runtimeClasses, $knownClasses);
foreach ($unknownRuntimeClasses as $runtimeClass) {
$definition = $this->di->definitions()->getDefinitionForClass($runtimeClass);
$this->renderClassDefinition($definition, $runtimeClass);
}
echo PHP_EOL . 'Instance Configuration Info:' . PHP_EOL;
echo PHP_EOL . ' Aliases:' . PHP_EOL;
$configuredTypes = array();
foreach ($this->di->instanceManager()->getAliases() as $alias => $class) {
echo ' ' . $alias . ' [type: ' . $class . ']' . PHP_EOL;
$configuredTypes[] = $alias;
}
echo PHP_EOL . ' Classes:' . PHP_EOL;
foreach ($this->di->instanceManager()->getClasses() as $class) {
echo ' ' . $class . PHP_EOL;
$configuredTypes[] = $class;
}
echo PHP_EOL . ' Configurations:' . PHP_EOL;
foreach ($configuredTypes as $type) {
$info = $this->di->instanceManager()->getConfiguration($type);
echo ' ' . $type . PHP_EOL;
if ($info['parameters']) {
echo ' parameters:' . PHP_EOL;
foreach ($info['parameters'] as $param => $value) {
echo ' ' . $param . ' = ' . $value . PHP_EOL;
}
}
if ($info['injections']) {
echo ' injections:' . PHP_EOL;
foreach ($info['injections'] as $injection => $value) {
var_dump($injection, $value);
}
}
}
}
示例4: __construct
/**
* @param string $definitionsFile
* @param Zend\Di\Di $diInstance
*/
public function __construct($definitionsFile = null, Zend\Di\Di $diInstance = null)
{
Magento_Profiler::start('di');
if (is_file($definitionsFile) && is_readable($definitionsFile)) {
$definition = new Magento_Di_Definition_ArrayDefinition_Zend(unserialize(file_get_contents($definitionsFile)));
} else {
$definition = new Magento_Di_Definition_RuntimeDefinition_Zend();
}
$this->_di = $diInstance ? $diInstance : new Magento_Di();
$this->_di->setDefinitionList(new Magento_Di_DefinitionList_Zend($definition));
$this->_di->instanceManager()->addSharedInstance($this, 'Magento_ObjectManager');
Magento_Profiler::stop('di');
}
示例5: render
public function render(Di $di)
{
echo 'Definitions: ' . PHP_EOL;
foreach ($di->definitions() as $definition) {
foreach ($definition->getClasses() as $class) {
echo PHP_EOL . ' Parameters For Class: ' . $class . PHP_EOL;
foreach ($definition->getMethods($class) as $methodName => $methodIsRequired) {
foreach ($definition->getMethodParameters($class, $methodName) as $fqName => $pData) {
echo ' ' . $pData[0] . ' [type: ';
echo ($pData[1]) ? $pData[1] : 'scalar';
echo ($pData[2] === true && $methodIsRequired) ? ', required' : ', not required';
echo ', injection-method: ' . $methodName;
echo ' fq-name: ' . $fqName;
echo ']' . PHP_EOL;
}
}
}
}
echo PHP_EOL . 'Instance Configuration Info:' . PHP_EOL;
echo PHP_EOL . ' Aliases:' . PHP_EOL;
$configuredTypes = array();
foreach ($di->instanceManager()->getAliases() as $alias => $class) {
echo ' ' . $alias . ' [type: ' . $class . ']' . PHP_EOL;
$configuredTypes[] = $alias;
}
echo PHP_EOL . ' Classes:' . PHP_EOL;
foreach ($di->instanceManager()->getClasses() as $class) {
echo ' ' . $class . PHP_EOL;
$configuredTypes[] = $class;
}
echo PHP_EOL . ' Configurations:' . PHP_EOL;
foreach ($configuredTypes as $type) {
$info = $di->instanceManager()->getConfiguration($type);
echo ' ' . $type . PHP_EOL;
foreach ($info['parameters'] as $param => $value) {
echo ' ' . $param . ' = ' . $value . PHP_EOL;
}
}
}
示例6: setUp
public function setUp()
{
$this->mock = $this->getMock('Tillikum\\Billing\\Event\\Strategy\\StrategyInterface');
$di = new Di\Di();
$im = $di->instanceManager();
$im->addSharedInstance($this->mock, 'TestStrategyAlias');
$this->processor = new BookingProcessor($di);
$rule = new FacilityBookingRule();
$config = new FacilityBookingRuleConfig();
$config->amount = 10.0;
$config->currency = 'USD';
$config->start = new DateTime('2010-07-01');
$config->end = new DateTime('2010-07-15');
$config->strategy = 'TestStrategyAlias';
$rule->configs->add($config);
$config = new FacilityBookingRuleConfig();
$config->amount = 20.0;
$config->currency = 'USD';
$config->start = new DateTime('2010-07-15');
$config->end = new DateTime('2010-07-31');
$config->strategy = 'TestStrategyAlias';
$rule->configs->add($config);
$this->event = new FacilityBookingEvent();
$this->event->start = new DateTime('2010-07-01');
$this->event->end = new DateTime('2010-07-15');
$this->event->rule = $rule;
}
示例7: testConfigurationCanConfigureInstanceManagerWithIniFile
public function testConfigurationCanConfigureInstanceManagerWithIniFile()
{
$ini = new \Zend\Config\Ini(__DIR__ . '/_files/sample.ini', 'section-a');
$config = new Configuration($ini->di);
$di = new Di();
$di->configure($config);
$im = $di->instanceManager();
$this->assertTrue($im->hasAlias('my-repository'));
$this->assertEquals('My\RepositoryA', $im->getClassFromAlias('my-repository'));
$this->assertTrue($im->hasAlias('my-mapper'));
$this->assertEquals('My\Mapper', $im->getClassFromAlias('my-mapper'));
$this->assertTrue($im->hasAlias('my-dbAdapter'));
$this->assertEquals('My\DbAdapter', $im->getClassFromAlias('my-dbAdapter'));
$this->assertTrue($im->hasTypePreferences('my-repository'));
$this->assertContains('my-mapper', $im->getTypePreferences('my-repository'));
$this->assertTrue($im->hasTypePreferences('my-mapper'));
$this->assertContains('my-dbAdapter', $im->getTypePreferences('my-mapper'));
$this->assertTrue($im->hasConfiguration('My\DbAdapter'));
$expected = array('parameters' => array('username' => 'readonly', 'password' => 'mypassword'), 'injections' => array());
$this->assertEquals($expected, $im->getConfiguration('My\DbAdapter'));
$this->assertTrue($im->hasConfiguration('my-dbAdapter'));
$expected = array('parameters' => array('username' => 'readwrite'), 'injections' => array());
$this->assertEquals($expected, $im->getConfiguration('my-dbAdapter'));
}
示例8: createService
public function createService(ServiceLocatorInterface $oServiceLocator)
{
$oServiceLocator = $oServiceLocator->getServiceLocator();
$oAppService = $oServiceLocator->get('AppService');
$oDi = new Di();
$oDi->instanceManager()->setParameters('API\\Controller\\IndexController', array('app' => $oAppService));
return $oDi->get('API\\Controller\\IndexController');
}
示例9: testWillDiscoverInjectedMethodParameters
public function testWillDiscoverInjectedMethodParameters()
{
$di = new Di();
$a = new A();
$di->instanceManager()->setParameters('ZendTest\\Di\\TestAsset\\SetterInjection\\B', array('a' => $a));
$proxy = new DependencyInjectorProxy($di);
$b = $proxy->get('ZendTest\\Di\\TestAsset\\SetterInjection\\B');
$methods = $b->getMethods();
$this->assertSame('setA', $methods[0]['method']);
$this->assertSame($a, $methods[0]['params'][0]);
}
示例10: newInstance
/**
* Initialize and register the class via Dependency Injection
*
* @param string $alias
* @param string $class
* @param array $parameters
*/
public function newInstance($alias, $class, array $parameters = [])
{
try {
$this->checkExists($alias);
if (!empty($parameters)) {
static::$di->instanceManager()->setParameters($class, $parameters);
}
$this->services[$alias] = static::$di->newInstance($class);
} catch (\InvalidArgumentException $e) {
throw $e;
}
}
示例11: getClasses
/**
* Retrieves all classes that are involved in generating an instance for $name and its dependencies
*
* @param array|string $name
* @return string[]
*/
public function getClasses($name)
{
$classes = array();
$instanceDefinitions = $this->getInjectedDefinitions($name);
foreach ($instanceDefinitions as $instanceDefinition) {
if ($instanceDefinition->getClass()) {
$classes[$instanceDefinition->getClass()] = true;
continue;
}
$alias = $this->di->instanceManager()->getClassFromAlias($instanceDefinition->getName());
$classes[$alias] = true;
}
return array_keys($classes);
}
示例12: switchThemeConfiguration
public function switchThemeConfiguration($fullyQualifiedClassName)
{
$reflection = new \ReflectionClass($fullyQualifiedClassName);
if ($reflection->implementsInterface('Magium\\Themes\\ThemeConfigurationInterface')) {
$this->baseThemeClass = $fullyQualifiedClassName;
$this->di->instanceManager()->unsetTypePreferences('Magium\\Themes\\ThemeConfigurationInterface');
$this->di->instanceManager()->setTypePreference('Magium\\Themes\\ThemeConfigurationInterface', [$fullyQualifiedClassName]);
if ($reflection->implementsInterface('Magium\\Themes\\BaseThemeInterface')) {
$this->di->instanceManager()->unsetTypePreferences('Magium\\Themes\\BaseThemeInterface');
$this->di->instanceManager()->setTypePreference('Magium\\Themes\\BaseThemeInterface', [$fullyQualifiedClassName]);
}
$theme = $this->getTheme();
if ($theme instanceof BaseThemeInterface) {
$theme->configure($this);
}
} else {
throw new InvalidConfigurationException('The theme configuration implement Magium\\Themes\\ThemeConfigurationInterface');
}
$this->getLogger()->addCharacteristic(Logger::CHARACTERISTIC_THEME, $fullyQualifiedClassName);
}
示例13: configureInstance
/**
* Configures a given Di instance
*
* @param Di $di
* @param $instanceData
*/
public function configureInstance(Di $di, $instanceData)
{
$im = $di->instanceManager();
foreach ($instanceData as $target => $data) {
switch (strtolower($target)) {
case 'aliases':
case 'alias':
foreach ($data as $n => $v) {
$im->addAlias($n, $v);
}
break;
case 'preferences':
case 'preference':
foreach ($data as $n => $v) {
if (is_array($v)) {
foreach ($v as $v2) {
$im->addTypePreference($n, $v2);
}
} else {
$im->addTypePreference($n, $v);
}
}
break;
default:
foreach ($data as $n => $v) {
switch ($n) {
case 'parameters':
case 'parameter':
$im->setParameters($target, $v);
break;
case 'injections':
case 'injection':
$im->setInjections($target, $v);
break;
case 'shared':
case 'share':
$im->setShared($target, $v);
break;
}
}
}
}
}
示例14: testNewInstanceWillUsePreferredClassForInterfaceHints
/**
* Fix for PHP bug in is_subclass_of
*
* @see https://bugs.php.net/bug.php?id=53727
*/
public function testNewInstanceWillUsePreferredClassForInterfaceHints()
{
$di = new Di();
$di->instanceManager()->addTypePreference(
'ZendTest\Di\TestAsset\PreferredImplClasses\A',
'ZendTest\Di\TestAsset\PreferredImplClasses\BofA'
);
$c = $di->get('ZendTest\Di\TestAsset\PreferredImplClasses\C');
$a = $c->a;
$this->assertInstanceOf('ZendTest\Di\TestAsset\PreferredImplClasses\BofA', $a);
$d = $di->get('ZendTest\Di\TestAsset\PreferredImplClasses\D');
$this->assertSame($a, $d->a);
}
示例15: testInjectionCanHandleMultipleInjectionsWithMultipleArguments
public function testInjectionCanHandleMultipleInjectionsWithMultipleArguments()
{
$definitionList = new DefinitionList(array($classdef = new Definition\ClassDefinition('ZendTest\\Di\\TestAsset\\InjectionClasses\\A'), new Definition\RuntimeDefinition()));
$classdef->addMethod('injectSplitDependency');
$classdef->addMethodParameter('injectSplitDependency', 'b', array('required' => true, 'type' => 'ZendTest\\Di\\TestAsset\\InjectionClasses\\B'));
$classdef->addMethodParameter('injectSplitDependency', 'somestring', array('required' => true, 'type' => null));
/**
* First test that this works with a single call
*/
$di = new Di($definitionList);
$di->instanceManager()->setInjections('ZendTest\\Di\\TestAsset\\InjectionClasses\\A', array('injectSplitDependency' => array('b' => 'ZendTest\\Di\\TestAsset\\InjectionClasses\\B', 'somestring' => 'bs-id')));
$a = $di->newInstance('ZendTest\\Di\\TestAsset\\InjectionClasses\\A');
$this->assertInstanceOf('ZendTest\\Di\\TestAsset\\InjectionClasses\\B', $a->bs[0]);
$this->assertEquals('bs-id', $a->bs[0]->id);
/**
* Next test that this works with multiple calls
*/
$di = new Di($definitionList);
$di->instanceManager()->setInjections('ZendTest\\Di\\TestAsset\\InjectionClasses\\A', array('injectSplitDependency' => array(array('b' => 'ZendTest\\Di\\TestAsset\\InjectionClasses\\B', 'somestring' => 'bs-id'), array('b' => 'ZendTest\\Di\\TestAsset\\InjectionClasses\\C', 'somestring' => 'bs-id-for-c'))));
$a = $di->newInstance('ZendTest\\Di\\TestAsset\\InjectionClasses\\A');
$this->assertInstanceOf('ZendTest\\Di\\TestAsset\\InjectionClasses\\B', $a->bs[0]);
$this->assertEquals('bs-id', $a->bs[0]->id);
$this->assertInstanceOf('ZendTest\\Di\\TestAsset\\InjectionClasses\\C', $a->bs[1]);
$this->assertEquals('bs-id-for-c', $a->bs[1]->id);
}