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

Commit

Permalink
Merge branch 'feature/di'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Schindler committed Jul 13, 2011
7 parents 0967316 + 37f548b + 0df2db0 + e43587e + 5784352 + 4a62d6b + 6fb196a commit 47a731a
Show file tree
Hide file tree
Showing 15 changed files with 755 additions and 288 deletions.
54 changes: 34 additions & 20 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public function __construct($data)
$data = iterator_to_array($data, true);
}
} elseif (!is_array($data)) {
throw new Exception\InvalidArgumentException('Configuration data must be of type Zend\Config\Config or an array');
throw new Exception\InvalidArgumentException(
'Configuration data must be of type Zend\Config\Config or an array'
);
}
$this->data = $data;
}
Expand Down Expand Up @@ -53,7 +55,10 @@ public function configureDefinitions(DependencyInjector $di, $definitionsData)
{
if ($di->hasDefinition()) {
if (!$di->getDefinition() instanceof Definition\AggregateDefinition) {
throw new Exception\InvalidArgumentException('In order to configure multiple definitions, the primary definition must not be set, or must be of type AggregateDefintion');
throw new Exception\InvalidArgumentException(
'In order to configure multiple definitions, the primary definition must not be set, '
. 'or must be of type AggregateDefintion'
);
}
} else {
$di->setDefinition($di->createDefinition('Zend\Di\Definition\AggregateDefinition'));
Expand All @@ -69,7 +74,10 @@ public function configureDefinition(DependencyInjector $di, $definitionData)
if ($di->hasDefinition()) {
$aggregateDef = $di->getDefinition();
if (!$aggregateDef instanceof Definition\AggregateDefinition) {
throw new Exception\InvalidArgumentException('In order to configure multiple definitions, the primary definition must not be set, or must be of type AggregateDefintion');
throw new Exception\InvalidArgumentException(
'In order to configure multiple definitions, the primary definition must not be set, '
. 'or must be of type AggregateDefintion'
);
}
} /* else {
$aggregateDef = $di->createDefinition('Zend\Di\Definition\AggregateDefinition');
Expand Down Expand Up @@ -101,32 +109,38 @@ public function configureInstance(DependencyInjector $di, $instanceData)
switch (strtolower($target)) {
case 'aliases':
case 'alias':
foreach ($data as $aliasName => $className) {
$im->addAlias($aliasName, $className);
}
break;
case 'properties':
case 'property':
foreach ($data as $classOrAlias => $properties) {
foreach ($properties as $propName => $propValue) {
$im->setProperty($classOrAlias, $propName, $propValue);
}
foreach ($data as $n => $v) {
$im->addAlias($n, $v);
}
break;
case 'preferences':
case 'preferredinstances':
case 'preferredinstance':
foreach ($data as $classOrAlias => $preferredValueOrValues) {
if (is_array($preferredValueOrValues)) {
foreach ($preferredValueOrValues as $preferredValue) {
$im->addPreferredInstance($classOrAlias, $preferredValue);
case 'preference':
foreach ($data as $n => $v) {
if (is_array($v)) {
foreach ($v as $v2) {
$im->addTypePreference($n, $v2);
}
} else {
$im->addPreferredInstance($classOrAlias, $preferredValueOrValues);
$im->addTypePreference($n, $v);
}
}
break;
default:
foreach ($data as $n => $v) {
switch ($n) {
case 'parameters':
case 'parameter':
$im->setParameters($target, $v);
break;
case 'methods':
case 'method':
$im->setMethods($target, $v);
break;
}
}
}
}

}

}
3 changes: 3 additions & 0 deletions src/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

interface Definition
{
const PARAMETER_REQUIRED = 0x00;
const PARAMETER_OPTIONAL = 0x01;

public function getClasses();
public function hasClass($class);
public function getClassSupertypes($class);
Expand Down
21 changes: 21 additions & 0 deletions src/Definition/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@

class Compiler
{
protected $introspectionRuleset = null;
protected $codeScanners = array();
protected $codeReflectors = array();

public function setIntrospectionRuleset(IntrospectionRuleset $introspectionRuleset)
{
$this->introspectionRuleset = $introspectionRuleset;
}

/**
*
* @return Zend\Di\Definition\IntrospectionRuleset
*/
public function getIntrospectionRuleset()
{
if ($this->introspectionRuleset == null) {
$this->introspectionRuleset = new IntrospectionRuleset();
}
return $this->introspectionRuleset;
}

public function addCodeScannerDirectory(DirectoryScanner $scannerDirectory)
{
$this->codeScanners[] = $scannerDirectory;
Expand Down Expand Up @@ -80,10 +98,13 @@ public function compileScannerInjectionMethods(ClassScanner $scannerClass)
{
$data = array();
$className = $scannerClass->getName();
//$strategy = $this->getStrategy();
foreach ($scannerClass->getMethods(true) as $scannerMethod) {
$methodName = $scannerMethod->getName();

// determine initiator & constructor dependencies
//$constructorRules = $strategy->getConstructorRules();
//if ($constructorRules[''])
if ($methodName === '__construct' && $scannerMethod->isPublic()) {
$params = $scannerMethod->getParameters(true);
if ($params) {
Expand Down
118 changes: 118 additions & 0 deletions src/Definition/IntrospectionRuleset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Zend\Di\Definition;

class IntrospectionRuleset
{
const TYPE_CONSTRUCTOR = 'constructor';
const TYPE_SETTER = 'setter';
const TYPE_INTERFACE = 'interface';

protected $construtorRules = array(
'enabled' => true,
'includedClasses' => array(),
'excludedClasses' => array(),
);

protected $setterRules = array(
'enabled' => true,
'pattern' => '^set[A-Z]{1}\w*',
'includedClasses' => array(),
'excludedClasses' => array('ArrayObject'),
'methodMaximumParams' => 1,
'paramCanBeOptional' => true,
);

protected $interfaceRules = array(
'enabled' => true,
'pattern' => '\w*Aware\w*',
'includedInterfaces' => array(),
'excludedInterfaces' => array()
);

public function __construct($config = null)
{

}

public function addRule($strategy, $name, $value)
{
switch ($strategy) {
case self::TYPE_CONSTRUCTOR:
$rule = &$this->construtorRules;
break;
case self::TYPE_SETTER:
$rule = &$this->setterRules;
break;
case self::TYPE_INTERFACE:
$rule = &$this->interfaceRules;
break;
}

if (!isset($rule[$name])) {
throw new \InvalidArgumentException('The rule name provided is not a valid rule name.');
}

switch (gettype($rule[$name])) {
case 'array':
array_push($rule[$name], $value);
break;
case 'bool':
$rule[$name] = (bool) $value;
break;
case 'string':
$rule[$name] = (string) $value;
break;
}

return $this;
}

public function getRules($ruleType)
{
if (!$ruleType) {
return array(
self::TYPE_CONSTRUCTOR => $this->construtorRules,
self::TYPE_SETTER => $this->setterRules,
self::TYPE_INTERFACE => $this->interfaceRules
);
} else {
switch ($ruleType) {
case self::TYPE_CONSTRUCTOR: return $this->construtorRules;
case self::TYPE_SETTER: return $this->setterRules;
case self::TYPE_INTERFACE: return $this->interfaceRules;
}
}
}

public function addConstructorRule($name, $value)
{
$this->addRule(self::TYPE_CONSTRUCTOR, $name, $value);
}

public function getConstructorRules()
{
return $this->construtorRules;
}

public function addSetterRule($name, $value)
{
$this->addRule(self::TYPE_SETTER, $name, $value);
}

public function getSetterRules()
{
return $this->setterRules;
}

public function addInterfaceRule($name, $value)
{
$this->addRule(self::TYPE_INTERFACE, $name, $value);
}

public function getInterfaceRules()
{
return $this->interfaceRules;
}

}

0 comments on commit 47a731a

Please sign in to comment.