News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1284416102:v:use_geshi-1:v:type-php

Started by Guest, Sep 13, 2010, 10:15 PM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Guest

class Connection
{
   private $type = 'read';
   private $prefix = '';
   private $section = '';
   // other attributes - needed to establish a connection

   // Connection management: this could be its own ConnectionManager class,
   // if it becomes a bit too big
   private static $siteConnections = array();

   // Every attribute should have its accessor functions (*if* they're necessary)
   // meaning, forbid any other means to be accessed from outside the class
   function __getType()
   {
      return $type;
   }
   function __setType($t)
   {
      $this->type = $t;
   }

   private function __construct($settings, $type)
   {
      // Make this object.
      // No Connection object exists unless this function is called,
      // so make sure everything is fine with the new object, here.
      // it must be as fully defined as possible.

      // Then:
      $this->type = $settings['type'];
      $this->prefix = $settings['prefix'];
      $this->section = $settings['section'];
      // etc, the rest of initializations if any

      // Now the Connection object knows its own settings
      // and can be used anytime to connect/test connection to the stuff it knows about
   }

   // This is the only function which makes a Connection object
   // It tries to do that for $section and the type sent.
   static function getInstance($section, $write = false)
   {
      $section = empty($section) ? $site_section : $section;
      $type = $write ? 'write' : 'read';

      if (empty($siteConnections[$section][$type]))
      {
         // Okay, we need to make a new one.
         // With what settings?
         $settings = Settings::getSettings($section);

         // Okay, now make it
         $connection = new Connection($settings, $type);
      }
   }

   function loadSiteDb()
   {
      // There's no need for global $site_section, $siteman_settings etc, since
      // the needed attributes are set for this object

      smf_db_initiate($server, $databaseName, $user, $passwd, $prefix, array('persist' => false));

   }
}

class Settings
{
   // Just a rather dummy class here
   // (these settings could be grabbed directly in a Manager-type class too)
   private $settings = array();
   private function __construct()
   {
      // SiteMan settings tells us what to do.
      // It should never change, so it's better as a non-writable static var
      // like SiteMan::$settingsDir, or a $settingsDir here, in the Settings class.
      require(SiteMan::$settingsDir . '/settings_' . '.php');
      // let's assume we always read a $sitedb_settings here. (make whatever checks necessary)
      $settings = &$sitedb_settings;
   }

   public static function getSettings($section)
   {
      if (empty($settings[$section]))
         $settings[$section] = new Settings();
      // Problems? I hope not!
      // but we could handle then here, if Settings initialization from the disk
      // didn't work.
      return $settings[$section];
   }
}

// This is dummy stuff
class SiteMan
{
   // more central stuff
   static $settingsDir = "/home/sites/simplemachines.org";
}