Changeset 551
- Timestamp:
- 04/17/07 16:21:19 (2 years ago)
- Files:
-
- trunk/src/main/php/net/stubbles/websites/variantmanager/stubAbstractVariantFactory.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantXJConfFactory.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantsMap.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubAbstractVariant.php (modified) (24 diffs)
- trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubVariant.php (modified) (17 diffs)
- trunk/src/test/php/net/stubbles/websites/variantmanager/VariantManagerTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/websites/variantmanager/stubVariantXJConfFactoryTestCase.php (added)
- trunk/src/test/php/net/stubbles/websites/variantmanager/stubVariantsMapTestCase.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/websites/variantmanager/types/stubAbstractVariantTestCase.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/variantmanager/stubAbstractVariantFactory.php
r379 r551 8 8 * @subpackage websites_variantmanager 9 9 */ 10 stubClassLoader::load('net.stubbles.websites.variantmanager.stubVariantFactory'); 10 stubClassLoader::load('net.stubbles.websites.variantmanager.stubVariantFactory', 11 'net.stubbles.websites.variantmanager.stubVariantsMap' 12 ); 11 13 /** 12 14 * Basic abstract implementation for a variant factory. trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantXJConfFactory.php
r447 r551 8 8 * @subpackage websites_variantmanager 9 9 */ 10 stubClassLoader::load('net.stubbles.util.stubFactory', 11 'net.stubbles.util.xjconf.xjconf', 10 stubClassLoader::load('net.stubbles.util.xjconf.xjconf', 12 11 'net.stubbles.websites.variantmanager.stubAbstractVariantFactory' 13 12 ); … … 18 17 * @subpackage websites_variantmanager 19 18 */ 20 class stubVariantXJConfFactory extends stubAbstractVariantFactory 19 class stubVariantXJConfFactory extends stubAbstractVariantFactory implements stubXJConfInitializer 21 20 { 22 21 /** 23 22 * constructor 24 23 * 25 * @param string $configfile optional name of configuration file where variants are configured26 24 * @throws stubVariantConfigurationException 27 25 */ 28 public function __construct( $configFile = null)26 public function __construct() 29 27 { 30 $xjconf = new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance())); 31 $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/variantmanager.xml')); 28 $xjconfProxy = new stubXJConfProxy($this); 29 try { 30 $xjconfProxy->process(); 31 } catch (stubXJConfException $xjce) { 32 throw new stubVariantConfigurationException('Can not read variant configuration: ' . $xjce->getMessage(), $xjce); 33 } 34 } 35 36 /** 37 * returns the descriptor that identifies this initializer 38 * 39 * @return string 40 */ 41 public function getDescriptor() 42 { 43 return 'variantmanager'; 44 } 45 46 /** 47 * returns the data to cache 48 * 49 * @return array 50 */ 51 public function getCacheData() 52 { 53 $cacheData = array('name' => $this->variantsMap->getName(), 54 'usePersistence' => $this->variantsMap->shouldUsePersistence(), 55 'rootVariant' => $this->variantsMap->getRootVariant()->getSerialized() 56 ); 57 return $cacheData; 58 } 59 60 /** 61 * sets the data from the cache 62 * 63 * @param array $cacheData 64 */ 65 public function setCacheData(array $cacheData) 66 { 67 $this->variantsMap = new stubVariantsMap($cacheData['rootVariant']->getUnserialized()); 68 $this->variantsMap->setName($cacheData['name']); 69 $this->variantsMap->setUsePersistence($cacheData['usePersistence']); 32 70 33 if (null == $configFile) {34 $configFile = stubConfig::getConfigPath() . '/xml/variantmanager.xml'; 35 }36 37 try {38 $xjconf->parse($configFile);39 } catch (stubXJConfException $xjce) {40 throw new stubVariantConfigurationException('Can not read variant configuration.', $xjce);41 }42 71 } 72 73 /** 74 * will be called in case the stubXJConfProxy did not found the data in the 75 * cache and the initializer has to load values from the facade 76 * 77 * @param stubXJConfFacade $xjconf 78 */ 79 public function loadData(stubXJConfFacade $xjconf) 80 { 43 81 $this->variantsMap = $xjconf->getConfigValue('variants'); 44 82 } trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantsMap.php
r328 r551 54 54 */ 55 55 protected $root; 56 56 57 57 /** 58 58 * constructor 59 */ 60 public function __construct() 61 { 62 $this->root = new stubRootVariant(); 59 * 60 * @param stubRootVariant $rootVariant optional 61 */ 62 public function __construct(stubRootVariant $rootVariant = null) 63 { 64 if (null == $rootVariant) { 65 $this->root = new stubRootVariant(); 66 } else { 67 $this->root = $rootVariant; 68 $children = $rootVariant->getChildren(); 69 foreach ($children as $child) { 70 $this->flattenVariantTree($child); 71 } 72 } 63 73 } 64 74 … … 97 107 return array_keys($this->variants); 98 108 } 99 109 100 110 /** 101 111 * Add a new variant trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubAbstractVariant.php
r524 r551 25 25 * @subpackage websites_variantmanager_types 26 26 */ 27 abstract class stubAbstractVariant extends stub BaseObject implements stubVariant27 abstract class stubAbstractVariant extends stubSerializableObject implements stubVariant 28 28 { 29 29 /** … … 33 33 */ 34 34 protected $children = array(); 35 36 35 /** 37 36 * Reference to the parent variant. … … 40 39 */ 41 40 protected $parent; 42 43 41 /** 44 42 * Name of the variant … … 47 45 */ 48 46 protected $name = ''; 49 50 47 /** 51 48 * Title of the variant, only used when exporting the … … 55 52 */ 56 53 protected $title = ''; 57 58 54 /** 59 55 * alias of the variant, only used when exporting the … … 63 59 */ 64 60 protected $alias = ''; 65 61 66 62 /** 67 63 * returns the name of the variant … … 73 69 return $this->name; 74 70 } 75 71 76 72 /** 77 73 * returns the full qualified name of the variant … … 87 83 return $this->name; 88 84 } 89 85 90 86 /** 91 87 * sets the name of the variant … … 102 98 $this->name = $name; 103 99 } 104 100 105 101 /** 106 102 * returns title of the variant … … 112 108 return $this->title; 113 109 } 114 110 115 111 /** 116 112 * sets the title of the variant … … 122 118 $this->title = $title; 123 119 } 124 120 125 121 /** 126 122 * returns alias name of the variant … … 132 128 return $this->alias; 133 129 } 134 130 135 131 /** 136 132 * set alias name of the variant … … 142 138 $this->alias = $alias; 143 139 } 144 140 145 141 /** 146 142 * return the forced variant … … 167 163 return null; 168 164 } 169 165 170 166 /** 171 167 * return the variant … … 193 189 return null; 194 190 } 195 191 196 192 /** 197 193 * check whether the conditions for this variant are met … … 205 201 return $this->isValid($session, $request); 206 202 } 207 203 208 204 /** 209 205 * assign that this variant has been choosen … … 222 218 return false; 223 219 } 224 220 225 221 /** 226 222 * returns parent variant … … 232 228 return $this->parent; 233 229 } 234 230 235 231 /** 236 232 * set parent variant … … 242 238 $this->parent = $parent; 243 239 } 244 240 245 241 /** 246 242 * check whether the variant has a parent variant … … 252 248 return (null != $this->parent); 253 249 } 254 250 255 251 /** 256 252 * return child variants of this variant … … 262 258 return $this->children; 263 259 } 264 260 265 261 /** 266 262 * add a child variant … … 270 266 public function addChild(stubVariant $child) 271 267 { 268 if ($child->hashCode() == $this->hashCode()) { 269 throw new stubVariantConfigurationException('A variant can not add itself as child.'); 270 } 271 272 272 $this->children[$child->getName()] = $child; 273 273 $child->setParent($this); 274 274 } 275 275 276 276 /** 277 277 * remove a child variant … … 286 286 } 287 287 } 288 288 289 289 /** 290 290 * return string representation of this variant … … 305 305 return $this->name . '(' . $return . ')'; 306 306 } 307 308 /** 309 * ensure that all children are correctly serialized 310 * 311 * @return array<string> list of properties to serialize 312 */ 313 public function __sleep() 314 { 315 $this->_serializedProperties = array(); 316 foreach ($this->children as $name => $child) { 317 $this->_serializedProperties[$name] = $child->getSerialized(); 318 } 319 320 return array('name', 'title', 'alias', '_serializedProperties'); 321 } 322 323 /** 324 * restore all children 325 */ 326 public function __wakeup() 327 { 328 foreach ($this->_serializedProperties as $name => $serializedChild) { 329 $this->addChild($serializedChild->getUnserialized()); 330 } 331 332 $this->_serializedProperties = array(); 333 } 307 334 } 308 335 ?> trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubVariant.php
r523 r551 17 17 * @subpackage websites_variantmanager_types 18 18 */ 19 interface stubVariant 19 interface stubVariant extends stubSerializable 20 20 { 21 21 /** … … 25 25 */ 26 26 public function getName(); 27 27 28 28 /** 29 29 * returns the full qualified name of the variant … … 32 32 */ 33 33 public function getFullQualifiedName(); 34 34 35 35 /** 36 36 * sets the name of the variant … … 39 39 */ 40 40 public function setName($name); 41 41 42 42 /** 43 43 * returns title of the variant … … 46 46 */ 47 47 public function getTitle(); 48 48 49 49 /** 50 50 * sets the title of the variant … … 53 53 */ 54 54 public function setTitle($title); 55 55 56 56 /** 57 57 * returns alias name of the variant … … 60 60 */ 61 61 public function getAlias(); 62 62 63 63 /** 64 64 * check whether the variant is an enforcing variant … … 69 69 */ 70 70 public function isEnforcing(stubSession $session, stubRequest $request); 71 71 72 72 /** 73 73 * return the forced variant … … 78 78 */ 79 79 public function getEnforcingVariant(stubSession $session, stubRequest $request); 80 80 81 81 /** 82 82 * return the variant … … 87 87 */ 88 88 public function getVariant(stubSession $session, stubRequest $request); 89 89 90 90 /** 91 91 * check whether the conditions for this variant are met … … 96 96 */ 97 97 public function conditionsMet(stubSession $session, stubRequest $request); 98 98 99 99 /** 100 100 * check whether the variant is valid … … 113 113 */ 114 114 public function assign(stubSession $session, stubRequest $request); 115 115 116 116 /** 117 117 * returns parent variant … … 120 120 */ 121 121 public function getParent(); 122 122 123 123 /** 124 124 * set parent variant … … 127 127 */ 128 128 public function setParent(stubVariant $parent = null); 129 129 130 130 /** 131 131 * check whether the variant has a parent variant … … 134 134 */ 135 135 public function hasParent(); 136 136 137 137 /** 138 138 * return child variants of this variant … … 141 141 */ 142 142 public function getChildren(); 143 143 144 144 /** 145 145 * add a child variant … … 148 148 */ 149 149 public function addChild(stubVariant $child); 150 150 151 151 /** 152 152 * remove a child variant trunk/src/test/php/net/stubbles/websites/variantmanager/VariantManagerTestSuite.php
r382 r551 30 30 $this->addTestFile(dirname(__FILE__) . '/stubVariantsPreInterceptorCookieVariantTestCase.php'); 31 31 $this->addTestFile(dirname(__FILE__) . '/stubVariantsPreInterceptorProcessTestCase.php'); 32 $this->addTestFile(dirname(__FILE__) . '/stubVariantXJConfFactoryTestCase.php'); 32 33 } 33 34 } trunk/src/test/php/net/stubbles/websites/variantmanager/stubVariantsMapTestCase.php
r328 r551 57 57 */ 58 58 protected $v3; 59 59 60 60 /** 61 61 * set up the test environment … … 83 83 $this->assertEqual($this->variantMap->getName(), 'foo'); 84 84 } 85 85 86 86 /** 87 87 * assure that persistence is handled correct … … 93 93 $this->assertFalse($this->variantMap->shouldUsePersistence()); 94 94 } 95 95 96 96 /** 97 97 * test handling with no variants configured … … 108 108 $this->variantMap->getVariant($this->mockSession, $this->mockRequest); 109 109 } 110 110 111 111 /** 112 112 * test handling with one variant … … 126 126 $this->assertReference($variantTest2, $this->v1); 127 127 } 128 128 129 129 /** 130 130 * Tests that all nested variants are found … … 139 139 $this->assertTrue($this->variantMap->variantExists('v3')); 140 140 } 141 141 142 142 /** 143 143 * Test that no enforcing variant is returned when there is no enforcing variant set … … 165 165 $this->assertReference($this->variantMap->getVariantByName('v3'), $this->v3); 166 166 } 167 167 168 168 /** 169 169 * Test that all variant names will be returned … … 177 177 $this->assertEqual($this->variantMap->getVariantNames(), array('v1', 'v2', 'v3')); 178 178 } 179 180 /** 181 * test creating the variants map with a root variant as argument 182 */ 183 public function testWithRootArgument() 184 { 185 $rootVariant = new stubRootVariant(); 186 $this->v2->addChild($this->v3); 187 $this->v1->addChild($this->v2); 188 $rootVariant->addChild($this->v1); 189 $variantMap = new stubVariantsMap($rootVariant); 190 191 $this->assertEqual($variantMap->getVariantNames(), array('v1', 'v2', 'v3')); 192 $this->assertReference($variantMap->getVariantByName('v1'), $this->v1); 193 $this->assertReference($variantMap->getVariantByName('v2'), $this->v2); 194 $this->assertReference($variantMap->getVariantByName('v3'), $this->v3); 195 } 179 196 } 180 197 ?> trunk/src/test/php/net/stubbles/websites/variantmanager/types/stubAbstractVariantTestCase.php
r524 r551 61 61 */ 62 62 protected $mockRequest; 63 63 64 64 /** 65 65 * set up test environment … … 71 71 $this->mockRequest = new MockstubRequest(); 72 72 } 73 73 74 74 /** 75 75 * test that setting the name works as expected … … 85 85 $this->abstractVariant->setName('foobarbazfoobarbaz'); 86 86 } 87 87 88 88 /** 89 89 * test that setting the title works as expected … … 105 105 $this->assertEqual($this->abstractVariant->getAlias(), 'foo'); 106 106 } 107 107 108 108 /** 109 109 * test a non valid and non enforcing variant … … 130 130 $this->assertReference($variant, $this->abstractVariant); 131 131 } 132 132 133 133 /** 134 134 * test a variant that has non-valid childs … … 160 160 $this->assertReference($variant, $this->abstractVariant); 161 161 } 162 162 163 163 /** 164 164 * test with a valid child … … 184 184 $this->assertReference($variant, $child2); 185 185 } 186 186 187 187 /** 188 188 * test that removing a child works as expected … … 204 204 $this->assertEqual($this->abstractVariant->getChildren(), array('bar' => $child2)); 205 205 } 206 206 207 /** 208 * assure that a variant can not add itself as its own child 209 */ 210 public function testAddItself() 211 { 212 $this->expectException('stubVariantConfigurationException'); 213 $this->abstractVariant->addChild($this->abstractVariant); 214 } 215 207 216 /** 208 217 * test parent … … 224 233 $this->assertEqual($this->abstractVariant->getFullQualifiedName(), 'foo:bar'); 225 234 } 226 235 227 236 /** 228 237 * test full qualified name when parent variant is root variant … … 235 244 $this->assertEqual($this->abstractVariant->getFullQualifiedName(), 'bar'); 236 245 } 246 247 /** 248 * test __sleep() and __wakeup() 249 */ 250 public function testSleepWakeup() 251 { 252 $this->abstractVariant->setName('foo'); 253 $dummy1 = new stubDummyVariant(); 254 $dummy1->setName('bar'); 255 $this->abstractVariant->addChild($dummy1); 256 $dummy2 = new stubDummyVariant(); 257 $dummy2->setName('baz'); 258 $dummy1->addChild($dummy2); 259 260 $serialized = serialize($this->abstractVariant); 261 $abstractVariant = unserialize($serialized); 262 $this->assertFalse($abstractVariant->hasParent()); 263 $children1 = $abstractVariant->getChildren(); 264 $this->assertEqual($children1['bar']->getName(), 'bar'); 265 $this->assertEqual($children1['bar']->getParent()->getName(), 'foo'); 266 $children2 = $children1['bar']->getChildren(); 267 $this->assertEqual($children2['baz']->getName(), 'baz'); 268 $this->assertEqual($children2['baz']->getParent()->getName(), 'bar'); 269 } 237 270 } 238 271 ?>
