-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiteAccess.php
143 lines (117 loc) · 3.52 KB
/
SiteAccess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace BaseXMS;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\EventManager\EventManager;
use Zend\Http\PhpEnvironment\Response as ZendResponse;
use Zend\Config\Config;
use BaseXMS\BaseX as BaseXDb;
use BaseXMS\RequestHandler\RequestHandler;
class SiteAccess
{
protected $baseXMSServices;
protected $config;
public function __construct()
{
if( ! defined( 'BASEXMS_START' ) )
{
define( 'BASEXMS_START', microtime( true ) );
}
}
public function init( ServiceLocatorInterface $serviceLocator )
{
$application = $serviceLocator->get( 'application' );
// Add siteaccess specific config
$configHandler = new Config( $application->getConfig() );
$configHandler->merge( $this->addConfig() );
//TODO: avoid config in context of the siteaccess
$this->config = $configHandler->toArray();
$application->getServiceManager()->setService( 'accumulator', new Accumulator() );
$application->getServiceManager()->setFactory( 'xmldb', 'BaseXMS\BaseXFactory' );
//TODO: remove all references to baseXMSServices
$this->baseXMSServices = $application->getServiceManager();
// not needed I think
#$this->baseXMSServices->setService( 'siteaccess', $this );
}
public function getBaseXMSServices()
{
return $this->baseXMSServices;
}
/*
* building a Zend response object
* TODO: consider to move more lgic inside the RequestHandler factory
*/
public function getResponse( ServiceLocatorInterface $serviceLocator, $path )
{
$serviceLocator->get( 'log' )->info( 'Build Response' );
$urlDispatcher = new UrlDispatcher();
$urlDispatcher->setServiceLocator( $serviceLocator );
$baseXMSResponse = $urlDispatcher->dispatch( $path );
$response = new ZendResponse();
switch( (string) $baseXMSResponse->code )
{
case 200:
{
$requestHandler = RequestHandler::factory
(
(string) $baseXMSResponse->id,
(string) $baseXMSResponse->contentclass,
$this
);
// override response
$response = $requestHandler->getResponse();
}
break;
case 301:
{
if( true ) // redirect debug case
{
$response->setContent( 'Redirect to <a href="' . (string) $baseXMSResponse->path . '">' . (string) $baseXMSResponse->path . '</a>');
$response->setStatusCode( 200 );
}
else
{
$response->setContent( 'Redirect' );
$response->setStatusCode( 301 );
$headers = Headers::fromString( 'Location: ' . (string) $baseXMSResponse->path );
$response->setHeaders( $headers );
}
}
break;
case 500:
{
$response->setContent( '500 - Server Error.' );
$response->setStatusCode( (string) $baseXMSResponse->code );
}
break;
case 400:
{
$response->setContent( '404 - Not found.' );
$response->setStatusCode( (string) $baseXMSResponse->code );
}
break;
default:
{
$response->setContent( 'Unhandled return code.' );
$response->setStatusCode( 500 );
}
}
$response->setReasonPhrase( $response->getReasonPhrase() ); // set default phrase
$serviceLocator->get( 'accumulator' )->start( 'BaseXMS', BASEXMS_START );
$serviceLocator->get( 'accumulator' )->stop( 'BaseXMS' );
return $response;
}
public function getConfig()
{
return $this->config;
}
/**
* Add siteaccess related config. It overrides/adds to the module config.
*
* @return \Zend\Config\Config
*/
protected function addConfig()
{
return new Config( array() );
}
}