Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * sfComponent.
13
 *
14
 * @package    symfony
15
 * @subpackage action
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfComponent.class.php 23922 2009-11-14 14:58:38Z fabien $
18
 */
19
abstract class sfComponent
20
{
21
  protected
22
    $moduleName             = '',
23
    $actionName             = '',
24
    $context                = null,
25
    $dispatcher             = null,
26
    $request                = null,
27
    $response               = null,
28
    $varHolder              = null,
29
    $requestParameterHolder = null;
30
 
31
  /**
32
   * Class constructor.
33
   *
34
   * @see initialize()
35
   */
36
  public function __construct($context, $moduleName, $actionName)
37
  {
38
    $this->initialize($context, $moduleName, $actionName);
39
  }
40
 
41
  /**
42
   * Initializes this component.
43
   *
44
   * @param sfContext $context    The current application context.
45
   * @param string    $moduleName The module name.
46
   * @param string    $actionName The action name.
47
   *
48
   * @return boolean true, if initialization completes successfully, otherwise false
49
   */
50
  public function initialize($context, $moduleName, $actionName)
51
  {
52
    $this->moduleName             = $moduleName;
53
    $this->actionName             = $actionName;
54
    $this->context                = $context;
55
    $this->dispatcher             = $context->getEventDispatcher();
56
    $this->varHolder              = new sfParameterHolder();
57
    $this->request                = $context->getRequest();
58
    $this->response               = $context->getResponse();
59
    $this->requestParameterHolder = $this->request->getParameterHolder();
60
  }
61
 
62
  /**
63
   * Execute any application/business logic for this component.
64
   *
65
   * In a typical database-driven application, execute() handles application
66
   * logic itself and then proceeds to create a model instance. Once the model
67
   * instance is initialized it handles all business logic for the action.
68
   *
69
   * A model should represent an entity in your application. This could be a
70
   * user account, a shopping cart, or even a something as simple as a
71
   * single product.
72
   *
73
   * @param sfRequest $request The current sfRequest object
74
   *
75
   * @return mixed     A string containing the view name associated with this action
76
   */
77
  abstract function execute($request);
78
 
79
  /**
80
   * Gets the module name associated with this component.
81
   *
82
   * @return string A module name
83
   */
84
  public function getModuleName()
85
  {
86
    return $this->moduleName;
87
  }
88
 
89
  /**
90
   * Gets the action name associated with this component.
91
   *
92
   * @return string An action name
93
   */
94
  public function getActionName()
95
  {
96
    return $this->actionName;
97
  }
98
 
99
  /**
100
   * Retrieves the current application context.
101
   *
102
   * @return sfContext The current sfContext instance
103
   */
104
  public final function getContext()
105
  {
106
    return $this->context;
107
  }
108
 
109
  /**
110
   * Retrieves the current logger instance.
111
   *
112
   * @return sfLogger The current sfLogger instance
113
   */
114
  public final function getLogger()
115
  {
116
    return $this->context->getLogger();
117
  }
118
 
119
  /**
120
   * Logs a message using the sfLogger object.
121
   *
122
   * @param mixed  $message  String or object containing the message to log
123
   * @param string $priority The priority of the message
124
   *                         (available priorities: emerg, alert, crit, err,
125
   *                         warning, notice, info, debug)
126
   *
127
   * @see sfLogger
128
   */
129
  public function logMessage($message, $priority = 'info')
130
  {
131
    if (sfConfig::get('sf_logging_enabled'))
132
    {
133
      $this->dispatcher->notify(new sfEvent($this, 'application.log', array($message, 'priority' => constant('sfLogger::'.strtoupper($priority)))));
134
    }
135
  }
136
 
137
  /**
138
   * Returns the value of a request parameter.
139
   *
140
   * This is a proxy method equivalent to:
141
   *
142
   * <code>$this->getRequest()->getParameterHolder()->get($name)</code>
143
   *
144
   * @param string $name    The parameter name
145
   * @param mixed  $default The default value if parameter does not exist
146
   *
147
   * @return string The request parameter value
148
   */
149
  public function getRequestParameter($name, $default = null)
150
  {
151
    return $this->requestParameterHolder->get($name, $default);
152
  }
153
 
154
  /**
155
   * Returns true if a request parameter exists.
156
   *
157
   * This is a proxy method equivalent to:
158
   *
159
   * <code>$this->getRequest()->getParameterHolder()->has($name)</code>
160
   *
161
   * @param string $name The parameter name
162
   * @return boolean true if the request parameter exists, false otherwise
163
   */
164
  public function hasRequestParameter($name)
165
  {
166
    return $this->requestParameterHolder->has($name);
167
  }
168
 
169
  /**
170
   * Retrieves the current sfRequest object.
171
   *
172
   * This is a proxy method equivalent to:
173
   *
174
   * <code>$this->getContext()->getRequest()</code>
175
   *
176
   * @return sfRequest The current sfRequest implementation instance
177
   */
178
  public function getRequest()
179
  {
180
    return $this->request;
181
  }
182
 
183
  /**
184
   * Retrieves the current sfResponse object.
185
   *
186
   * This is a proxy method equivalent to:
187
   *
188
   * <code>$this->getContext()->getResponse()</code>
189
   *
190
   * @return sfResponse The current sfResponse implementation instance
191
   */
192
  public function getResponse()
193
  {
194
    return $this->response;
195
  }
196
 
197
  /**
198
   * Retrieves the current sfController object.
199
   *
200
   * This is a proxy method equivalent to:
201
   *
202
   * <code>$this->getContext()->getController()</code>
203
   *
204
   * @return sfController The current sfController implementation instance
205
   */
206
  public function getController()
207
  {
208
    return $this->context->getController();
209
  }
210
 
211
  /**
212
   * Generates a URL for the given route and arguments.
213
   *
214
   * This is a proxy method equivalent to:
215
   *
216
   * <code>$this->getContext()->getRouting()->generate(...)</code>
217
   *
218
   * @param string  The route name
219
   * @param array   An array of parameters for the route
220
   * @param Boolean Whether to generate an absolute URL or not
221
   *
222
   * @return string  The URL
223
   */
224
  public function generateUrl($route, $params = array(), $absolute = false)
225
  {
226
    return $this->context->getRouting()->generate($route, $params, $absolute);
227
  }
228
 
229
  /**
230
   * Retrieves the current sfUser object.
231
   *
232
   * This is a proxy method equivalent to:
233
   *
234
   * <code>$this->getContext()->getUser()</code>
235
   *
236
   * @return sfUser The current sfUser implementation instance
237
   */
238
  public function getUser()
239
  {
240
    return $this->context->getUser();
241
  }
242
 
243
  /**
244
   * Gets the current mailer instance.
245
   *
246
   * @return sfMailer A sfMailer instance
247
   */
248
  public function getMailer()
249
  {
250
    return $this->getContext()->getMailer();
251
  }
252
 
253
  /**
254
   * Sets a variable for the template.
255
   *
256
   * If you add a safe value, the variable won't be output escaped
257
   * by symfony, so this is your responsability to ensure that the
258
   * value is escaped properly.
259
   *
260
   * @param string  $name  The variable name
261
   * @param mixed   $value The variable value
262
   * @param Boolean $safe  true if the value is safe for output (false by default)
263
   */
264
  public function setVar($name, $value, $safe = false)
265
  {
266
    $this->varHolder->set($name, $safe ? new sfOutputEscaperSafe($value) : $value);
267
  }
268
 
269
  /**
270
   * Gets a variable set for the template.
271
   *
272
   * @param string $name The variable name
273
   *
274
   * @return mixed  The variable value
275
   */
276
  public function getVar($name)
277
  {
278
    return $this->varHolder->get($name);
279
  }
280
 
281
  /**
282
   * Gets the sfParameterHolder object that stores the template variables.
283
   *
284
   * @return sfParameterHolder The variable holder.
285
   */
286
  public function getVarHolder()
287
  {
288
    return $this->varHolder;
289
  }
290
 
291
  /**
292
   * Sets a variable for the template.
293
   *
294
   * This is a shortcut for:
295
   *
296
   * <code>$this->setVar('name', 'value')</code>
297
   *
298
   * @param string $key   The variable name
299
   * @param string $value The variable value
300
   *
301
   * @return boolean always true
302
   *
303
   * @see setVar()
304
   */
305
  public function __set($key, $value)
306
  {
307
    return $this->varHolder->setByRef($key, $value);
308
  }
309
 
310
  /**
311
   * Gets a variable for the template.
312
   *
313
   * This is a shortcut for:
314
   *
315
   * <code>$this->getVar('name')</code>
316
   *
317
   * @param string $key The variable name
318
   *
319
   * @return mixed The variable value
320
   *
321
   * @see getVar()
322
   */
323
  public function & __get($key)
324
  {
325
    return $this->varHolder->get($key);
326
  }
327
 
328
  /**
329
   * Returns true if a variable for the template is set.
330
   *
331
   * This is a shortcut for:
332
   *
333
   * <code>$this->getVarHolder()->has('name')</code>
334
   *
335
   * @param string $name The variable name
336
   *
337
   * @return boolean true if the variable is set
338
   */
339
  public function __isset($name)
340
  {
341
    return $this->varHolder->has($name);
342
  }
343
 
344
  /**
345
   * Removes a variable for the template.
346
   *
347
   * This is just really a shortcut for:
348
   *
349
   * <code>$this->getVarHolder()->remove('name')</code>
350
   *
351
   * @param string $name The variable Name
352
   */
353
  public function __unset($name)
354
  {
355
    $this->varHolder->remove($name);
356
  }
357
 
358
  /**
359
   * Calls methods defined via sfEventDispatcher.
360
   *
361
   * @param string $method The method name
362
   * @param array  $arguments The method arguments
363
   *
364
   * @return mixed The returned value of the called method
365
   *
366
   * @throws sfException If called method is undefined
367
   */
368
  public function __call($method, $arguments)
369
  {
370
    $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'component.method_not_found', array('method' => $method, 'arguments' => $arguments)));
371
    if (!$event->isProcessed())
372
    {
373
      throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
374
    }
375
 
376
    return $event->getReturnValue();
377
  }
378
}