本文整理汇总了PHP中Zend\Di\Di::definitions方法的典型用法代码示例。如果您正苦于以下问题:PHP Di::definitions方法的具体用法?PHP Di::definitions怎么用?PHP Di::definitions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Di\Di
的用法示例。
在下文中一共展示了Di::definitions方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getInitialInstanceDefinitions
/**
* @return array
*/
public function getInitialInstanceDefinitions()
{
$im = $this->di->instanceManager();
$classes = $im->getClasses();
$definedClasses = $this->di->definitions()->getClasses();
$aliases = array_keys($im->getAliases());
return array_unique(array_merge($classes, $aliases, $definedClasses));
}
示例2: configureDefinition
public function configureDefinition(Di $di, $definition)
{
foreach ($definition as $definitionType => $definitionData) {
switch ($definitionType) {
case 'compiler':
// @todo
// @todo
case 'runtime':
// @todo
// @todo
case 'class':
foreach ($definitionData as $className => $classData) {
$classDefinitions = $di->definitions()->getDefinitionsByType('Zend\\Di\\Definition\\ClassDefinition');
foreach ($classDefinitions as $classDefinition) {
if (!$classDefinition->hasClass($className)) {
unset($classDefinition);
}
}
if (!isset($classDefinition)) {
$classDefinition = new Definition\ClassDefinition($className);
$di->definitions()->addDefinition($classDefinition, false);
}
foreach ($classData as $classDefKey => $classDefData) {
switch ($classDefKey) {
case 'instantiator':
$classDefinition->setInstantiator($classDefData);
break;
case 'supertypes':
$classDefinition->setSupertypes($classDefData);
break;
case 'methods':
case 'method':
foreach ($classDefData as $methodName => $methodInfo) {
if (isset($methodInfo['required'])) {
$classDefinition->addMethod($methodName, $methodInfo['required']);
unset($methodInfo['required']);
}
foreach ($methodInfo as $paramName => $paramInfo) {
$classDefinition->addMethodParameter($methodName, $paramName, $paramInfo);
}
}
break;
default:
$methodName = $classDefKey;
$methodInfo = $classDefData;
if (isset($classDefData['required'])) {
$classDefinition->addMethod($methodName, $methodInfo['required']);
unset($methodInfo['required']);
}
foreach ($methodInfo as $paramName => $paramInfo) {
$classDefinition->addMethodParameter($methodName, $paramName, $paramInfo);
}
}
}
}
}
}
}
示例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: testDiConstructorCanTakeDependencies
public function testDiConstructorCanTakeDependencies()
{
$dl = new DefinitionList(array());
$im = new InstanceManager();
$cg = new Configuration(array());
$di = new Di($dl, $im, $cg);
$this->assertSame($dl, $di->definitions());
$this->assertSame($im, $di->instanceManager());
$di->setDefinitionList($dl);
$di->setInstanceManager($im);
$this->assertSame($dl, $di->definitions());
$this->assertSame($im, $di->instanceManager());
}
示例5: testDependencyInjectorHasBuiltInImplementations
public function testDependencyInjectorHasBuiltInImplementations()
{
$di = new Di();
$this->assertInstanceOf('Zend\Di\InstanceManager', $di->instanceManager());
$definitions = $di->definitions();
$this->assertInstanceOf('Zend\Di\DefinitionList', $definitions);
$this->assertInstanceOf('Zend\Di\Definition\RuntimeDefinition', $definitions->top());
}
示例6: createDefinitions
public function createDefinitions()
{
$inspect = new Builder\PhpClass();
$inspect->setName('ZendTest\\Di\\TestAsset\\InspectedClass');
$inspectCtor = new Builder\InjectionMethod();
$inspectCtor->setName('__construct')->addParameter('foo', 'composed')->addParameter('baz', null);
$inspect->addInjectionMethod($inspectCtor);
$composed = new Builder\PhpClass();
$composed->setName('ZendTest\\Di\\TestAsset\\ComposedClass');
$struct = new Builder\PhpClass();
$struct->setName('ZendTest\\Di\\TestAsset\\Struct');
$structCtor = new Builder\InjectionMethod();
$structCtor->setName('__construct')->addParameter('param1', null)->addParameter('param2', 'inspect');
$definition = new Definition();
$definition->addClass($inspect)->addClass($composed)->addClass($struct);
$this->di->definitions()->unshift($definition);
$data = array('instance' => array('alias' => array('composed' => 'ZendTest\\Di\\TestAsset\\ComposedClass', 'inspect' => 'ZendTest\\Di\\TestAsset\\InspectedClass', 'struct' => 'ZendTest\\Di\\TestAsset\\Struct'), 'preferences' => array('composed' => array('composed'), 'inspect' => array('inspect'), 'struct' => array('struct')), 'ZendTest\\Di\\TestAsset\\InspectedClass' => array('parameters' => array('baz' => 'BAZ')), 'ZendTest\\Di\\TestAsset\\Struct' => array('parameters' => array('param1' => 'foo'))));
$configuration = new Configuration($data);
$configuration->configure($this->di);
}
示例7: testConfigurationCanConfigureCompiledDefinition
public function testConfigurationCanConfigureCompiledDefinition()
{
$config = ConfigFactory::fromFile(__DIR__ . '/_files/sample.php', true);
$config = new Configuration($config->di);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\\Di\\Definition\\ArrayDefinition');
$this->assertInstanceOf('Zend\\Di\\Definition\\ArrayDefinition', $definition);
$this->assertTrue($di->definitions()->hasClass('My\\DbAdapter'));
$this->assertTrue($di->definitions()->hasClass('My\\EntityA'));
$this->assertTrue($di->definitions()->hasClass('My\\Mapper'));
$this->assertTrue($di->definitions()->hasClass('My\\RepositoryA'));
$this->assertTrue($di->definitions()->hasClass('My\\RepositoryB'));
$this->assertFalse($di->definitions()->hasClass('My\\Foo'));
}
示例8: 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;
}
}
}
示例9: renderClassDefinition
protected function renderClassDefinition($class)
{
$definitions = $this->di->definitions();
echo PHP_EOL . ' Parameters For Class: ' . $class . PHP_EOL;
foreach ($definitions->getMethods($class) as $methodName => $methodIsRequired) {
foreach ($definitions->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;
}
示例10: 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);
}
示例11: __construct
/**
* @param Di $di
*/
public function __construct(Di $di)
{
$this->di = $di;
$this->definitions = $di->definitions();
$this->instanceManager = $di->instanceManager();
}
示例12: configureDefinition
/**
* @param Di $di
* @param array $definition
*/
public function configureDefinition(Di $di, $definition)
{
foreach ($definition as $definitionType => $definitionData) {
switch ($definitionType) {
case 'compiler':
foreach ($definitionData as $filename) {
if (is_readable($filename)) {
$di->definitions()->addDefinition(new ArrayDefinition(include $filename), false);
}
}
break;
case 'runtime':
if (isset($definitionData['enabled']) && !$definitionData['enabled']) {
// Remove runtime from definition list if not enabled
$definitions = array();
foreach ($di->definitions() as $definition) {
if (!$definition instanceof RuntimeDefinition) {
$definitions[] = $definition;
}
}
$definitionList = new DefinitionList($definitions);
$di->setDefinitionList($definitionList);
} elseif (isset($definitionData['use_annotations']) && $definitionData['use_annotations']) {
/* @var $runtimeDefinition Definition\RuntimeDefinition */
$runtimeDefinition = $di->definitions()->getDefinitionByType('\\Zend\\Di\\Definition\\RuntimeDefinition');
$runtimeDefinition->getIntrospectionStrategy()->setUseAnnotations(true);
}
break;
case 'class':
foreach ($definitionData as $className => $classData) {
$classDefinitions = $di->definitions()->getDefinitionsByType('Zend\\Di\\Definition\\ClassDefinition');
foreach ($classDefinitions as $classDefinition) {
if (!$classDefinition->hasClass($className)) {
unset($classDefinition);
}
}
if (!isset($classDefinition)) {
$classDefinition = new Definition\ClassDefinition($className);
$di->definitions()->addDefinition($classDefinition, false);
}
foreach ($classData as $classDefKey => $classDefData) {
switch ($classDefKey) {
case 'instantiator':
$classDefinition->setInstantiator($classDefData);
break;
case 'supertypes':
$classDefinition->setSupertypes($classDefData);
break;
case 'methods':
case 'method':
foreach ($classDefData as $methodName => $methodInfo) {
if (isset($methodInfo['required'])) {
$classDefinition->addMethod($methodName, $methodInfo['required']);
unset($methodInfo['required']);
}
foreach ($methodInfo as $paramName => $paramInfo) {
$classDefinition->addMethodParameter($methodName, $paramName, $paramInfo);
}
}
break;
default:
$methodName = $classDefKey;
$methodInfo = $classDefData;
if (isset($classDefData['required'])) {
$classDefinition->addMethod($methodName, $methodInfo['required']);
unset($methodInfo['required']);
}
foreach ($methodInfo as $paramName => $paramInfo) {
$classDefinition->addMethodParameter($methodName, $paramName, $paramInfo);
}
}
}
}
}
}
}
示例13: testInjectionForSetterInjectionWillConsultSupertypeDefinitionInClassDefinition
/**
* @group SetterInjection
* @group SupertypeResolution
*/
public function testInjectionForSetterInjectionWillConsultSupertypeDefinitionInClassDefinition()
{
$di = new Di();
// for setter injection, the dependency is not required, thus it must be forced
$classDef = new Definition\ClassDefinition('ZendTest\\Di\\TestAsset\\SetterInjection\\B');
$classDef->addMethod('setA', true);
$di->definitions()->addDefinition($classDef, false);
// top of stack b/c Runtime is already there
$c = $di->get('ZendTest\\Di\\TestAsset\\SetterInjection\\C');
$this->assertInstanceOf('ZendTest\\Di\\TestAsset\\SetterInjection\\C', $c);
$this->assertInstanceOf('ZendTest\\Di\\TestAsset\\SetterInjection\\A', $c->a);
}
示例14: testDiWillInjectClassNameAsStringAtCallTime
/**
* @group ZF2-260
*/
public function testDiWillInjectClassNameAsStringAtCallTime()
{
$di = new Di();
$classDef = new Definition\ClassDefinition('ZendTest\\Di\\TestAsset\\SetterInjection\\D');
$classDef->addMethod('setA', true);
$classDef->addMethodParameter('setA', 'a', array('type' => false, 'required' => true));
$di->definitions()->addDefinition($classDef, false);
$d = $di->get('ZendTest\\Di\\TestAsset\\SetterInjection\\D', array('a' => 'ZendTest\\Di\\TestAsset\\SetterInjection\\A'));
$this->assertSame($d->a, 'ZendTest\\Di\\TestAsset\\SetterInjection\\A');
}
示例15: testDiWillInjectDependenciesForAlias
public function testDiWillInjectDependenciesForAlias()
{
$di = new Di;
// for setter injection, the dependency is not required, thus it must be forced
$classDef = new Definition\ClassDefinition('ZendTest\Di\TestAsset\SetterInjection\B');
$classDef->addMethod('setA', false);
$classDef->addMethodParameter('setA', 'a', array('type' => 'ZendTest\Di\TestAsset\SetterInjection\A', 'required' => false));
$di->definitions()->addDefinition($classDef, false);
$di->instanceManager()->addAlias('b_alias', 'ZendTest\Di\TestAsset\SetterInjection\B');
$di->instanceManager()->setInjections('b_alias', array('ZendTest\Di\TestAsset\SetterInjection\A'));
$b = $di->get('b_alias');
$this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b->a);
}