Changeset 908

Show
Ignore:
Timestamp:
09/12/07 00:06:07 (1 year ago)
Author:
schst
Message:

Added the ability to inject session resources in the XML page elements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/examples/config/xml/pages/conf/index.xml

    r906 r908  
    1717    </xmlElementCachingDecorator> 
    1818    <xmlElement type="net.stubbles.examples.pageelements.CurrentTimeXMLPageElement" name="uncached" /> 
     19    <xmlElement type="net.stubbles.examples.pageelements.TestElementWithInjectedResource" name="uncached" /> 
    1920 
    2021    <xmlPassThru fileName="test.xml" name="passThru"> 
  • trunk/examples/config/xml/pages/txt/main_index.xml

    r858 r908  
    2525      <!-- This is an include in the same document --> 
    2626      <stub:include part="caching"/> 
    27             <stub:link page="test">Test</stub:link> 
     27      <stub:include part="resources"/> 
     28 
     29      <!-- test a link --> 
     30      <stub:link page="test">Test</stub:link> 
    2831      <!-- This is an include from a different file --> 
    2932      <stub:include part="disclaimer" href="include/parts.xml"/> 
     
    4245  </part> 
    4346 
     47  <part name="resources"> 
     48    <h1>Example for resources</h1> 
     49    <p> 
     50      This example shows, how a resource can be used, to easily store any kind of 
     51      data in the session without having to touch the stubSession interface. 
     52    </p> 
     53    <p>Current counter value : <ixsl:value-of select="document/resources/counter/count/text()"/></p> 
     54  </part> 
     55 
    4456</parts> 
  • trunk/src/main/php/net/stubbles/ioc/stubInjector.php

    r855 r908  
    122122        return ($this->getBinding($type, $name) != null); 
    123123    } 
     124 
     125    /** 
     126     * Enter description here... 
     127     * 
     128     * @param unknown_type $object 
     129     */ 
     130    public function handleInjections($instance) { 
     131        $clazz = new stubReflectionClass(get_class($instance)); 
     132 
     133        foreach ($clazz->getMethods() as $method) { 
     134            if (strncmp($method->getName(), '__', 2) === 0) { 
     135                continue; 
     136            } 
     137            if (!$method->isPublic()) { 
     138                continue; 
     139            } 
     140            if (!$method->hasAnnotation('Inject')) { 
     141                continue; 
     142            } 
     143            $paramValues = array(); 
     144            foreach ($method->getParameters() as $param) { 
     145                $class = $param->getClass(); 
     146                if ($class !== null) { 
     147                    $type = $class->getName(); 
     148                } else { 
     149                    $type = stubConstantBinding::TYPE; 
     150                } 
     151 
     152                $name  = null; 
     153                if ($method->hasAnnotation('Named')) { 
     154                    $name = $method->getAnnotation('Named')->getName(); 
     155                } 
     156                if (!$this->hasBinding($type, $name)) { 
     157                    if ($method->getAnnotation('Inject')->isOptional()) { 
     158                        continue 2; 
     159                    } 
     160                    throw new stubBindingException("Could not create instance of {$this->type}. No binding for type {$type} specified."); 
     161                } 
     162                $paramValues[] = $this->getInstance($type, $name); 
     163            } 
     164            $method->invokeArgs($instance, $paramValues); 
     165        } 
     166    } 
    124167} 
    125168?> 
  • trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManager.php

    r904 r908  
    1111 * @link        http://pustefix.oss.schlund.de 
    1212 */ 
    13 stubClassLoader::load('net.stubbles.ipo.session.resourcemanager.stubSessionResource'); 
     13stubClassLoader::load('net.stubbles.ipo.session.resourcemanager.stubSessionResource', 
     14                      'net.stubbles.ioc.stubInjectionProvider'); 
    1415 
    1516/** 
     
    2324 * @subpackage  ipo_resourcemanager 
    2425 */ 
    25 class stubSessionResourceManager extends stubSerializableObject
     26class stubSessionResourceManager extends stubSerializableObject implements stubInjectionProvider
    2627 
    2728    /** 
     
    6162            $this->resourceDefinitions[$interface] = $class; 
    6263        } 
     64    } 
     65 
     66    /** 
     67     * Get a resource for a specifiec type. 
     68     * 
     69     * This is required by the stubInjectionProvider interface. 
     70     * 
     71     * As PHP has no namespace, this only works, as long as there 
     72     * are no two interfaces with the same name. 
     73     * 
     74     * @param stubReflectionClass $type 
     75     */ 
     76    public function get(stubReflectionClass $type) { 
     77        $typeName = $type->getName(); 
     78        foreach ($this->resourceDefinitions as $interface => $implementation) { 
     79            $localName = substr($interface, strrpos($interface, '.')+1); 
     80            if ($localName === $typeName) { 
     81                return $this->getResource($interface); 
     82            } 
     83        } 
     84        return null; 
    6385    } 
    6486 
     
    151173        return $clazz->newInstance(); 
    152174    } 
     175 
     176    /** 
     177     * Register all session resources with the binder. 
     178     * 
     179     * @param stubBinder $binder 
     180     */ 
     181    public function registerBindings(stubBinder $binder) { 
     182        foreach ($this->resourceDefinitions as $interface => $impl) { 
     183            $localName = substr($interface, strrpos($interface, '.')+1); 
     184            $binder->bind($localName)->toProvider($this); 
     185        } 
     186    } 
    153187} 
    154188?> 
  • trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManagerPreInterceptor.php

    r905 r908  
    22/** 
    33 * Interceptor to initialize the stubResourceManager. 
     4 * 
     5 * If the registry contains a net.stubbles.ioc.stubBinder, the 
     6 * resource manager will be registered as a provider for 
     7 * all resources. 
    48 * 
    59 * @author      Stephan Schmidt <schst@stubbles.net> 
     
    4347        } 
    4448        stubRegistry::set('net.stubbles.ipo.session.resourcemanager.stubSessionResourceManager', $manager); 
     49 
     50        $binder = stubRegistry::get('net.stubbles.ioc.stubBinder'); 
     51        if ($binder != null) { 
     52            $manager->registerBindings($binder); 
     53        } 
    4554    } 
    4655 
  • trunk/src/main/php/net/stubbles/websites/xml/stubXMLProcessor.php

    r906 r908  
    5656        $formValues    = array(); 
    5757 
     58        if (stubRegistry::has('net.stubbles.ioc.stubBinder')) { 
     59            $injector = stubRegistry::get('net.stubbles.ioc.stubBinder')->getInjector(); 
     60        } else { 
     61            $injector = null; 
     62        } 
     63 
    5864        foreach ($elements as $name => $element) { 
    5965            $prefixRequest->setPrefix($name); 
     
    6167                continue; 
    6268            } 
    63  
     69            if ($injector != null) { 
     70                $injector->handleInjections($element); 
     71            } 
    6472            $data = $element->process($prefixRequest, $this->session, $this->response); 
    6573            $xmlSerializer->serialize($data, $xmlStreamWriter, array(stubXMLSerializer::OPT_ROOT_TAG => $name));