Subversion Repositories PHPX

Rev

Rev 29 | Rev 34 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 PointedEar 1
<?php
2
 
29 PointedEar 3
require_once __DIR__ . '/AbstractModel.php';
4
require_once __DIR__ . '/Registry.php';
27 PointedEar 5
 
6
/**
7
 * Basic application class
8
 *
9
 * @author Thomas Lahn
10
 */
11
class Application
12
{
13
  /**
14
   * Relative path to the controllers directory
15
   * @var string
16
   */
17
  protected $_controllerPath = 'application/controllers';
18
 
19
  /**
20
   * Default controller of the application
21
   * @var string
22
   */
23
  protected $_defaultController = 'Index';
24
 
25
  /**
26
   * Registry key for the default database of the application
27
   * @var string
28
   */
29
  protected $_defaultDatabase;
30
 
31
  /**
32
   * Currently active controller of this application
33
   * @var Controller
34
   */
35
  protected $_currentController;
36
 
37
  /**
38
   * Singleton
39
   *
40
   * @var Application
41
   */
42
  private static $_instance;
43
 
44
  protected function __construct()
45
  {
46
    /* Singleton pattern */
47
  }
48
 
49
  /**
50
   * Gets a reference to the <code>Application</code> instance
51
   *
52
   * @param Application $instance
53
   *   The instance to be used as application.  The default is a new
54
   *   application.  This parameter is ignored if the application was
55
   *   already initialized.
56
   * @return Application
57
   */
58
  public static function getInstance(Application $instance = null)
59
  {
31 PointedEar 60
    if (self::$_instance === null)
27 PointedEar 61
    {
62
      self::$_instance = ($instance === null) ? new self() : $instance;
63
    }
64
 
65
    return self::$_instance;
66
  }
67
 
31 PointedEar 68
  /**
69
   * Getter for properties
70
   *
71
   * @param string $name
72
   * @throws ModelPropertyException
73
   * @return mixed
74
   */
75
  public function __get($name)
76
  {
77
    /* Support for Object-Relational Mappers */
78
    if (strpos($name, 'persistent') === 0)
79
    {
80
      $class = get_class($this);
81
      return $class::${
82
        $name};
83
    }
27 PointedEar 84
 
31 PointedEar 85
    $method = 'get' . ucfirst($name);
86
 
87
    if (method_exists($this, $method))
88
    {
89
      return $this->$method();
90
    }
91
 
92
    if (property_exists($this, "_$name"))
93
    {
94
      return $this->{"_$name"};
95
    }
96
 
97
    return $this->$name;
98
  }
99
 
27 PointedEar 100
  /**
31 PointedEar 101
   * Setter for properties
102
   *
103
   * @param string $name
104
   * @param mixed $value  The new property value before assignment
105
   * @throws ModelPropertyException
106
   */
107
  public function __set($name, $value)
108
  {
109
    $method = 'set' . ucfirst($name);
110
 
111
    if (method_exists($this, $method))
112
    {
113
      return $this->$method($value);
114
    }
115
 
116
    if (property_exists($this, "_$name"))
117
    {
118
      $this->{"_$name"} = $value;
119
      return $this->{"_$name"};
120
    }
121
 
122
    /* NOTE: Attempts to set other properties are _silently_ _ignored_ */
123
  }
124
 
125
  /**
27 PointedEar 126
   * Runs the application, setting up session management and
127
   * constructing the controller indicated by the URI
128
   */
129
  public function run()
130
  {
131
    $this->startSession();
132
 
133
    $controller = self::getParam('controller', $_REQUEST);
134
    if (!$controller)
135
    {
136
      $controller = $this->_defaultController;
137
    }
138
 
139
    $controller = ucfirst($controller);
140
 
141
    $controller = $controller . 'Controller';
142
    require_once "{$this->_controllerPath}/{$controller}.php";
143
    $this->_currentController = new $controller();
144
 
145
    return $this;
146
  }
147
 
148
  protected function startSession()
149
  {
150
    session_start();
151
  }
152
 
153
  /**
154
   * Gets a request parameter
155
   *
156
   * @param string $key
157
   *   Key to look up in the array
158
   * @param array $array
159
   *   Array where to look up <var>$key</var>.
160
   *   The default is <code>$_GET</code>.
161
   * @return mixed
162
   *   <code>null</code> if there is no such <var>$key</var>
163
   *   in <var>$array</var>
164
   */
165
  public static function getParam($key, array $array = null)
166
  {
31 PointedEar 167
    if ($array === null)
27 PointedEar 168
    {
169
      $array = $_GET;
170
    }
171
 
172
    return isset($array[$key]) ? $array[$key] : null;
173
  }
174
 
175
  /**
176
   * Registers a database
177
   *
178
   * @param string $key
179
   * @param Database $database
180
   */
181
  public function registerDatabase($key, Database $database)
182
  {
183
    Registry::set($key, $database);
184
  }
185
 
186
  /**
187
   * Sets the default database
188
   * @param key Registry key to refer to the {@link Database}
189
   */
190
  public function setDefaultDatabase($key)
191
  {
192
    $this->_defaultDatabase = $key;
193
  }
194
 
195
  /**
196
  * Sets the current controller for this application
197
  *
198
  * @param Controller $controller
199
  * @return Application
200
  */
201
  public function setCurrentController(Controller $controller)
202
  {
203
    $this->_currentController = $controller;
204
    return $this;
205
  }
206
 
207
  /**
208
   * Returns the current controller for this application
209
   *
210
   * @return Controller
211
   */
212
  public function getCurrentController()
213
  {
214
    return $this->_currentController;
215
  }
216
 
217
  /**
218
   * Returns the default database for this application
219
   *
220
   * @return Database
221
   */
222
  public function getDefaultDatabase()
223
  {
224
    return Registry::get($this->_defaultDatabase);
225
  }
226
 
227
  /**
228
   * Returns a relative URI reference for an action of the
229
   * application
230
   *
231
   * @param string[optional] $controller
232
   * @param string[optional] $action
233
   * @param int[optional] $id
234
   */
235
  public function getURL($controller = null, $action = null, $id = null)
236
  {
237
    /* Apache module */
238
    $url = self::getParam('SCRIPT_URL', $_SERVER);
239
    if ($url === null)
240
    {
241
      /* FastCGI */
242
      $url = self::getParam('URL', $_SERVER);
243
      if ($url === null)
244
      {
31 PointedEar 245
        /* Server/PHP too old, compute URI */
246
        $url = self::getParam('REQUEST_URI', $_SERVER);
247
        if (preg_match('/^[^?]+/', $url, $matches) > 0)
248
        {
249
          $url = $matches[0];
250
        }
251
        else
252
        {
253
          /* Has .php in it, but at least it works */
254
          $url = self::getParam('SCRIPT_NAME', $_SERVER);
255
          if ($url === null)
256
          {
257
            throw new Exception(
258
              'None of $_SERVER["SCRIPT_URL"], $_SERVER["URL"],'
259
              . ' $_SERVER["REQUEST_URI"], or $_SERVER["SCRIPT_NAME"]'
260
              . ' is available, cannot continue.');
261
          }
262
        }
27 PointedEar 263
      }
264
    }
265
 
31 PointedEar 266
    $query = (($controller !== null) ? 'controller=' . $controller : '')
267
           . (($action !== null) ? '&action=' . $action : '')
268
           . (($id !== null) ? '&id=' . $id : '');
27 PointedEar 269
 
270
    return $url . ($query ? '?' . $query : '');
271
  }
272
 
273
  /**
274
   * Performs a server-side redirect within the application
275
   */
276
  public static function redirect($query = '')
277
  {
278
    $script_uri = self::getParam('SCRIPT_URI', $_SERVER);
31 PointedEar 279
    if ($script_uri === null)
27 PointedEar 280
    {
281
      /* Server/PHP too old, compute URI */
282
      if (preg_match('/^[^?]+/',
283
          self::getParam('REQUEST_URI', $_SERVER), $matches) > 0)
284
      {
285
        $query_prefix = $matches[0];
286
      }
287
      else
288
      {
289
        /* Has .php in it, but at least it works */
290
        $query_prefix = self::getParam('SCRIPT_NAME', $_SERVER);
291
      }
292
 
293
      /* TODO: Let user decide which ports map to which URI scheme */
294
      $script_uri = (self::getParam('SERVER_PORT', $_SERVER) == 443
295
                      ? 'https://'
296
                      : 'http://')
297
                  . self::getParam('HTTP_HOST', $_SERVER)
298
                  . $query_prefix;
299
    }
300
 
301
    header('Location: ' . $script_uri
302
      . ($query ? (substr($query, 0, 1) === '?' ? '' : '?') . $query : ''));
303
  }
304
}