Changeset 468 for trunk/lib/App.inc.php


Ignore:
Timestamp:
Feb 20, 2014 3:03:59 AM (10 years ago)
Author:
anonymous
Message:

Completed integrating /branches/eli_branch into /trunk. Changes include:

  • Removed closing ?> from end of files
  • Upgrade old-style contructor methods to use construct() instead.
  • Class properties and methods defined as public, private, static or protected
  • Ensure code runs under E_ALL with only mysql_* deprecated warnings
  • Search for the '@' symbol anywhere it might be used to supress runtime errors, then replace with proper error recovery.
  • Run the php cli -l option to check files for syntax errors.
  • Bring tests up-to-date with latest version and methods of PHPUnit
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/lib/App.inc.php

    r453 r468  
    4242class App {
    4343
     44    // A place to keep an object instance for the singleton pattern.
     45    private static $instance = null;
     46
    4447    // Namespace of this application instance.
    45     var $_ns;
     48    private $_ns;
    4649
    4750    // If $app->start has run successfully.
    48     var $running = false;
     51    public $running = false;
    4952
    5053    // Instance of database object.
    51     var $db;
     54    public $db;
    5255
    5356    // Array of query arguments will be carried persistently between requests.
    54     var $_carry_queries = array();
     57    private $_carry_queries = array();
    5558
    5659    // Dictionary of global application parameters.
    57     var $_params = array();
     60    private $_params = array();
    5861
    5962    // Default parameters.
    60     var $_param_defaults = array(
     63    private $_param_defaults = array(
    6164
    6265        // Public name and email address for this application.
     
    162165
    163166    /**
     167     * Constructor.
     168     */
     169    public function __construct($namespace='')
     170    {
     171        // Set namespace of application instance.
     172        $this->_ns = $namespace;
     173
     174        // Initialize default parameters.
     175        $this->_params = array_merge($this->_params, $this->_param_defaults);
     176
     177        // Begin timing script.
     178        require_once dirname(__FILE__) . '/ScriptTimer.inc.php';
     179        $this->timer = new ScriptTimer();
     180        $this->timer->start('_app');
     181    }
     182
     183    /**
    164184     * This method enforces the singleton pattern for this class. Only one application is running at a time.
    165185     *
     
    169189     * @static
    170190     */
    171     static function &getInstance($namespace='')
    172     {
    173         static $instance = null;
    174 
    175         if ($instance === null) {
    176             $instance = new App($namespace);
    177         }
    178 
    179         return $instance;
    180     }
    181 
    182     /**
    183      * Constructor.
    184      */
    185     function App($namespace='')
    186     {
    187         // Set namespace of application instance.
    188         $this->_ns = $namespace;
    189 
    190         // Initialize default parameters.
    191         $this->_params = array_merge($this->_params, $this->_param_defaults);
    192 
    193         // Begin timing script.
    194         require_once dirname(__FILE__) . '/ScriptTimer.inc.php';
    195         $this->timer = new ScriptTimer();
    196         $this->timer->start('_app');
     191    public static function &getInstance($namespace='')
     192    {
     193        if (self::$instance === null) {
     194            // TODO: Yep, having a namespace with one singletone instance is not very useful.
     195            self::$instance = new self($namespace);
     196        }
     197
     198        return self::$instance;
    197199    }
    198200
     
    203205     * @param  array    $param     Array of parameters (key => val pairs).
    204206     */
    205     function setParam($param=null)
     207    public function setParam($param=null)
    206208    {
    207209        if (isset($param) && is_array($param)) {
     
    218220     * @return  mixed               Parameter value, or null if not existing.
    219221     */
    220     function getParam($param=null)
     222    public function getParam($param=null)
    221223    {
    222224        if ($param === null) {
     
    225227            return $this->_params[$param];
    226228        } else {
    227             trigger_error(sprintf('Parameter is not set: %s', $param), E_USER_NOTICE);
    228229            return null;
    229230        }
     
    237238     * @since   15 Jul 2005 00:32:21
    238239     */
    239     function start()
     240    public function start()
    240241    {
    241242        if ($this->running) {
     
    275276
    276277            // DB connection parameters taken from environment variables in the httpd.conf file, readable only by root.
    277             if (!empty($_SERVER['DB_SERVER'])) {
     278            if (!empty($_SERVER['DB_SERVER']) && !$this->getParam('db_server')) {
    278279                $this->setParam(array('db_server' => $_SERVER['DB_SERVER']));
    279280            }
    280             if (!empty($_SERVER['DB_NAME'])) {
     281            if (!empty($_SERVER['DB_NAME']) && !$this->getParam('db_name')) {
    281282                $this->setParam(array('db_name' => $_SERVER['DB_NAME']));
    282283            }
    283             if (!empty($_SERVER['DB_USER'])) {
     284            if (!empty($_SERVER['DB_USER']) && !$this->getParam('db_user')) {
    284285                $this->setParam(array('db_user' => $_SERVER['DB_USER']));
    285286            }
    286             if (!empty($_SERVER['DB_PASS'])) {
     287            if (!empty($_SERVER['DB_PASS']) && !$this->getParam('db_pass')) {
    287288                $this->setParam(array('db_pass' => $_SERVER['DB_PASS']));
    288289            }
     
    394395     * @since   17 Jul 2005 17:20:18
    395396     */
    396     function stop()
     397    public function stop()
    397398    {
    398399        session_write_close();
    399         restore_include_path();
    400400        $this->running = false;
    401401        $num_queries = 0;
     
    423423     * @param string $line    __LINE__.
    424424     */
    425     function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null)
     425    public function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null)
    426426    {
    427427        $message = trim($message);
     
    465465     * @since   21 Dec 2005 13:09:20
    466466     */
    467     function getRaisedMessages()
     467    public function getRaisedMessages()
    468468    {
    469469        if (!$this->running) {
     
    471471            return false;
    472472        }
    473 
    474473        return isset($_SESSION['_app'][$this->_ns]['messages']) ? $_SESSION['_app'][$this->_ns]['messages'] : array();
    475474    }
     
    482481     * @since   21 Dec 2005 13:21:54
    483482     */
    484     function clearRaisedMessages()
     483    public function clearRaisedMessages()
    485484    {
    486485        if (!$this->running) {
     
    503502     * @since   15 Jul 2005 01:39:14
    504503     */
    505     function printRaisedMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg')
    506     {
     504    public function printRaisedMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg')
     505    {
     506
    507507        if (!$this->running) {
    508508            $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__);
     
    576576     * @param string $line      The line of the file where the log event occurs.
    577577     */
    578     function logMsg($message, $priority=LOG_INFO, $file=null, $line=null)
     578    public function logMsg($message, $priority=LOG_INFO, $file=null, $line=null)
    579579    {
    580580        static $previous_events = array();
     
    690690        // SCREEN ACTION
    691691        if (false !== $this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) {
    692             echo "[{$event['type']}] [{$event['message']}]\n";
     692            file_put_contents('php://stderr', "[{$event['type']}] [{$event['message']}]\n", FILE_APPEND);
    693693        }
    694694
     
    706706     * @return                The string representation of $priority.
    707707     */
    708     function logPriorityToString($priority) {
     708    public function logPriorityToString($priority) {
    709709        $priorities = array(
    710710            LOG_EMERG   => 'emergency',
     
    735735     * @since   13 Oct 2007 11:34:51
    736736     */
    737     function setQuery($query_key, $val)
     737    public function setQuery($query_key, $val)
    738738    {
    739739        if (!is_array($query_key)) {
     
    757757     * @since   14 Nov 2005 19:24:52
    758758     */
    759     function carryQuery($query_key, $default=false)
     759    public function carryQuery($query_key, $default=false)
    760760    {
    761761        if (!is_array($query_key)) {
     
    782782     * @since   18 Jun 2007 20:57:29
    783783     */
    784     function dropQuery($query_key, $unset=false)
     784    public function dropQuery($query_key, $unset=false)
    785785    {
    786786        if (!is_array($query_key)) {
     
    818818     * @return string url with attached queries and, if not using cookies, the session id
    819819     */
    820     function url($url, $carry_args=null, $always_include_sid=false)
     820    public function url($url, $carry_args=null, $always_include_sid=false)
    821821    {
    822822        if (!$this->running) {
     
    909909     * @since   09 Dec 2005 17:58:45
    910910     */
    911     function oHREF($url, $carry_args=null, $always_include_sid=false)
     911    public function oHREF($url, $carry_args=null, $always_include_sid=false)
    912912    {
    913913        $url = $this->url($url, $carry_args, $always_include_sid);
     
    929929     *                                      false  <-- To not carry any queries. If URL already has queries those will be retained.
    930930     */
    931     function printHiddenSession($carry_args=null)
     931    public function printHiddenSession($carry_args=null)
    932932    {
    933933        if (!$this->running) {
     
    995995     * @param   bool    $always_include_sid     Force session id to be added to Location header.
    996996     */
    997     function dieURL($url, $carry_args=null, $always_include_sid=false)
     997    public function dieURL($url, $carry_args=null, $always_include_sid=false)
    998998    {
    999999        if (!$this->running) {
     
    10421042    * @since    31 Mar 2006 19:17:00
    10431043    */
    1044     function dieBoomerangURL($id=null, $carry_args=null, $default_url=null, $queryless_referrer_comparison=false)
     1044    public function dieBoomerangURL($id=null, $carry_args=null, $default_url=null, $queryless_referrer_comparison=false)
    10451045    {
    10461046        if (!$this->running) {
     
    10851085     * FIXME: url garbage collection?
    10861086     */
    1087     function setBoomerangURL($url=null, $id=null)
     1087    public function setBoomerangURL($url=null, $id=null)
    10881088    {
    10891089        if (!$this->running) {
     
    10931093        // A redirection will never happen immediately after setting the boomerangURL.
    10941094        // Set the time so ensure this doesn't happen. See $app->validBoomerangURL for more.
    1095         /// FIXME: Why isn't the time set here under setBoomerangURL() and only under dieBoomerangURL()?
    10961095
    10971096        if ('' != $url && is_string($url)) {
     
    11241123     * @param string  $id     An identification tag for this url.
    11251124     */
    1126     function getBoomerangURL($id=null)
     1125    public function getBoomerangURL($id=null)
    11271126    {
    11281127        if (!$this->running) {
     
    11491148     * @param string  $id     An identification tag for this url.
    11501149     */
    1151     function deleteBoomerangURL($id=null)
     1150    public function deleteBoomerangURL($id=null)
    11521151    {
    11531152        if (!$this->running) {
     
    11711170     * @return bool  True if it is set and valid, false otherwise.
    11721171     */
    1173     function validBoomerangURL($id=null, $use_nonspecificboomerang=false)
     1172    public function validBoomerangURL($id=null, $use_nonspecificboomerang=false)
    11741173    {
    11751174        if (!$this->running) {
     
    12211220     * the same page but with https.
    12221221     */
    1223     function sslOn()
     1222    public function sslOn()
    12241223    {
    12251224        if (function_exists('apache_get_modules')) {
     
    12421241     * a http version of the current url.
    12431242     */
    1244     function sslOff()
     1243    public function sslOff()
    12451244    {
    12461245        if ('' != getenv('HTTPS')) {
     
    12481247        }
    12491248    }
    1250 
    1251 
    12521249} // End.
    1253 
    1254 ?>
Note: See TracChangeset for help on using the changeset viewer.