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

Revision 1445, 4.4 kB (checked in by mikey, 2 years ago)

refactoring #137, part 7: page factories are now attributes of the processors, and not of the resolvers

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 */
9stubClassLoader::load('net::stubbles::lang::exceptions::stubConfigurationException',
10                      'net::stubbles::util::validators::stubPreSelectValidator',
11                      'net::stubbles::websites::processors::stubAbstractProcessorResolver'
12);
13/**
14 * Default implementation for the processor resolver.
15 *
16 * @static
17 * @package     stubbles
18 * @subpackage  websites_processors
19 */
20class stubDefaultProcessorResolver extends stubAbstractProcessorResolver
21{
22    /**
23     * the default processor to use
24     *
25     * @var  string
26     */
27    protected $defaultProcessor       = null;
28    /**
29     * list of processors
30     *
31     * @var  array<string,string>
32     */
33    protected $processors             = array();
34    /**
35     * list of interceptor descriptors
36     *
37     * @var  array<string,string>
38     */
39    protected $interceptorDescriptors = array();
40    /**
41     * list of page factory classes
42     *
43     * @var  array<string,string>
44     */
45    protected $pageFactoryClasses     = array();
46
47    /**
48     * adds a processor to the list of available processors
49     *
50     * @param  string  $paramValue             value of the request parameter that identifies this processor
51     * @param  string  $fqClassName            full qualified class name of the processor
52     * @param  string  $interceptorDescriptor  optional  the interceptor descriptor
53     * @param  string  $pageFactoryClass       optional  page factory class for the processor
54     */
55    public function addProcessor($paramValue, $fqClassName, $interceptorDescriptor = 'interceptors', $pageFactoryClass = null)
56    {
57        $this->processors[$paramValue]              = $fqClassName;
58        $this->interceptorDescriptors[$fqClassName] = $interceptorDescriptor;
59        if (null !== $pageFactoryClass) {
60            $this->pageFactoryClasses[$fqClassName] = $pageFactoryClass;
61        }
62    }
63
64    /**
65     * sets the name of the default processor
66     *
67     * @param  string  $defaultProcessor  value of the request parameter that identifies this processor
68     */
69    public function setDefaultProcessor($defaultProcessor)
70    {
71        $this->defaultProcessor = $defaultProcessor;
72    }
73
74    /**
75     * does the real resolving work
76     *
77     * @param   stubRequest   $request   the current request
78     * @param   stubSession   $session   the current session
79     * @param   stubResponse  $response  the current response
80     * @return  string        full qualified classname of the processor to create
81     * @throws  stubConfigurationException
82     */
83    protected function doResolve(stubRequest $request, stubSession $session, stubResponse $response)
84    {
85        if (isset($this->processors[$this->defaultProcessor]) == false) {
86            throw new stubConfigurationException('Configuration error: the default processor ' . $this->defaultProcessor . ' is not set.');
87        }
88       
89        if ($request->hasValue('processor') == false) {
90            $paramValue = $this->defaultProcessor;
91        } else {
92            $paramValue = $request->getValidatedValue(new stubPreSelectValidator(array_keys($this->processors)), 'processor');
93            if (null == $paramValue) {
94                $paramValue = $this->defaultProcessor;
95            }
96        }
97       
98        $session->putValue('net::stubbles::websites.lastProcessor', $paramValue);
99        return $this->processors[$paramValue];
100    }
101
102    /**
103     * configures the processor
104     *
105     * @param  stubProcessor  $processor
106     */
107    protected function configure(stubProcessor $processor)
108    {
109        if (isset($this->interceptorDescriptors[$processor->getClassName()]) == true && strlen($this->interceptorDescriptors[$processor->getClassName()]) > 0) {
110            $processor->setInterceptorDescriptor($this->interceptorDescriptors[$processor->getClassName()]);
111        }
112    }
113
114    /**
115     * returns the page factory class for the processor
116     *
117     * @param   stubProcessor  $processor
118     * @return  string
119     */
120    protected function getPageFactoryClass(stubProcessor $processor)
121    {
122        if (isset($this->pageFactoryClasses[$processor->getClassName()]) === true) {
123            return $this->pageFactoryClasses[$processor->getClassName()];
124        }
125       
126        return null;
127    }
128}
129?>
Note: See TracBrowser for help on using the browser.