本文整理汇总了PHP中Zend\Di\Di::newInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP Di::newInstance方法的具体用法?PHP Di::newInstance怎么用?PHP Di::newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Di\Di
的用法示例。
在下文中一共展示了Di::newInstance方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _assembleEntities
/**
* Assemble entities
*
* @return void
*/
private function _assembleEntities()
{
$this->_im->addAlias('Chat', 'App\\Bot\\Model\\Chat');
$this->_im->addAlias('LastUpdate', 'App\\Bot\\Model\\LastUpdate');
$this->_im->addAlias('UserIssue', 'App\\Redmine\\Model\\UserIssue');
$this->_im->addAlias('UserKey', 'App\\Redmine\\Model\\UserKey');
$this->_im->setParameters('Chat', ['collection' => $this->_di->newInstance('App\\Model\\Entity\\Collection')]);
$this->_im->setParameters('LastUpdate', ['collection' => $this->_di->newInstance('App\\Model\\Entity\\Collection')]);
$this->_im->setParameters('UserKey', ['collection' => $this->_di->newInstance('App\\Model\\Entity\\Collection')]);
}
示例2: newInstance
/**
* {@inheritDoc}
*/
public function newInstance($name, array $params = array(), $isShared = true)
{
if (empty($params) && isset($this->compiledInstantiators[$name])) {
$cb = $this->compiledInstantiators[$name];
return $cb($this, $isShared);
}
return parent::newInstance($name, $params, $isShared);
}
示例3: 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;
}
}
示例4: doProxy
/**
* @param Di $locator
* @return mixed
*/
public function doProxy(Di $locator)
{
$options = $this->getOptions();
$result = null;
if ('get' === $options['method']) {
$result = $locator->get($options['name'], $options['params']);
} elseif ('newInstance' === $options['method']) {
$result = $locator->newInstance($options['name'], $options['params'], $options['isShared']);
} else {
throw new InvalidArgumentException("'{$options['method']}' is undefined method'");
}
return $result;
}
示例5: newInstance
/**
* {@inheritDoc}
* @return GeneratorInstance
*/
public function newInstance($name, array $params = array(), $isShared = true)
{
$instance = parent::newInstance($name, $params, $isShared);
if ($instance instanceof GeneratorInstance) {
/* @var $instance GeneratorInstance */
$instance->setShared($isShared);
// When a callback is used, we don't know instance the class name.
// That's why we assume $name as the instance alias
if (null === $instance->getName()) {
$instance->setAlias($name);
}
}
return $instance;
}
示例6: testDiWillInjectDefaultParameters
/**
* @group ZF2-142
*/
public function testDiWillInjectDefaultParameters()
{
$di = new Di();
$classDef = new Definition\ClassDefinition('ZendTest\\Di\\TestAsset\\ConstructorInjection\\OptionalParameters');
$classDef->addMethod('__construct', true);
$classDef->addMethodParameter('__construct', 'a', array('type' => false, 'required' => false, 'default' => null));
$classDef->addMethodParameter('__construct', 'b', array('type' => false, 'required' => false, 'default' => 'defaultConstruct'));
$classDef->addMethodParameter('__construct', 'c', array('type' => false, 'required' => false, 'default' => array()));
$di->definitions()->addDefinition($classDef, false);
$optionalParams = $di->newInstance('ZendTest\\Di\\TestAsset\\ConstructorInjection\\OptionalParameters');
$this->assertSame(null, $optionalParams->a);
$this->assertSame('defaultConstruct', $optionalParams->b);
$this->assertSame(array(), $optionalParams->c);
}
示例7: 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);
}
示例8: create
/**
* Create new object instance
*
* @param string $className
* @param array $arguments
* @param bool $isShared
* @return object
*/
public function create($className, array $arguments = array(), $isShared = true)
{
$object = $this->_di->newInstance($className, $arguments, $isShared);
return $object;
}
示例9: testNewInstanceThrowsExceptionWhenEnteringInMiddleOfCircularDependency
/**
* Test for Circular Dependencies (case 2)
*
* C->D, D->E, E->C
* @group CircularDependencyCheck
*/
public function testNewInstanceThrowsExceptionWhenEnteringInMiddleOfCircularDependency()
{
$di = new Di();
$this->setExpectedException(
'Zend\Di\Exception\CircularDependencyException',
'Circular dependency detected: ZendTest\Di\TestAsset\CircularClasses\C depends on ZendTest\Di\TestAsset\CircularClasses\D and viceversa'
);
$di->newInstance('ZendTest\Di\TestAsset\CircularClasses\D');
}