root/trunk/src/main/php/net/stubbles/websites/stubFrontController.php @ 1464

Revision 1464, 5.3 kB (checked in by mikey, 2 years ago)

cover wrong configuration cases

Line 
1<?php
2/**
3 * The front controller for websites.
4 *
5 * @author      Frank Kleine <mikey@stubbles.net>
6 * @package     stubbles
7 * @subpackage  websites
8 */
9stubClassLoader::load('net::stubbles::ipo::request::stubRequest',
10                      'net::stubbles::ipo::response::stubBaseResponse',
11                      'net::stubbles::ipo::session::stubSession',
12                      'net::stubbles::util::stubRegistry',
13                      'net::stubbles::websites::stubWebsiteInitializer',
14                      'net::stubbles::websites::cache::stubWebsiteCacheFactory'
15);
16/**
17 * The front controller for websites.
18 *
19 * @package     stubbles
20 * @subpackage  websites
21 */
22class stubFrontController extends stubBaseObject
23{
24    /**
25     * initializer
26     *
27     * @var  stubWebsiteInitializer
28     */
29    protected $websiteInitializer;
30    /**
31     * contains request data
32     *
33     * @var  stubRequest
34     */
35    protected $request;
36    /**
37     * session container
38     *
39     * @var  stubSession
40     */
41    protected $session;
42    /**
43     * response container
44     *
45     * @var  stubResponse
46     */
47    protected $response;
48    /**
49     * factory for the website cache
50     *
51     * @var  stubWebsiteCacheFactory
52     */
53    protected $websiteCacheFactory;
54
55    /**
56     * constructor
57     *
58     * @param  stubWebsiteInitializer  $websiteInitializer  initializer to init basic stuff
59     */
60    public function __construct(stubWebsiteInitializer $websiteInitializer)
61    {
62        $websiteInitializer->init();
63        $websiteInitializer->getRegistryInitializer()->init();
64        if ($websiteInitializer->hasGeneralInitializer() === true) {
65            $websiteInitializer->getGeneralInitializer()->init();
66        }
67       
68        $this->websiteInitializer = $websiteInitializer;
69        $this->createInstances();
70    }
71
72    /**
73     * creates the required instances
74     *
75     * @throws  stubRuntimeException
76     */
77    protected function createInstances()
78    {
79        $fqClassName = stubRegistry::getConfig(stubRequest::CLASS_REGISTRY_KEY, 'net::stubbles::ipo::request::stubWebRequest');
80        $className   = stubClassLoader::getNonQualifiedClassName($fqClassName);
81        if (class_exists($className, false) === false) {
82            stubClassLoader::load($fqClassName);
83        }
84       
85        $this->request = new $className();
86        if (($this->request instanceof stubRequest) === false) {
87            throw new stubRuntimeException('Configured request class is not an instance of net::stubbles::ipo::request::stubRequest.');
88        }
89
90        $fqClassName = stubRegistry::getConfig(stubSession::CLASS_REGISTRY_KEY, 'net::stubbles::ipo::session::stubPHPSession');
91        $className   = stubClassLoader::getNonQualifiedClassName($fqClassName);
92        if (class_exists($className, false) === false) {
93            stubClassLoader::load($fqClassName);
94        }
95       
96        $this->session = new $className($this->request, stubRegistry::getConfig(stubSession::NAME_REGISTRY_KEY, stubSession::DEFAULT_SESSION_NAME));
97        if (($this->session instanceof stubSession) === false) {
98            throw new stubRuntimeException('Configured session class is not an instance of net::stubbles::ipo::session::stubSession.');
99        }
100       
101        $this->response = new stubBaseResponse();
102    }
103
104    /**
105     * sets the website cache factory to be used
106     *
107     * @param  stubWebsiteCacheFactory  $websiteCacheFactory
108     */
109    public function setWebsiteCacheFactory(stubWebsiteCacheFactory $websiteCacheFactory)
110    {
111        $this->websiteCacheFactory = $websiteCacheFactory;
112    }
113
114    /**
115     * does the whole processing
116     */
117    public function process()
118    {
119        $processorResolverFactory = $this->websiteInitializer->getProcessorResolverFactory();
120        $processorResolverFactory->init();
121        $processor = $processorResolverFactory->getResolver()->resolve($this->request, $this->session, $this->response);
122        if ($processor->forceSSL() === true && $processor->isSSL() === false) {
123            $this->response->addHeader('Location', 'https://' . $this->request->getURI());
124            $this->request->cancel();
125            $this->response->send();
126            return;
127        }
128       
129        $interceptorInitializer = $this->websiteInitializer->getInterceptorInitializer();
130        $interceptorInitializer->setDescriptor($processor->getInterceptorDescriptor());
131        $interceptorInitializer->init();
132        foreach ($interceptorInitializer->getPreInterceptors() as $preInterceptor) {
133            $preInterceptor->preProcess($this->request, $this->session, $this->response);
134            if ($this->request->isCancelled() === true) {
135                $this->response->send();
136                return;
137            }
138        }
139       
140        if (null !== $this->websiteCacheFactory) {
141            $processor = $this->websiteCacheFactory->configure($processor);
142        }
143       
144        $processor->process();
145        if ($this->request->isCancelled() === false) {
146            foreach ($interceptorInitializer->getPostInterceptors() as $postInterceptor) {
147                $postInterceptor->postProcess($this->request, $this->session, $this->response);
148                if ($this->request->isCancelled() === true) {
149                    break;
150                }
151            }
152        }
153       
154        $this->response->send();
155    }
156}
157?>
Note: See TracBrowser for help on using the browser.