| [ PHPXref.com ] | [ Generated: Sun Jul 20 20:29:46 2008 ] | [ Swat 0.1.8 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 3 require_once 'Swat/SwatObject.php'; 4 require_once 'Swat/SwatLayout.php'; 5 require_once 'Swat/SwatApplicationModule.php'; 6 7 /** 8 * Base class for a web application 9 * 10 * @package Swat 11 * @copyright 2004-2005 silverorange 12 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 13 */ 14 class SwatApplication extends SwatObject 15 { 16 // {{{ class constants 17 18 const VAR_POST = 1; 19 const VAR_GET = 2; 20 const VAR_REQUEST = 4; 21 const VAR_COOKIE = 8; 22 const VAR_SERVER = 16; 23 const VAR_SESSION = 32; 24 const VAR_FILES = 64; 25 const VAR_ENV = 128; 26 27 // }}} 28 // {{{ public properties 29 30 /** 31 * A unique identifier for this application 32 * 33 * @var string 34 */ 35 public $id; 36 37 // }}} 38 // {{{ protected properties 39 40 /** 41 * The current page of this application 42 * 43 * @var SwatPage 44 */ 45 protected $page = null; 46 47 /** 48 * The base value for all of this application's anchor hrefs 49 * 50 * @var string 51 */ 52 protected $base_href = null; 53 54 /** 55 * The uri of the current page request 56 * 57 * @var string 58 */ 59 protected $uri = null; 60 61 /** 62 * Application modules 63 * 64 * @var array 65 */ 66 protected $modules = array(); 67 68 // }}} 69 // {{{ private properties 70 71 /** 72 * Whether init() has been run on this->page 73 * 74 * @var boolean 75 */ 76 private $page_initialized = false; 77 78 // }}} 79 // {{{ public function __construct() 80 81 /** 82 * Creates a new Swat application 83 * 84 * @param string $id a unique identifier for this application. 85 */ 86 public function __construct($id) 87 { 88 $this->id = $id; 89 } 90 91 // }}} 92 // {{{ public function init() 93 94 /** 95 * Initializes this application 96 * 97 * Subclasses should implement all application level initialization here 98 * and call whichever SwatApplication::init* methods are necessary. 99 */ 100 public function init() 101 { 102 $this->initBaseHref(); 103 $this->initModules(); 104 105 // call this last 106 $this->initPage(); 107 } 108 109 // }}} 110 // {{{ protected function initModules() 111 112 /** 113 * Initializes the modules 114 */ 115 protected function initModules() 116 { 117 foreach ($this->modules as $module) 118 $module->init(); 119 } 120 121 // }}} 122 // {{{ protected function initBaseHref() 123 124 /** 125 * Initializes the base href 126 */ 127 protected function initBaseHref($prefix_length = 0, $secure = false) 128 { 129 $this->uri = $_SERVER['REQUEST_URI']; 130 131 $uri_array = explode('/', $this->uri); 132 133 $base_uri = implode('/', 134 array_slice($uri_array, 0, $prefix_length + 1)).'/'; 135 136 $protocol = ($secure) ? 'https://' : 'http://'; 137 138 $this->base_href = $protocol.$this->getServerName().$base_uri; 139 } 140 141 // }}} 142 // {{{ protected function initPage() 143 144 /** 145 * Initializes the page 146 */ 147 protected function initPage() 148 { 149 if ($this->page === null) 150 $this->page = $this->resolvePage(); 151 152 $this->page->init(); 153 154 $this->page_initialized = true; 155 } 156 157 // }}} 158 // {{{ public function getPage() 159 160 /** 161 * Gets the current page 162 */ 163 public function getPage() 164 { 165 return $this->page; 166 } 167 168 // }}} 169 // {{{ public function setPage() 170 171 /** 172 * Sets the current page 173 * 174 * If a page object is provided, the current page is set to the provided 175 * page replacing any previous page. This can be useful to process one page 176 * then load another page to process and display. 177 * 178 * If no page object is provided a default page is chosen based on 179 * application specific code. Subclasses should implement logic here to 180 * decide which page sub-class to instantiate. 181 * 182 * @param SwatPage the page to load as a replacement of the current page. 183 * 184 * @see SwatPage 185 */ 186 public function setPage(SwatPage $page) 187 { 188 $this->page = $page; 189 190 if ($this->page_initialized) 191 $this->page->init(); 192 } 193 194 // }}} 195 // {{{ protected function resolvePage() 196 197 /** 198 * Resolves a page for this application 199 * 200 * This method is called if no {@link SwatPage} is provided to the 201 * {@link SwatApplication::setPage()} method. 202 */ 203 protected function resolvePage() 204 { 205 return new SwatPage($this); 206 } 207 208 // }}} 209 // {{{ public function getBaseHref() 210 211 /** 212 * Gets the base value for all application anchor hrefs 213 * 214 * @return string the base value for all application anchor hrefs. 215 */ 216 public function getBaseHref() 217 { 218 return $this->base_href; 219 } 220 221 // }}} 222 // {{{ public function getUri() 223 224 /** 225 * Gets the URI of the current page request 226 * 227 * @return string the URI of the current page request. 228 */ 229 public function getUri() 230 { 231 return $this->uri; 232 } 233 234 // }}} 235 // {{{ protected function getServerName() 236 237 /** 238 * Gets the servername 239 * 240 * @return string the servername 241 */ 242 protected function getServerName() 243 { 244 return $_SERVER['HTTP_HOST']; 245 } 246 247 // }}} 248 // {{{ public function addModule() 249 250 /** 251 * Add a module to the application 252 */ 253 public function addModule(SwatApplicationModule $module) 254 { 255 $this->modules[get_class($module)] = $module; 256 } 257 258 // }}} 259 // {{{ public function relocate() 260 261 /** 262 * Relocates to another URI 263 * 264 * Calls the PHP header() function to relocate this application to another 265 * URI. This function does not return and in fact calls the PHP exit() 266 * function just to be sure execution does not continue. 267 * 268 * @param string $uri the URI to relocate to. 269 */ 270 public function relocate($uri) 271 { 272 if (substr($uri, 0, 1) != '/' && strpos($uri, '://') === false) 273 $uri = $this->getBaseHref().$uri; 274 275 header('Location: '.$uri); 276 exit(); 277 } 278 279 // }}} 280 // {{{ public static function initVar() 281 282 /** 283 * Initializes a variable 284 * 285 * Static convenience method to initialize a local variable with a value 286 * from one of the PHP global arrays. 287 * 288 * @param string $name the name of the variable to lookup. 289 * 290 * @param integer $types a bitwise combination of self::VAR_* 291 * constants. 292 * 293 * @param mixed $default the value to return if variable is not found in 294 * the super-global arrays. 295 * 296 * @return mixed the value of the variable. 297 */ 298 public static function initVar($name, $default = null, $types = 0) 299 { 300 $var = $default; 301 302 if ($types == 0) 303 $types = self::VAR_POST | self::VAR_GET; 304 305 if (($types & self::VAR_POST) != 0 306 && isset($_POST[$name])) 307 $var = $_POST[$name]; 308 309 elseif (($types & self::VAR_GET) != 0 310 && isset($_GET[$name])) 311 $var = $_GET[$name]; 312 313 elseif (($types & self::VAR_REQUEST) != 0 314 && isset($_REQUEST[$name])) 315 $var = $_REQUEST[$name]; 316 317 elseif (($types & self::VAR_COOKIE) != 0 318 && isset($_COOKIE[$name])) 319 $var = $_COOKIE[$name]; 320 321 elseif (($types & self::VAR_SERVER) != 0 322 && isset($_SERVER[$name])) 323 $var = $_SERVER[$name]; 324 325 elseif (($types & self::VAR_SESSION) != 0 326 && isset($_SESSION[$name])) 327 $var = $_SESSION[$name]; 328 329 elseif (($types & self::VAR_FILES) != 0 330 && isset($_FILES[$name])) 331 $var = $_FILES[$name]; 332 333 elseif (($types & self::VAR_ENV != 0) 334 && isset($_ENV[$name])) 335 $var = $_ENV[$name]; 336 337 return $var; 338 } 339 340 // }}} 341 } 342 343 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |