Subversion Repositories PHPX

Rev

Rev 35 | Rev 51 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 35 Rev 45
Line 13... Line 13...
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
Line 59... Line 59...
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
Line 78... Line 78...
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
Line 165... Line 165...
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
  /**
Line 200... Line 203...
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
   */
Line 259... Line 262...
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
  {