Subversion Repositories PHPX

Compare Revisions

Last modification

Ignore whitespace Rev 51 → Rev 52

/trunk/Application.php
2,9 → 2,21
 
namespace PointedEars\PHPX;
 
require_once __DIR__ . '/AbstractModel.php';
require_once __DIR__ . '/Registry.php';
function autoload ($class)
{
if (\strpos($class, '..'))
{
throw new \InvalidArgumentException(
'Refusing to load unsafe class ' . $class);
}
 
require_once \preg_replace('#\\\#', '/',
\preg_replace('#^' . \preg_quote(__NAMESPACE__) .'#', __DIR__, $class)
) . '.php';
}
 
\spl_autoload_register(__NAMESPACE__ . '\\autoload');
 
/**
* Basic application class
*
43,7 → 55,7
*/
private static $_instance;
 
protected function __construct()
protected function __construct ()
{
/* Singleton pattern */
}
57,7 → 69,7
* already initialized.
* @return Application
*/
public static function getInstance(Application $instance = null)
public static function getInstance (Application $instance = null)
{
if (self::$_instance === null)
{
74,23 → 86,23
* @throws ModelPropertyException
* @return mixed
*/
public function __get($name)
public function __get ($name)
{
/* Support for Object-Relational Mappers */
if (strpos($name, 'persistent') === 0)
if (\strpos($name, 'persistent') === 0)
{
$class = get_class($this);
$class = \get_class($this);
return $class::${$name};
}
 
$method = 'get' . ucfirst($name);
$method = 'get' . \ucfirst($name);
 
if (method_exists($this, $method))
if (\method_exists($this, $method))
{
return $this->$method();
}
 
if (property_exists($this, "_$name"))
if (\property_exists($this, "_$name"))
{
return $this->{"_$name"};
}
105,16 → 117,16
* @param mixed $value The new property value before assignment
* @throws ModelPropertyException
*/
public function __set($name, $value)
public function __set ($name, $value)
{
$method = 'set' . ucfirst($name);
$method = 'set' . \ucfirst($name);
 
if (method_exists($this, $method))
if (\method_exists($this, $method))
{
return $this->$method($value);
}
 
if (property_exists($this, "_$name"))
if (\property_exists($this, "_$name"))
{
$this->{"_$name"} = $value;
return $this->{"_$name"};
127,7 → 139,7
* Runs the application, setting up session management and
* constructing the controller indicated by the URI
*/
public function run()
public function run ()
{
$this->startSession();
 
137,7 → 149,7
$controller = $this->_defaultController;
}
 
$controller = ucfirst($controller);
$controller = \ucfirst($controller);
 
$controller = $controller . 'Controller';
require_once "{$this->_controllerPath}/{$controller}.php";
146,9 → 158,9
return $this;
}
 
protected function startSession()
protected function startSession ()
{
session_start();
\session_start();
}
 
/**
163,7 → 175,7
* <code>null</code> if there is no such <var>$key</var>
* in <var>$array</var>
*/
public static function getParam($key, array $array = null)
public static function getParam ($key, array $array = null)
{
if ($array === null)
{
202,7 → 214,7
* @param Controller $controller
* @return Application
*/
public function setCurrentController(Controller $controller)
public function setCurrentController (Controller $controller)
{
$this->_currentController = $controller;
return $this;
213,7 → 225,7
*
* @return Controller
*/
public function getCurrentController()
public function getCurrentController ()
{
return $this->_currentController;
}
223,7 → 235,7
*
* @return Database
*/
public function getDefaultDatabase()
public function getDefaultDatabase ()
{
return Registry::get($this->_defaultDatabase);
}
236,7 → 248,7
* @param string[optional] $action
* @param int[optional] $id
*/
public function getURL($controller = null, $action = null, $id = null)
public function getURL ($controller = null, $action = null, $id = null)
{
/* Apache module */
$url = self::getParam('SCRIPT_URL', $_SERVER);
248,7 → 260,7
{
/* Server/PHP too old, compute URI */
$url = self::getParam('REQUEST_URI', $_SERVER);
if (preg_match('/^[^?]+/', $url, $matches) > 0)
if (\preg_match('/^[^?]+/', $url, $matches) > 0)
{
$url = $matches[0];
}
258,7 → 270,7
$url = self::getParam('SCRIPT_NAME', $_SERVER);
if ($url === null)
{
throw new Exception(
throw new \Exception(
'None of $_SERVER["SCRIPT_URL"], $_SERVER["URL"],'
. ' $_SERVER["REQUEST_URI"], or $_SERVER["SCRIPT_NAME"]'
. ' is available, cannot continue.');
277,13 → 289,13
/**
* Performs a server-side redirect within the application
*/
public static function redirect($query = '')
public static function redirect ($query = '')
{
$script_uri = self::getParam('SCRIPT_URI', $_SERVER);
if ($script_uri === null)
{
/* Server/PHP too old, compute URI */
if (preg_match('/^[^?]+/',
if (\preg_match('/^[^?]+/',
self::getParam('REQUEST_URI', $_SERVER), $matches) > 0)
{
$query_prefix = $matches[0];
302,7 → 314,7
. $query_prefix;
}
 
header('Location: ' . $script_uri
\header('Location: ' . $script_uri
. ($query ? (substr($query, 0, 1) === '?' ? '' : '?') . $query : ''));
}
}
}