root/trunk/src/main/php/net/stubbles/websites/processors/stubDefaultProcessorResolver.php

Revision 1547, 5.9 kB (checked in by mikey, 3 months ago)

refactoring #139, part 6: moved net::stubbles::util::validators to net::stubbles::ipo::request::validators

Line 
1 <?php
2 /**
3  * Default implementation for the processor resolver.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  websites_processors
8  */
9 stubClassLoader::load('net::stubbles::ipo::request::validator::stubPreSelectValidator',
10                       'net::stubbles::lang::exceptions::stubConfigurationException',
11                       'net::stubbles::websites::processors::stubAbstractProcessorResolver'
12 );
13 /**
14  * Default implementation for the processor resolver.
15  *
16  * The default processor resolver is able to select the processor to be used
17  * for the current request depending on the request parameter <em>processor</em>.
18  * For instance, if you add two processors with
19  * <code>
20  *   $defaultProcessor->addProcessor('foo', 'org::stubbles::test::FooProcessor');
21  *   $defaultProcessor->addProcessor('bar', 'org::stubbles::test::BarProcessor');
22  * </code>
23  * then the first processor class will be selected if the value of the request
24  * param is <em>foo</em>.
25  *
26  * To make sure that a processor gets selected even in case the parameter is not
27  * set or has a wrong value, one can set the default processor to be used:
28  * <code>
29  *   $defaultProcessor->setDefaultProcessor('foo');
30  * </code>
31  * Now, the processor with this key will be selected in such cases as well. You
32  * should make sure that the default processor matches one of the added
33  * processors, else an exception will be thrown.
34  *
35  * Additionally one can set the interceptor descriptor and the page factory
36  * class. While the first is used to determine the interceptor configuration to
37  * be used in conjunction with this processor, the latter determines the page
38  * factory class that will be injected into the processor's
39  * <code>selectPage()</code> method if the processor implements the
40  * <code>net::stubbles::websites::processors::stubPageBasedProcessor</code>
41  * interface.
42  *
43  * @package     stubbles
44  * @subpackage  websites_processors
45  */
46 class stubDefaultProcessorResolver extends stubAbstractProcessorResolver
47 {
48     /**
49      * the default processor to use
50      *
51      * @var  string
52      */
53     protected $defaultProcessor       = null;
54     /**
55      * list of processors
56      *
57      * @var  array<string,string>
58      */
59     protected $processors             = array();
60     /**
61      * list of interceptor descriptors
62      *
63      * @var  array<string,string>
64      */
65     protected $interceptorDescriptors = array();
66     /**
67      * list of page factory classes
68      *
69      * @var  array<string,string>
70      */
71     protected $pageFactoryClasses     = array();
72
73     /**
74      * adds a processor to the list of available processors
75      *
76      * @param  string  $paramValue             value of the request parameter that identifies this processor
77      * @param  string  $fqClassName            full qualified class name of the processor
78      * @param  string  $interceptorDescriptor  optional  the interceptor descriptor
79      * @param  string  $pageFactoryClass       optional  page factory class for the processor
80      */
81     public function addProcessor($paramValue, $fqClassName, $interceptorDescriptor = null, $pageFactoryClass = null)
82     {
83         $this->processors[$paramValue] = $fqClassName;
84         if (null != $interceptorDescriptor) {
85             $this->interceptorDescriptors[$fqClassName] = $interceptorDescriptor;
86         } else {
87             $this->interceptorDescriptors[$fqClassName] = 'interceptors';
88         }
89         
90         if (null !== $pageFactoryClass) {
91             $this->pageFactoryClasses[$fqClassName] = $pageFactoryClass;
92         }
93     }
94
95     /**
96      * sets the name of the default processor
97      *
98      * @param  string  $defaultProcessor  value of the request parameter that identifies this processor
99      */
100     public function setDefaultProcessor($defaultProcessor)
101     {
102         $this->defaultProcessor = $defaultProcessor;
103     }
104
105     /**
106      * does the real resolving work
107      *
108      * @param   stubRequest   $request   the current request
109      * @param   stubSession   $session   the current session
110      * @param   stubResponse  $response  the current response
111      * @return  string        full qualified classname of the processor to create
112      * @throws  stubConfigurationException
113      */
114     protected function doResolve(stubRequest $request, stubSession $session, stubResponse $response)
115     {
116         $paramValue = null;
117         if ($request->hasValue('processor') === true) {
118             $paramValue = $request->getValidatedValue(new stubPreSelectValidator(array_keys($this->processors)), 'processor');
119         }
120         
121         if (null == $paramValue) {
122             if (null == $this->defaultProcessor || isset($this->processors[$this->defaultProcessor]) === false) {
123                 throw new stubConfigurationException('Configuration error: the default processor ' . $this->defaultProcessor . ' is not set.');
124             }
125             
126             $paramValue = $this->defaultProcessor;
127         }
128             
129         $session->putValue('net.stubbles.websites.lastProcessor', $paramValue);
130         return $this->processors[$paramValue];
131     }
132
133     /**
134      * configures the processor
135      *
136      * @param  stubProcessor  $processor
137      */
138     protected function configure(stubProcessor $processor)
139     {
140         $processorClassName = $processor->getClassName();
141         if (isset($this->interceptorDescriptors[$processorClassName]) === true && strlen($this->interceptorDescriptors[$processorClassName]) > 0) {
142             $processor->setInterceptorDescriptor($this->interceptorDescriptors[$processorClassName]);
143         }
144     }
145
146     /**
147      * returns the page factory class for the processor
148      *
149      * @param   stubProcessor  $processor
150      * @return  string
151      */
152     protected function getPageFactoryClass(stubProcessor $processor)
153     {
154         $processorClassName = $processor->getClassName();
155         if (isset($this->pageFactoryClasses[$processorClassName]) === true) {
156             return $this->pageFactoryClasses[$processorClassName];
157         }
158         
159         return null;
160     }
161 }
162 ?>
Note: See TracBrowser for help on using the browser.