Subversion Repositories PHPX

Rev

Rev 35 | Rev 51 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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