Changeset 382

Show
Ignore:
Timestamp:
03/15/07 15:46:27 (2 years ago)
Author:
mikey
Message:

finished net.stubbles.websites.variantmanager.stubVariantsPreInterceptor
added unit tests for net.stubbles.websites.variantmanager.stubVariantsPreInterceptor

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantsPreInterceptor.php

    r380 r382  
    2222 * net.stubbles.websites.variantmanager.variantfactory.class 
    2323 *     name of the variant factory class to use 
    24  *     default: net.stubbles.websites.variantmanager.stubVariantXJConfFactory 
     24 *     default: net.stubbles.websites.variantmanager.stubXJConfVariantFactory 
    2525 * net.stubbles.websites.variantmanager.cookie.name 
    2626 *     name of the cookie where the variant name should be stored 
     
    2929 *     duration of how long the cookie should be stored 
    3030 *     default: 90 days 
     31 * net.stubbles.websites.variantmanager.cookie.url 
     32 *     url of the cookie 
     33 *     default: null (same domain as application is running on) 
     34 * net.stubbles.websites.variantmanager.cookie.path 
     35 *     path of the cookie 
     36 *     default: / 
    3137 * </code> 
    3238 * Default values are choosen if no explicit values are set. 
     
    3743 * @package     stubbles 
    3844 * @subpackage  websites_variantmanager 
    39  * Inject(stubRequest:stubSession
     45 * @Inject(stubRequest:stubSession:stubResponse
    4046 */ 
    4147class stubVariantsPreInterceptor extends stubBaseObject implements stubPreInterceptor 
     
    95101    public function preProcess() 
    96102    { 
    97         if ($this->session->isNew() == false) { 
     103        if ($this->session->hasValue('net.stubbles.websites.variantmanager.variant') == true) { 
    98104            return; 
    99105        } 
    100106         
    101         $fqClassname = stubRegistry::getConfig('net.stubbles.websites.variantmanager.variantfactory.class', 'net.stubbles.websites.variantmanager.stubVariantXJConfFactory'); 
     107        $fqClassname = stubRegistry::getConfig('net.stubbles.websites.variantmanager.variantfactory.class', 'net.stubbles.websites.variantmanager.stubXJConfVariantFactory'); 
    102108        $nqClassname = stubClassLoader::getNonQualifiedClassName($fqClassname); 
    103109        if (class_exists($nqClassname, false) == false) { 
     
    105111        } 
    106112         
    107         $variantFactory = new $nqClassname(); 
    108         $variant        = null; 
    109         $cookieName     = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.name', 'variant'); 
    110         if ($this->request->hasValue($cookieName, stubRequest::SOURCE_COOKIE) == true) { 
    111             $variant = $variantFactory->getVariantsMap()->getEnforcingVariant($this->session, $this->request); 
    112             if (null == $variant) { 
    113                 $variantName = $this->request->getValidatedValue(new stubPreSelectValidator($variantFactory->getVariantNames()), $cookieName, stubRequest::SOURCE_COOKIE); 
    114                 if (null == $variantName) { 
    115                     $variant = $variantFactory->getVariantByName($variantName); 
    116                 } 
    117             } 
     113        $variantFactory = $this->createVariantFactory($nqClassname); 
     114        if (($variantFactory instanceof stubVariantFactory) == false) { 
     115            throw new stubVariantConfigurationException('Configured variant factory is not an instance of net.stubbles.websites.variantmanager.stubVariantFactory.'); 
     116        } 
     117         
     118        $variant    = null; 
     119        $cookieName = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.name', 'variant'); 
     120        if ($variantFactory->getVariantsMap()->shouldUsePersistence() == true) { 
     121            $variant = $this->getVariantFromCookie($variantFactory, $cookieName); 
    118122        } 
    119123         
     
    123127         
    124128        $this->session->putValue('net.stubbles.websites.variantmanager.variant', $variant); 
    125         $expiring = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.expiring', (86400 * 90)); // 90 days default 
    126         $this->response->setCookie(stubCookie::create($cookieName, $variant->getName())->expiringAt(time() + $expiring)->forPath('/')); 
     129        $expiring   = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.expiring', (86400 * 90)); // 90 days default 
     130        $cookieURL  = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.url', null); 
     131        $cookiePath = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.path', '/'); 
     132        $this->response->setCookie($this->createCookie($cookieName, $variant->getName(), $expiring, $cookieURL, $cookiePath)); 
     133    } 
     134     
     135    /** 
     136     * creates the variant factory 
     137     * 
     138     * @param   string              $classname  classname of the variant factory to create 
     139     * @return  stubVariantFactory 
     140     */ 
     141    protected function createVariantFactory($classname) 
     142    { 
     143        return new $classname(); 
     144    } 
     145     
     146    /** 
     147     * tries to get the variant from the cookie 
     148     * 
     149     * @param   stubVariantFactory  $variantFactory  the variant factory to use 
     150     * @param   string              $cookieName      name of the cookie 
     151     * @return  stubVariant 
     152     */ 
     153    protected function getVariantFromCookie(stubVariantFactory $variantFactory, $cookieName) 
     154    { 
     155        if ($this->request->hasValue($cookieName, stubRequest::SOURCE_COOKIE) == false) { 
     156            return null; 
     157        } 
     158         
     159        $variantName = $this->request->getValidatedValue(new stubPreSelectValidator($variantFactory->getVariantNames()), $cookieName, stubRequest::SOURCE_COOKIE); 
     160        if (null == $variantName) { 
     161            return null; 
     162        } 
     163         
     164        $cookieVariant    = $variantFactory->getVariantByName($variantName); 
     165        $enforcingVariant = $variantFactory->getVariantsMap()->getEnforcingVariant($this->session, $this->request); 
     166        if (null == $enforcingVariant) { 
     167            return $cookieVariant; 
     168        } 
     169         
     170        if (substr($cookieVariant->getName(), 0, strlen($enforcingVariant->getName())) == $enforcingVariant->getName()) { 
     171            return $enforcingVariant; 
     172        } 
     173         
     174        return $cookieVariant; 
     175    } 
     176     
     177    /** 
     178     * creates the cookie 
     179     * 
     180     * @param   string      $cookieName 
     181     * @param   string      $variantName 
     182     * @param   int         $expiring 
     183     * @param   string      $cookieURL 
     184     * @param   string      $cookiePath 
     185     * @return  stubCookie 
     186     */ 
     187    protected function createCookie($cookieName, $variantName, $expiring, $cookieURL, $cookiePath) 
     188    { 
     189        return stubCookie::create($cookieName, $variantName)->expiringAt(time() + $expiring)->forPath($cookiePath)->forDomain($cookieURL); 
    127190    } 
    128191} 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/stubXJConfVariantFactory.php

    r380 r382  
    1818 * @subpackage  websites_variantmanager 
    1919 */ 
    20 class stubVariantXJConfFactory extends stubAbstractVariantFactory 
     20class stubXJConfVariantFactory extends stubAbstractVariantFactory 
    2121{ 
    2222    /** 
  • trunk/src/test/php/net/stubbles/websites/variantmanager/VariantManagerTestSuite.php

    r328 r382  
    2828        $this->addTestFile(dirname(__FILE__) . '/types/stubRootVariantTestCase.php'); 
    2929        $this->addTestFile(dirname(__FILE__) . '/stubVariantsMapTestCase.php'); 
     30        $this->addTestFile(dirname(__FILE__) . '/stubVariantsPreInterceptorCookieVariantTestCase.php'); 
     31        $this->addTestFile(dirname(__FILE__) . '/stubVariantsPreInterceptorProcessTestCase.php'); 
    3032    } 
    3133}