Changeset 61
- Timestamp:
- 11/29/06 23:54:41 (2 years ago)
- Files:
-
- trunk/XJConf/DefinedTag.php (modified) (4 diffs)
- trunk/XJConf/converters/AbstractObjectValueConverter.php (modified) (1 diff)
- trunk/XJConf/converters/ArrayValueConverter.php (modified) (2 diffs)
- trunk/XJConf/converters/ConstructorValueConverter.php (modified) (1 diff)
- trunk/XJConf/converters/FactoryMethodValueConverter.php (modified) (1 diff)
- trunk/XJConf/definitions/AbstractTagDefinition.php (added)
- trunk/XJConf/definitions/BaseTagDefinition.php (added)
- trunk/XJConf/definitions/NamespaceDefinition.php (modified) (3 diffs)
- trunk/XJConf/definitions/NamespaceDefinitions.php (modified) (1 diff)
- trunk/XJConf/definitions/TagDefinition.php (modified) (4 diffs)
- trunk/XJConf/definitions/handler/AbstractTagDefinitionHandler.php (added)
- trunk/examples/xml/defines-dynamic-types.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/XJConf/DefinedTag.php
r53 r61 7 7 */ 8 8 XJConfLoader::load('Tag', 9 'definitions. TagDefinition'9 'definitions.BaseTagDefinition' 10 10 ); 11 11 /** … … 55 55 * value of the tag 56 56 * 57 * @var TagDefinition57 * @var BaseTagDefinition 58 58 */ 59 59 private $tagDef = null; … … 265 265 * Set the tag definition object used for this tag 266 266 * 267 * @param TagDefinition $tagDef 268 * @todo in case of dynamic types the original type should not get lost 269 * but used to check if the dynamic type is as well an instance of 270 * the original type >>> forcing interfaces/inheritance 267 * @param BaseTagDefinition $tagDef 271 268 */ 272 public function setDefinition( TagDefinition $tagDef)273 { 274 if ($t his->hasAttribute('__type') == false) {275 $this->tagDef = $tagDef;269 public function setDefinition(BaseTagDefinition $tagDef) 270 { 271 if ($tagDef instanceof TagDefinition) { 272 $this->tagDef = $tagDef; 276 273 return; 277 274 } 278 275 279 $clonedTagDef = clone $tagDef; 280 $this->tagDef = $clonedTagDef; 281 $this->tagDef->setType($this->getAttribute('__type')); 276 $this->tagDef = clone $tagDef; 277 $this->tagDef->setType($this->getAttribute($this->tagDef->getConcreteTypeAttributeName())); 282 278 } 283 279 … … 285 281 * get the tag definition object used for this tag 286 282 * 287 * @return TagDefinition283 * @return BaseTagDefinition 288 284 */ 289 285 public function getDefinition() trunk/XJConf/converters/AbstractObjectValueConverter.php
r56 r61 27 27 * Add all attributes using the appropriate setter methods 28 28 * 29 * @param Tag $tag30 * @param Definition $def31 * @param object $instance29 * @param Tag $tag 30 * @param BaseTagDefinition $def 31 * @param object $instance 32 32 * @throws ValueConversionException 33 33 */ 34 protected function addAttributesToValue(Tag $tag, TagDefinition $def, $instance)34 protected function addAttributesToValue(Tag $tag, BaseTagDefinition $def, $instance) 35 35 { 36 36 $class = new ReflectionClass(get_class($instance)); trunk/XJConf/converters/ArrayValueConverter.php
r33 r61 7 7 XJConfLoader::load('converters.ValueConverter', 8 8 'definitions.Definition', 9 'definitions. TagDefinition'9 'definitions.BaseTagDefinition' 10 10 ); 11 11 /** … … 48 48 * @param array $array 49 49 */ 50 protected function addAttributesToValue(Tag $tag, TagDefinition $def, $array)50 protected function addAttributesToValue(Tag $tag, BaseTagDefinition $def, $array) 51 51 { 52 52 // set all attributes trunk/XJConf/converters/ConstructorValueConverter.php
r56 r61 70 70 71 71 // add attributes and child elements 72 if ($def instanceof TagDefinition) {72 if ($def instanceof BaseTagDefinition) { 73 73 $this->addAttributesToValue($tag, $def, $instance); 74 74 $this->addCDataToValue($tag, $def, $instance); trunk/XJConf/converters/FactoryMethodValueConverter.php
r56 r61 73 73 74 74 // add attributes and child elements 75 if ($def instanceof TagDefinition) {75 if ($def instanceof BaseTagDefinition) { 76 76 $this->addAttributesToValue($tag, $def, $instance); 77 77 $this->addCDataToValue($tag, $def, $instance); trunk/XJConf/definitions/NamespaceDefinition.php
r2 r61 11 11 * list of tag definitions 12 12 * 13 * @var array<String, TagDefinition>13 * @var array<String,BaseTagDefinition> 14 14 */ 15 15 private $tagDefinitions = array(); … … 35 35 * Add a new tag definition 36 36 * 37 * @param TagDefinition $tagDefinition37 * @param BaseTagDefinition $tagDefinition 38 38 */ 39 public function addTagDefinition( TagDefinition $tagDefinition)39 public function addTagDefinition(BaseTagDefinition $tagDefinition) 40 40 { 41 41 $this->tagDefinitions[$tagDefinition->getTagName()] = $tagDefinition; … … 67 67 * 68 68 * @param tagName name of the tag 69 * @return TagDefinition69 * @return BaseTagDefinition 70 70 */ 71 71 public function getDefinition($tagName) trunk/XJConf/definitions/NamespaceDefinitions.php
r2 r61 81 81 * Get the definition of a single tag 82 82 * 83 * @param string $namespace namespace URI84 * @param string $tagName local tag name85 * @return TagDefinition83 * @param string $namespace namespace URI 84 * @param string $tagName local tag name 85 * @return BaseTagDefinition 86 86 */ 87 87 public function getTagDefinition($namespace, $tagName) trunk/XJConf/definitions/TagDefinition.php
r46 r61 6 6 * @author Frank Kleine <frank.kleine@schlund.de> 7 7 */ 8 XJConfLoader::load('converters.factories.ValueConverterFactoryChain', 9 'definitions.AttributeDefinition', 10 'definitions.CDataDefinition', 11 'definitions.ConstructorDefinition', 12 'definitions.FactoryMethodDefinition', 13 'definitions.Definition', 14 'exceptions.ValueConversionException', 15 'exceptions.XJConfException', 16 'XJConfClassLoader' 17 ); 8 XJConfLoader::load('definitions.BaseTagDefinition'); 18 9 /** 19 10 * Definition of an XML tag. … … 22 13 * @subpackage definitions 23 14 */ 24 class TagDefinition implementsDefinition15 class TagDefinition extends BaseTagDefinition 25 16 { 26 /**27 * the name28 *29 * @var string30 */31 private $name = null;32 /**33 * the name of the tag34 *35 * @var string36 */37 private $tagName = null;38 /**39 * type of the tag40 *41 * @var string42 */43 private $type = null;44 /**45 * list of attribute definitions46 *47 * @var array<AttributeDefinition>48 */49 private $atts = array();50 /**51 * name of the setter52 *53 * @var string54 */55 private $setter = null;56 /**57 * name of attribute that contains the name58 *59 * @var string60 */61 private $nameAttribute = null;62 /**63 * definition of how to construct the object64 *65 * @var ConstructorDefinition66 */67 private $constructor = null;68 /**69 * definition of factory that is able to construct the object70 *71 * @var FactoryMethodDefinition72 */73 private $factoryMethod = null;74 /**75 * converts the value76 *77 * @var ValueConverter78 */79 private $valueConverter;80 /**81 * definition of tag content82 *83 * @var CDataDefinition84 * @todo Eventually call the setter method for the cdata85 */86 private $cdata = null;87 /**88 * the class loader to use89 *90 * @var XJConfClassLoader91 */92 private $classLoader = null;93 94 17 /** 95 18 * Create a new tag definition … … 97 20 * @param string $name name of the tag 98 21 * @param string $type type of the tag 99 * @throws XJConfException22 * @throws InvalidTagDefinitionException 100 23 */ 101 24 public function __construct($name, $type) 102 25 { 103 26 if (null == $name || strlen($name) == 0) { 104 throw new XJConfException('TagDefinition needs a name.');27 throw new InvalidTagDefinitionException('TagDefinition needs a name.'); 105 28 } 106 29 if (null == $type || strlen($type) == 0) { 107 throw new XJConfException('TagDefinition needs a type.');30 throw new InvalidTagDefinitionException('TagDefinition needs a type.'); 108 31 } 109 32 … … 112 35 $this->setType($type); 113 36 } 114 115 /**116 * set the name of the value117 *118 * @param string $name119 */120 public function setName($name)121 {122 $this->name = $name;123 }124 125 /**126 * get the name of the value127 *128 * @return string129 */130 public function getName()131 {132 return $this->name;133 }134 135 /**136 * Set the type of the tag137 *138 * @param string $type139 */140 public function setType($type)141 {142 $this->type = $type;143 }144 145 /**146 * get the type of the tag147 *148 * @return string149 */150 public function getType()151 {152 if (null != $this->classLoader) {153 return $this->classLoader->getType($this->type);154 }155 156 return $this->type;157 }158 159 /**160 * Convert the value of the tag.161 *162 * @param Tag $tag tag that will be converted163 * @return mixed converted value164 * @throws ValueConversionException165 */166 public function convertValue(Tag $tag)167 {168 // get the data169 $data = $tag->getContent();170 if (null == $data) {171 $data = '';172 }173 174 // no constructor definition has been set,175 // create a new one176 if (null == $this->constructor && null == $this->factoryMethod) {177 $this->constructor = new ConstructorDefinition();178 $this->constructor->addChildDefinition(new CDataDefinition());179 }180 181 $instance = $this->getValueConverter()->convertValue($tag, $this);182 return $instance;183 }184 185 /**186 * Get the type of the tag187 *188 * @return string189 */190 public function getValueType(Tag $tag)191 {192 return $this->getValueConverter()->getType();193 }194 195 /**196 * Set the setter method197 *198 * @param string $setter name of the setter method199 */200 public function setSetterMethod($setter)201 {202 $this->setter = $setter;203 }204 205 /**206 * Get the name of the setter method that should be used207 *208 * @return string209 */210 public function getSetterMethod(Tag $tag)211 {212 if (null != $this->setter) {213 return $this->setter;214 }215 216 // no name, the parent should be a collection217 if ('__none' == $this->name) {218 return null;219 }220 221 return 'set' . ucfirst($this->getKey($tag));222 }223 224 /**225 * Add a new child definition226 *227 * Possible definitions are:228 * - AttributeDefinition229 * - ConstructorDefinition230 * - FactoryMethodDefinition231 * - CDataDefinition232 *233 * @param Definition $def234 */235 public function addChildDefinition(Definition $def)236 {237 if ($def instanceof AttributeDefinition) {238 $this->addAttribute($def);239 return;240 }241 242 if ($def instanceof FactoryMethodDefinition) {243 $this->factoryMethod = $def;244 return;245 }246 247 if ($def instanceof ConstructorDefinition) {248 $this->constructor = $def;249 return;250 }251 252 if ($def instanceof CDataDefinition) {253 $this->cdata = $def;254 return;255 }256 }257 258 /**259 * Checks whether this definition has a specific child condition260 *261 * @param string $def262 * @return boolean true if definition has a specific child condition, else false263 */264 public function hasChildDefinition($def)265 {266 $children = $this->getChildDefinitions();267 foreach ($children as $child) {268 if (get_class($child) == $def) {269 return true;270 }271 }272 273 return false;274 }275 276 /**277 * Returns the first found definition of type $def278 *279 * @param string $def280 * @return Definition281 */282 public function getChildDefinition($def)283 {284 $children = $this->getChildDefinitions();285 foreach ($children as $child) {286 if (get_class($child) == $def) {287 return $child;288 }289 }290 291 return null;292 }293 294 /**295 * Return all child definitions.296 *297 * @return array298 */299 public function getChildDefinitions()300 {301 $children = $this->atts;302 if ($this->factoryMethod instanceof Definition) {303 $children[] = $this->factoryMethod;304 }305 306 if ($this->constructor instanceof Definition) {307 $children[] = $this->constructor;308 }309 310 if ($this->cdata instanceof Definition) {311 $children[] = $this->cdata;312 }313 314 return $children;315 }316 317 /**318 * Add an attribute to the tag319 *320 * @param AttributeDefinition $att321 */322 public function addAttribute(AttributeDefinition $att)323 {324 array_push($this->atts, $att);325 }326 327 /**328 * Return list of attributes for this tag329 *330 * @return array331 */332 public function getAttributes() {333 return $this->atts;334 }335 336 /**337 * Set the name of the tag338 *339 * @param name340 */341 public function setTagName($name)342 {343 $this->tagName = $name;344 }345 346 /**347 * Set the attribute that will be used as key.348 *349 * @return name of the value350 */351 public function setKeyAttribute($att)352 {353 $this->name = '__attribute';354 $this->nameAttribute = $att;355 }356 357 /**358 * get the name of the tag359 *360 * @return string361 */362 public function getTagName()363 {364 return $this->tagName;365 }366 367 /**368 * get the name of the tag369 *370 * @return string371 */372 public function getKey(DefinedTag $tag)373 {374 if ('__attribute' == $this->name) {375 return $tag->getAttribute($this->nameAttribute);376 }377 378 return $this->name;379 }380 381 /**382 * Check, whether the value supports indexed children383 *384 * @return boolean385 */386 public function supportsIndexedChildren()387 {388 if ($this->getType() == 'array') {389 return true;390 }391 392 return false;393 }394 395 /**396 * set the class loader for this tag397 *398 * @param XJConfClassLoader $classLoader399 */400 public function setClassLoader(XJConfClassLoader $classLoader)401 {402 $this->classLoader = $classLoader;403 }404 405 /**406 * Get the value converter for this tag407 *408 * @return ValueConverter409 */410 private function getValueConverter()411 {412 if (null != $this->classLoader) {413 $this->classLoader->loadClass($this->type);414 }415 416 if (null == $this->valueConverter) {417 $this->valueConverter = ValueConverterFactoryChain::getFactory($this)->createValueConverter($this);418 }419 420 return $this->valueConverter;421 }422 37 } 423 38 ?> trunk/examples/xml/defines-dynamic-types.xml
r53 r61 1 1 <defines> 2 2 <tag name="foo" type="MyCollector"/> 3 < tag name="bar" type="MyInterface" setter="addBar" />3 <abstractTag name="bar" abstractType="MyInterface" setter="addBar" concreteTypeAttribute="__type" /> 4 4 </defines>
