Subversion Repositories PHPX

Rev

Rev 34 | Rev 51 | 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);
34 PointedEar 81
      return $class::${$name};
31 PointedEar 82
    }
27 PointedEar 83
 
31 PointedEar 84
    $method = 'get' . ucfirst($name);
85
 
86
    if (method_exists($this, $method))
87
    {
88
      return $this->$method();
89
    }
90
 
91
    if (property_exists($this, "_$name"))
92
    {
93
      return $this->{"_$name"};
94
    }
95
 
96
    return $this->$name;
97
  }
98
 
27 PointedEar 99
  /**
31 PointedEar 100
   * Setter for properties
101
   *
102
   * @param string $name
103
   * @param mixed $value  The new property value before assignment
104
   * @throws ModelPropertyException
105
   */
106
  public function __set($name, $value)
107
  {
108
    $method = 'set' . ucfirst($name);
109
 
110
    if (method_exists($this, $method))
111
    {
112
      return $this->$method($value);
113
    }
114
 
115
    if (property_exists($this, "_$name"))
116
    {
117
      $this->{"_$name"} = $value;
118
      return $this->{"_$name"};
119
    }
120
 
121
    /* NOTE: Attempts to set other properties are _silently_ _ignored_ */
122
  }
123
 
124
  /**
27 PointedEar 125
   * Runs the application, setting up session management and
126
   * constructing the controller indicated by the URI
127
   */
128
  public function run()
129
  {
130
    $this->startSession();
131
 
132
    $controller = self::getParam('controller', $_REQUEST);
133
    if (!$controller)
134
    {
135
      $controller = $this->_defaultController;
136
    }
137
 
138
    $controller = ucfirst($controller);
139
 
140
    $controller = $controller . 'Controller';
141
    require_once "{$this->_controllerPath}/{$controller}.php";
142
    $this->_currentController = new $controller();
143
 
144
    return $this;
145
  }
146
 
147
  protected function startSession()
148
  {
149
    session_start();
150
  }
151
 
152
  /**
153
   * Gets a request parameter
154
   *
155
   * @param string $key
156
   *   Key to look up in the array
157
   * @param array $array
158
   *   Array where to look up <var>$key</var>.
159
   *   The default is <code>$_GET</code>.
160
   * @return mixed
161
   *   <code>null</code> if there is no such <var>$key</var>
162
   *   in <var>$array</var>
163
   */
164
  public static function getParam($key, array $array = null)
165
  {
31 PointedEar 166
    if ($array === null)
27 PointedEar 167
    {
168
      $array = $_GET;
169
    }
170
 
171
    return isset($array[$key]) ? $array[$key] : null;
172
  }
173
 
174
  /**
175
   * Registers a database
176
   *
177
   * @param string $key
178
   * @param Database $database
179
   */
180
  public function registerDatabase($key, Database $database)
181
  {
182
    Registry::set($key, $database);
183
  }
184
 
185
  /**
186
   * Sets the default database
187
   * @param key Registry key to refer to the {@link Database}
188
   */
189
  public function setDefaultDatabase($key)
190
  {
191
    $this->_defaultDatabase = $key;
192
  }
193
 
194
  /**
195
  * Sets the current controller for this application
196
  *
197
  * @param Controller $controller
198
  * @return Application
199
  */
200
  public function setCurrentController(Controller $controller)
201
  {
202
    $this->_currentController = $controller;
203
    return $this;
204
  }
205
 
206
  /**
207
   * Returns the current controller for this application
208
   *
209
   * @return Controller
210
   */
211
  public function getCurrentController()
212
  {
213
    return $this->_currentController;
214
  }
215
 
216
  /**
217
   * Returns the default database for this application
218
   *
219
   * @return Database
220
   */
221
  public function getDefaultDatabase()
222
  {
223
    return Registry::get($this->_defaultDatabase);
224
  }
225
 
226
  /**
35 PointedEar 227
   * Returns a relative URI-reference for an action of the
27 PointedEar 228
   * application
229
   *
230
   * @param string[optional] $controller
231
   * @param string[optional] $action
232
   * @param int[optional] $id
233
   */
234
  public function getURL($controller = null, $action = null, $id = null)
235
  {
236
    /* Apache module */
237
    $url = self::getParam('SCRIPT_URL', $_SERVER);
238
    if ($url === null)
239
    {
240
      /* FastCGI */
241
      $url = self::getParam('URL', $_SERVER);
242
      if ($url === null)
243
      {
31 PointedEar 244
        /* Server/PHP too old, compute URI */
245
        $url = self::getParam('REQUEST_URI', $_SERVER);
246
        if (preg_match('/^[^?]+/', $url, $matches) > 0)
247
        {
248
          $url = $matches[0];
249
        }
250
        else
251
        {
252
          /* Has .php in it, but at least it works */
253
          $url = self::getParam('SCRIPT_NAME', $_SERVER);
254
          if ($url === null)
255
          {
256
            throw new Exception(
257
              'None of $_SERVER["SCRIPT_URL"], $_SERVER["URL"],'
258
              . ' $_SERVER["REQUEST_URI"], or $_SERVER["SCRIPT_NAME"]'
259
              . ' is available, cannot continue.');
260
          }
261
        }
27 PointedEar 262
      }
263
    }
264
 
31 PointedEar 265
    $query = (($controller !== null) ? 'controller=' . $controller : '')
266
           . (($action !== null) ? '&action=' . $action : '')
267
           . (($id !== null) ? '&id=' . $id : '');
27 PointedEar 268
 
269
    return $url . ($query ? '?' . $query : '');
270
  }
271
 
272
  /**
273
   * Performs a server-side redirect within the application
274
   */
275
  public static function redirect($query = '')
276
  {
277
    $script_uri = self::getParam('SCRIPT_URI', $_SERVER);
31 PointedEar 278
    if ($script_uri === null)
27 PointedEar 279
    {
280
      /* Server/PHP too old, compute URI */
281
      if (preg_match('/^[^?]+/',
282
          self::getParam('REQUEST_URI', $_SERVER), $matches) > 0)
283
      {
284
        $query_prefix = $matches[0];
285
      }
286
      else
287
      {
288
        /* Has .php in it, but at least it works */
289
        $query_prefix = self::getParam('SCRIPT_NAME', $_SERVER);
290
      }
291
 
292
      /* TODO: Let user decide which ports map to which URI scheme */
293
      $script_uri = (self::getParam('SERVER_PORT', $_SERVER) == 443
294
                      ? 'https://'
295
                      : 'http://')
296
                  . self::getParam('HTTP_HOST', $_SERVER)
297
                  . $query_prefix;
298
    }
299
 
300
    header('Location: ' . $script_uri
301
      . ($query ? (substr($query, 0, 1) === '?' ? '' : '?') . $query : ''));
302
  }
303
}