Changeset 908
- Timestamp:
- 09/12/07 00:06:07 (1 year ago)
- Files:
-
- trunk/examples/config/xml/pages/conf/index.xml (modified) (1 diff)
- trunk/examples/config/xml/pages/txt/main_index.xml (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/examples/pageelements/TestElementWithInjectedResource.php (added)
- trunk/src/main/php/net/stubbles/ioc/stubInjector.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManager.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManagerPreInterceptor.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/websites/xml/stubXMLProcessor.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/examples/config/xml/pages/conf/index.xml
r906 r908 17 17 </xmlElementCachingDecorator> 18 18 <xmlElement type="net.stubbles.examples.pageelements.CurrentTimeXMLPageElement" name="uncached" /> 19 <xmlElement type="net.stubbles.examples.pageelements.TestElementWithInjectedResource" name="uncached" /> 19 20 20 21 <xmlPassThru fileName="test.xml" name="passThru"> trunk/examples/config/xml/pages/txt/main_index.xml
r858 r908 25 25 <!-- This is an include in the same document --> 26 26 <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> 28 31 <!-- This is an include from a different file --> 29 32 <stub:include part="disclaimer" href="include/parts.xml"/> … … 42 45 </part> 43 46 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 44 56 </parts> trunk/src/main/php/net/stubbles/ioc/stubInjector.php
r855 r908 122 122 return ($this->getBinding($type, $name) != null); 123 123 } 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 } 124 167 } 125 168 ?> trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManager.php
r904 r908 11 11 * @link http://pustefix.oss.schlund.de 12 12 */ 13 stubClassLoader::load('net.stubbles.ipo.session.resourcemanager.stubSessionResource'); 13 stubClassLoader::load('net.stubbles.ipo.session.resourcemanager.stubSessionResource', 14 'net.stubbles.ioc.stubInjectionProvider'); 14 15 15 16 /** … … 23 24 * @subpackage ipo_resourcemanager 24 25 */ 25 class stubSessionResourceManager extends stubSerializableObject {26 class stubSessionResourceManager extends stubSerializableObject implements stubInjectionProvider { 26 27 27 28 /** … … 61 62 $this->resourceDefinitions[$interface] = $class; 62 63 } 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; 63 85 } 64 86 … … 151 173 return $clazz->newInstance(); 152 174 } 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 } 153 187 } 154 188 ?> trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManagerPreInterceptor.php
r905 r908 2 2 /** 3 3 * 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. 4 8 * 5 9 * @author Stephan Schmidt <schst@stubbles.net> … … 43 47 } 44 48 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 } 45 54 } 46 55 trunk/src/main/php/net/stubbles/websites/xml/stubXMLProcessor.php
r906 r908 56 56 $formValues = array(); 57 57 58 if (stubRegistry::has('net.stubbles.ioc.stubBinder')) { 59 $injector = stubRegistry::get('net.stubbles.ioc.stubBinder')->getInjector(); 60 } else { 61 $injector = null; 62 } 63 58 64 foreach ($elements as $name => $element) { 59 65 $prefixRequest->setPrefix($name); … … 61 67 continue; 62 68 } 63 69 if ($injector != null) { 70 $injector->handleInjections($element); 71 } 64 72 $data = $element->process($prefixRequest, $this->session, $this->response); 65 73 $xmlSerializer->serialize($data, $xmlStreamWriter, array(stubXMLSerializer::OPT_ROOT_TAG => $name));
