Changeset 22
- Timestamp:
- 08/28/06 18:28:35 (2 years ago)
- Files:
-
- trunk/XJConf/converters/AbstractObjectValueConverter.php (modified) (2 diffs)
- trunk/XJConf/converters/ConstructorValueConverter.php (modified) (3 diffs)
- trunk/XJConf/converters/FactoryMethodValueConverter.php (modified) (2 diffs)
- trunk/XJConf/converters/factories/ConstructorValueConverterFactory.php (modified) (1 diff)
- trunk/XJConf/converters/factories/FactoryMethodValueConverterFactory.php (modified) (1 diff)
- trunk/XJConf/converters/factories/ValueConverterFactoryChain.php (modified) (1 diff)
- trunk/XJConf/definitions/FactoryMethodDefinition.php (modified) (1 diff)
- trunk/XJConf/definitions/TagDefinition.php (modified) (2 diffs)
- trunk/examples/ColorPrimitives.php (modified) (1 diff)
- trunk/examples/TestPrimitivesFactory.php (added)
- trunk/examples/xml/defines-primitives-factory.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/XJConf/converters/AbstractObjectValueConverter.php
r20 r22 32 32 */ 33 33 protected function addAttributesToValue(Tag $tag, TagDefinition $def, $instance) { 34 $class = new ReflectionClass( $this->getType());34 $class = new ReflectionClass(get_class($instance)); 35 35 // set all attributes 36 36 foreach ($def->getAttributes() as $att) { … … 65 65 } 66 66 67 $class = new ReflectionClass( $this->getType());67 $class = new ReflectionClass(get_class($instance)); 68 68 foreach ($children as $child) { 69 69 if (in_array($child->getName(), $ignore) == true) { trunk/XJConf/converters/ConstructorValueConverter.php
r20 r22 8 8 XJConfLoader::load('converters.ValueConverter', 9 9 'converters.AbstractObjectValueConverter', 10 'definitions.ConstructorDefinition',11 10 'exceptions.ValueConversionException' 12 11 ); … … 42 41 } 43 42 44 $constructor = ConstructorValueConverter::getConstructorDefinition($def);43 $constructor = $def->getChildDefinition('ConstructorDefinition'); 45 44 $tmpParams = $constructor->getParams(); 46 45 $cParams = array(); … … 72 71 return $instance; 73 72 } 74 75 /**76 * Get the factory method of the definition, if available77 *78 * @param Definition $def79 * @return ConstructorDefinition80 */81 public static function getConstructorDefinition(Definition $def) {82 foreach ($def->getChildDefinitions() as $child) {83 if ($child instanceof ConstructorDefinition) {84 return $child;85 }86 }87 return null;88 }89 73 } 90 74 ?> trunk/XJConf/converters/FactoryMethodValueConverter.php
r14 r22 1 1 <?php 2 /** 3 * Value converter that uses a factory method to create an object. 4 * 5 * @author Stephan Schmidt <me@schst.net> 6 * @author Frank Kleine <frank.kleine@schlund.de> 7 */ 2 8 XJConfLoader::load('converters.ValueConverter', 9 'converters.AbstractObjectValueConverter', 3 10 'exceptions.ValueConversionException' 4 11 ); 5 12 /** 6 * Value converter , that uses a factory method13 * Value converter that uses a factory method to create an object. 7 14 * 8 * @author Stephan Schmidt <me@schst.net> 15 * @package XJConf 16 * @subpackage converters 9 17 */ 10 class FactoryMethodValueConverter implements ValueConverter18 class FactoryMethodValueConverter extends AbstractObjectValueConverter implements ValueConverter 11 19 { 12 /**13 * name of class to use as factory14 *15 * @var string16 */17 protected $className;18 19 20 /** 20 21 * name of method to use as factory method … … 45 46 public function convertValue(Tag $tag, Definition $def) 46 47 { 47 /*48 48 if (class_exists($this->className) == false) { 49 49 throw new ValueConversionException('Class "' . $this->className . '" does not exist.'); 50 50 } 51 51 52 $factoryMethod = $def->getChildDefinition('FactoryMethodDefinition'); 53 $tmpParams = $factoryMethod->getParams(); 54 $cParams = array(); 55 // get all values and their types 56 foreach ($tmpParams as $key => $conParam) { 57 $cParams[] = $conParam->convertValue($tag); 58 } 59 52 60 try { 53 $refClass = new ReflectionClass($this->className); 54 $instance = $refClass->getMethod($this->methodName)->invoke(null, $values); 61 $refClass = new ReflectionClass($this->className); 62 $refMethod = $refClass->getMethod($this->methodName); 63 if (method_exists($refMethod, 'invokeArgs') == true) { 64 $instance = $refMethod->invokeArgs(null, $cParams); 65 } elseif (count($cParams) == 1) { 66 $instance = $refMethod->invoke(null, $cParams[0]); 67 } else { 68 throw new ValueConversionException('Could not create instance of "' . $this->className . '" as Reflection does not support invokeArgs().'); 69 } 55 70 } catch (ReflectionException $re) { 56 71 throw new ValueConversionException('Could not create instance of "' . $this->className . '" using the factory method "' . $this->methodName . '": ' . $re->getMessage()); 57 72 } 73 74 // add attributes and child elements 75 if ($def instanceof TagDefinition) { 76 $this->addAttributesToValue($tag, $def, $instance); 77 } 78 $this->addChildrenToValue($tag, $def, $instance, $factoryMethod->getUsedChildrenNames()); 79 58 80 59 81 return $instance; 60 */61 82 } 62 63 /**64 * Get the factory method of the definition, if available65 *66 * @param Definition $def67 * @return FactoryMethodDefinition68 */69 public static function getFactoryMethodDefinition(Definition $def) {70 foreach ($def->getChildDefinitions() as $child) {71 if ($child instanceof FactoryMethodDefinition) {72 return $child;73 }74 }75 return null;76 }77 83 } 78 84 ?> trunk/XJConf/converters/factories/ConstructorValueConverterFactory.php
r14 r22 1 1 <?php 2 /** 3 * Factory to create a ConstructorValueConverter. 4 * 5 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 6 * @author Frank Kleine <frank.kleine@schlund.de> 7 */ 2 8 XJConfLoader::load('converters.factories.ValueConverterFactory', 3 9 'converters.ConstructorValueConverter'); 10 /** 11 * Factory to create an ConstructorValueConverter. 12 * 13 * @package XJConf 14 * @subpackage converters 15 */ 16 class ConstructorValueConverterFactory implements ValueConverterFactory 17 { 4 18 5 class ObjectValueConverterFactory implements ValueConverterFactory { 6 7 public function isResponsible(Definition $def) { 8 return class_exists($def->getType()); 19 /** 20 * Decides whether the ConstructorValueConverter is responsible for the given Definition. 21 * 22 * @param Definition $def 23 * @return boolean true if is responsible, else false 24 */ 25 public function isResponsible(Definition $def) 26 { 27 if (class_exists($def->getType()) == false) { 28 return false; 29 } 30 31 return $def->hasChildDefinition('ConstructorDefinition'); 9 32 } 10 11 public function createValueConverter(Definition $def) { 33 34 /** 35 * creates an instance of the ConstructorValueConverter 36 * 37 * @param Definition $def 38 * @return ConstructorValueConverter 39 */ 40 public function createValueConverter(Definition $def) 41 { 12 42 $converter = new ConstructorValueConverter($def->getType()); 13 43 return $converter; trunk/XJConf/converters/factories/FactoryMethodValueConverterFactory.php
r14 r22 1 1 <?php 2 XJConfLoader::load('converters.factories.ValueConverterFactory'); 3 4 class FactoryMethodValueConverterFactory implements ValueConverterFactory { 5 6 public function isResponsible(Definition $def) { 7 return ($this->getFactoryMethod($def) != null); 2 /** 3 * Factory to create a FactoryMethodValueConverter. 4 * 5 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 6 * @author Frank Kleine <frank.kleine@schlund.de> 7 */ 8 XJConfLoader::load('converters.factories.ValueConverterFactory', 9 'converters.FactoryMethodValueConverter' 10 ); 11 /** 12 * Factory to create an FactoryMethodValueConverter. 13 * 14 * @package XJConf 15 * @subpackage converters 16 */ 17 class FactoryMethodValueConverterFactory implements ValueConverterFactory 18 { 19 /** 20 * Decides whether the FactoryMethodValueConverter is responsible for the given Definition. 21 * 22 * @param Definition $def 23 * @return boolean true if is responsible, else false 24 */ 25 public function isResponsible(Definition $def) 26 { 27 if (class_exists($def->getType()) == false) { 28 return false; 29 } 30 31 return $def->hasChildDefinition('FactoryMethodDefinition'); 8 32 } 9 10 public function createValueConverter(Definition $def) { 11 $factoryMethod = $this->getFactoryMethod($def); 12 if ($factoryMethod == null) { 33 34 /** 35 * creates an instance of the FactoryMethodValueConverter 36 * 37 * @param Definition $def 38 * @return FactoryMethodValueConverter 39 */ 40 public function createValueConverter(Definition $def) 41 { 42 $factoryMethod = $def->getChildDefinition('FactoryMethodDefinition'); 43 if (null == $factoryMethod) { 13 44 return null; 14 45 } 46 15 47 $converter = new FactoryMethodValueConverter($def->getType(), $factoryMethod->getName()); 16 48 return $converter; trunk/XJConf/converters/factories/ValueConverterFactoryChain.php
r14 r22 59 59 ValueConverterFactoryChain::push(new PrimitiveValueConverterFactory()); 60 60 ValueConverterFactoryChain::push(new ArrayValueConverterFactory()); 61 ValueConverterFactoryChain::push(new ObjectValueConverterFactory());61 ValueConverterFactoryChain::push(new ConstructorValueConverterFactory()); 62 62 ValueConverterFactoryChain::push(new FactoryMethodValueConverterFactory()); 63 63 ?> trunk/XJConf/definitions/FactoryMethodDefinition.php
r21 r22 147 147 return $this->params; 148 148 } 149 150 /** 151 * Get the names of all child elements that are used in 152 * the constructor. 153 * 154 * These children are not used, when adding them using 155 * setter-methods. 156 * 157 * @return array 158 */ 159 public function getUsedChildrenNames() 160 { 161 $childrenNames = array(); 162 foreach ($this->params as $param) { 163 if ($param instanceof ChildDefinition) { 164 array_push($childrenNames, $param->getName()); 165 } 166 } 167 168 return $childrenNames; 169 } 149 170 150 171 /** trunk/XJConf/definitions/TagDefinition.php
r21 r22 163 163 // no constructor definition has been set, 164 164 // create a new one 165 if (null == $this->constructor ) {165 if (null == $this->constructor && null == $this->factoryMethod) { 166 166 $this->constructor = new ConstructorDefinition(); 167 167 try { … … 277 277 $children = $this->getChildDefinitions(); 278 278 foreach ($children as $child) { 279 if (get_class($ param) == $def) {280 return $ param;279 if (get_class($child) == $def) { 280 return $child; 281 281 } 282 282 } trunk/examples/ColorPrimitives.php
r2 r22 6 6 * Window - Preferences - Java - Code Style - Code Templates 7 7 */ 8 class ColorPrimitivesFactory 9 { 10 public static function create($name) 11 { 12 $colorPrimities = new ColorPrimitives($name); 13 return $colorPrimities; 14 } 15 } 16 8 17 class ColorPrimitives 9 18 {
