Changeset 22

Show
Ignore:
Timestamp:
08/28/06 18:28:35 (2 years ago)
Author:
mikey
Message:

finished first working version of FactoryMethodValueConverter?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/XJConf/converters/AbstractObjectValueConverter.php

    r20 r22  
    3232     */ 
    3333    protected function addAttributesToValue(Tag $tag, TagDefinition $def, $instance) { 
    34         $class = new ReflectionClass($this->getType()); 
     34        $class = new ReflectionClass(get_class($instance)); 
    3535        // set all attributes 
    3636        foreach ($def->getAttributes() as $att) { 
     
    6565        } 
    6666 
    67         $class = new ReflectionClass($this->getType()); 
     67        $class = new ReflectionClass(get_class($instance)); 
    6868        foreach ($children as $child) { 
    6969            if (in_array($child->getName(), $ignore) == true) { 
  • trunk/XJConf/converters/ConstructorValueConverter.php

    r20 r22  
    88XJConfLoader::load('converters.ValueConverter', 
    99                   'converters.AbstractObjectValueConverter', 
    10                    'definitions.ConstructorDefinition', 
    1110                   'exceptions.ValueConversionException' 
    1211); 
     
    4241            } 
    4342 
    44             $constructor = ConstructorValueConverter::getConstructorDefinition($def); 
     43            $constructor = $def->getChildDefinition('ConstructorDefinition'); 
    4544            $tmpParams   = $constructor->getParams(); 
    4645        $cParams     = array(); 
     
    7271        return $instance; 
    7372    } 
    74  
    75     /** 
    76      * Get the factory method of the definition, if available 
    77      * 
    78      * @param Definition $def 
    79      * @return ConstructorDefinition 
    80      */ 
    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     } 
    8973} 
    9074?> 
  • trunk/XJConf/converters/FactoryMethodValueConverter.php

    r14 r22  
    11<?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 */ 
    28XJConfLoader::load('converters.ValueConverter', 
     9                   'converters.AbstractObjectValueConverter', 
    310                   'exceptions.ValueConversionException' 
    411); 
    512/** 
    6  * Value converter, that uses a factory method 
     13 * Value converter that uses a factory method to create an object. 
    714 * 
    8  * @author Stephan Schmidt <me@schst.net> 
     15 * @package     XJConf 
     16 * @subpackage  converters 
    917 */ 
    10 class FactoryMethodValueConverter implements ValueConverter 
     18class FactoryMethodValueConverter extends AbstractObjectValueConverter implements ValueConverter 
    1119{ 
    12     /** 
    13      * name of class to use as factory 
    14      * 
    15      * @var  string 
    16      */ 
    17         protected $className; 
    18  
    1920        /** 
    2021         * name of method to use as factory method 
     
    4546        public function convertValue(Tag $tag, Definition $def) 
    4647        { 
    47             /* 
    4848            if (class_exists($this->className) == false) { 
    4949                throw new ValueConversionException('Class "' . $this->className . '" does not exist.'); 
    5050            } 
    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         
    5260        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                } 
    5570        } catch (ReflectionException $re) { 
    5671                throw new ValueConversionException('Could not create instance of "' . $this->className . '" using the factory method "' . $this->methodName . '": ' . $re->getMessage()); 
    5772        } 
     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 
    5880 
    5981        return $instance; 
    60         */ 
    6182        } 
    62  
    63     /** 
    64      * Get the factory method of the definition, if available 
    65      * 
    66      * @param Definition $def 
    67      * @return FactoryMethodDefinition 
    68      */ 
    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     } 
    7783} 
    7884?> 
  • trunk/XJConf/converters/factories/ConstructorValueConverterFactory.php

    r14 r22  
    11<?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 */ 
    28XJConfLoader::load('converters.factories.ValueConverterFactory', 
    39                   'converters.ConstructorValueConverter'); 
     10/** 
     11 * Factory to create an ConstructorValueConverter. 
     12 *  
     13 * @package     XJConf 
     14 * @subpackage  converters 
     15 */ 
     16class ConstructorValueConverterFactory implements ValueConverterFactory 
     17{ 
    418 
    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'); 
    932    } 
    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    { 
    1242        $converter = new ConstructorValueConverter($def->getType()); 
    1343        return $converter; 
  • trunk/XJConf/converters/factories/FactoryMethodValueConverterFactory.php

    r14 r22  
    11<?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 */ 
     8XJConfLoader::load('converters.factories.ValueConverterFactory', 
     9                   'converters.FactoryMethodValueConverter' 
     10); 
     11/** 
     12 * Factory to create an FactoryMethodValueConverter. 
     13 *  
     14 * @package     XJConf 
     15 * @subpackage  converters 
     16 */ 
     17class 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'); 
    832    } 
    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) { 
    1344            return null; 
    1445        } 
     46         
    1547        $converter = new FactoryMethodValueConverter($def->getType(), $factoryMethod->getName()); 
    1648        return $converter; 
  • trunk/XJConf/converters/factories/ValueConverterFactoryChain.php

    r14 r22  
    5959ValueConverterFactoryChain::push(new PrimitiveValueConverterFactory()); 
    6060ValueConverterFactoryChain::push(new ArrayValueConverterFactory()); 
    61 ValueConverterFactoryChain::push(new ObjectValueConverterFactory()); 
     61ValueConverterFactoryChain::push(new ConstructorValueConverterFactory()); 
    6262ValueConverterFactoryChain::push(new FactoryMethodValueConverterFactory()); 
    6363?> 
  • trunk/XJConf/definitions/FactoryMethodDefinition.php

    r21 r22  
    147147        return $this->params; 
    148148    } 
     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    } 
    149170         
    150171    /** 
  • trunk/XJConf/definitions/TagDefinition.php

    r21 r22  
    163163        // no constructor definition has been set, 
    164164        // create a new one 
    165         if (null == $this->constructor) { 
     165        if (null == $this->constructor && null == $this->factoryMethod) { 
    166166            $this->constructor = new ConstructorDefinition(); 
    167167            try { 
     
    277277        $children = $this->getChildDefinitions(); 
    278278        foreach ($children as $child) { 
    279             if (get_class($param) == $def) { 
    280                 return $param
     279            if (get_class($child) == $def) { 
     280                return $child
    281281            } 
    282282        } 
  • trunk/examples/ColorPrimitives.php

    r2 r22  
    66 * Window - Preferences - Java - Code Style - Code Templates 
    77 */ 
     8class ColorPrimitivesFactory 
     9{ 
     10    public static function create($name) 
     11    { 
     12        $colorPrimities = new ColorPrimitives($name); 
     13        return $colorPrimities; 
     14    } 
     15} 
     16 
    817class ColorPrimitives 
    918{