1: <?php
2: /**
3: * Factory methods for basic objects required by the export.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_FreeBusy
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_FreeBusy
12: */
13:
14: /**
15: * Factory methods for basic objects required by the export.
16: *
17: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see
21: * http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_FreeBusy
25: * @author Gunnar Wrobel <wrobel@pardus.de>
26: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
27: * @link http://pear.horde.org/index.php?package=Kolab_FreeBusy
28: */
29: class Horde_Kolab_FreeBusy_Factory_Base
30: implements Horde_Kolab_FreeBusy_Factory
31: {
32: /**
33: * The injector providing required dependencies.
34: *
35: * @var Horde_Injector
36: */
37: protected $_injector;
38:
39: /**
40: * Constructor.
41: *
42: * @param Horde_Injector $injector The injector providing required
43: * dependencies.
44: */
45: public function __construct(Horde_Injector $injector)
46: {
47: $this->_injector = $injector;
48: }
49:
50: /**
51: * Create the object representing the current request.
52: *
53: * @return Horde_Controller_Request The current request.
54: *
55: * @throws Horde_Exception
56: */
57: public function createRequest()
58: {
59: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
60: $params = isset($configuration['request']) ? $configuration['request'] : array();
61: if (!empty($params['class'])) {
62: $request_class = $params['class'];
63: } else {
64: $request_class = 'Horde_Controller_Request_Http';
65: }
66:
67: if (!empty($params['params'])) {
68: $request_params = $params['params'];
69: } else {
70: $request_params = array();
71: }
72:
73: return new $request_class($request_params);
74: }
75:
76: /**
77: * Create the instance that will output the response.
78: *
79: * @return Horde_Controller_ResponseWriter The response writer.
80: *
81: * @throws Horde_Exception
82: */
83: public function createResponseWriter()
84: {
85: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
86: $params = isset($configuration['writer']) ? $configuration['writer'] : array();
87: if (!empty($params['class'])) {
88: $writer_class = $params['class'];
89: } else {
90: $writer_class = 'Horde_Controller_ResponseWriter_Web';
91: }
92: return new $writer_class();
93: }
94:
95: /**
96: * Create the view object.
97: *
98: * @return Horde_View The view helper.
99: */
100: public function createView()
101: {
102: $view = new Horde_View();
103: $view->addHelper('Tag');
104: $view->addHelper('Text');
105: return $view;
106: }
107:
108: /**
109: * Return the logger.
110: *
111: * @return Horde_Log_Logger The logger.
112: */
113: public function createLogger()
114: {
115: $logger = new Horde_Log_Logger();
116:
117: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
118: $logger_params = isset($configuration['logger']) ? $configuration['logger'] : array();
119:
120: if (empty($params)) {
121: $handlers = array('Horde_Log_Handler_Null' => array());
122: } else {
123: $handlers = $logger_params['logger'];
124: }
125:
126: foreach ($handlers as $name => $params) {
127: if (!empty($params['params'])) {
128: /**
129: * We need to pass parameters to the constructor so use
130: * reflection.
131: */
132: $reflectionObj = new ReflectionClass($name);
133: $handler = $reflectionObj->newInstanceArgs($params['params']);
134: } else {
135: $handler = new $name();
136: }
137:
138: if (!empty($params['options'])) {
139: foreach ($params['options'] as $key => $value) {
140: $handler->setOption($key, $value);
141: }
142: }
143:
144: $logger->addHandler($handler);
145: }
146: return $logger;
147: }
148:
149: /**
150: * Create the mapper.
151: *
152: * @return Horde_Route_Mapper The mapper.
153: */
154: public function createMapper()
155: {
156: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
157: $params = isset($configuration['mapper']) ? $configuration['mapper'] : array();
158: if (!empty($params['params'])) {
159: $mapper_params = $params['params'];
160: } else {
161: $mapper_params = array();
162: }
163: $mapper = new Horde_Routes_Mapper($mapper_params);
164:
165: /**
166: * Application routes are relative only to the application. Let the
167: * mapper know where they start.
168: */
169: if (!empty($configuration['script'])) {
170: $mapper->prefix = dirname($configuration['script']);
171: } else {
172: $mapper->prefix = dirname($_SERVER['PHP_SELF']);
173: }
174:
175: return $mapper;
176: }
177:
178: /**
179: * Create the request configuration.
180: *
181: * @return Horde_Controller_RequestConfiguration The request configuration.
182: */
183: public function createRequestConfiguration()
184: {
185: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
186: if (isset($configuration['request_config']['prefix'])) {
187: $prefix = $configuration['request_config']['prefix'];
188: } else {
189: $prefix = $this->getControllerPrefix();
190: }
191:
192: $match = $this->_injector->getInstance(
193: 'Horde_Kolab_FreeBusy_Controller_MatchDict'
194: )->getMatchDict();
195: if (empty($match['controller']) ||
196: !class_exists($prefix . ucfirst($match['controller']))) {
197: $controller = 'Horde_Kolab_FreeBusy_Controller_NotFound';
198: } else {
199: $controller = $prefix . ucfirst($match['controller']);
200: }
201:
202: $conf = new Horde_Kolab_FreeBusy_Controller_RequestConfiguration();
203: $conf->setControllerName($controller);
204: return $conf;
205: }
206:
207: /**
208: * Return the class name prefix for controllers.
209: *
210: * @return string The prefix.
211: */
212: protected function getControllerPrefix()
213: {
214: return 'Horde_Kolab_FreeBusy_Controller_';
215: }
216:
217: /**
218: * Create the user representation.
219: *
220: * @return Horde_Kolab_FreeBusy_User The user.
221: */
222: public function createUser()
223: {
224: list($user, $pass) = $this->_injector->getInstance(
225: 'Horde_Kolab_FreeBusy_Params_User'
226: )->getCredentials();
227: return $this->_injector->getInstance('Horde_Kolab_FreeBusy_UserDb')
228: ->getUser(
229: $user, $pass
230: );
231: }
232:
233: /**
234: * Create the owner representation.
235: *
236: * @return Horde_Kolab_FreeBusy_Owner The owner.
237: */
238: public function createOwner()
239: {
240: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
241: $params = isset($configuration['owner']) ? $configuration['owner'] : array();
242:
243: $params['user'] = $this->_injector->getInstance(
244: 'Horde_Kolab_FreeBusy_User'
245: );
246: return $this->_injector->getInstance('Horde_Kolab_FreeBusy_UserDb')
247: ->getOwner(
248: $this->_injector->getInstance(
249: 'Horde_Kolab_FreeBusy_Params_Owner'
250: )->getOwner(),
251: $params
252: );
253: }
254:
255: /**
256: * Create the data provider.
257: *
258: * @return Horde_Kolab_FreeBusy_Provider The provider.
259: */
260: public function createProvider()
261: {
262: $configuration = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Configuration');
263: $params = isset($configuration['provider']) ? $configuration['provider'] : array();
264:
265: if (!isset($params['server'])) {
266: $params['server'] = 'https://localhost/export';
267: }
268:
269: $owner_fb = $this->_injector->getInstance('Horde_Kolab_FreeBusy_Owner')
270: ->getRemoteServer();
271: if (!empty($owner_fb) && $owner_fb != $params['server']) {
272: $this->_injector->getInstance('Horde_Log_Logger')->info(
273: sprintf(
274: "URL \"%s\" indicates remote free/busy server since we only offer \"%s\". Redirecting.",
275: $owner_fb,
276: $params['server']
277: )
278: );
279: if (empty($params['redirect'])) {
280: return $this->_injector->getInstance(
281: 'Horde_Kolab_FreeBusy_Provider_Remote_PassThrough'
282: );
283: } else {
284: return $this->_injector->getInstance(
285: 'Horde_Kolab_FreeBusy_Provider_Remote_Redirect'
286: );
287: }
288: } else {
289: return $this->_injector->getInstance(
290: 'Horde_Kolab_FreeBusy_Provider_Local'
291: );
292: }
293: }
294: }
295: