Created a Singleton parent class and implemented some common Object method.

This commit is contained in:
Josh Sherman 2010-10-02 16:36:54 -04:00
parent 5b0c42bda3
commit d2966c7345
8 changed files with 204 additions and 68 deletions

View file

@ -18,21 +18,17 @@
/**
* Config Class
*
* Handles loading the site's configuration file (if available).
* Handles loading the site's configuration file (if available). At the moment
* this class is a very skewed Singleton. The plan is to eventually extend this
* out to support multiple configuration files, and the ability to load in
* custom config files on the fly as well. The core of PICKLES uses the class
* as a Singleton so we're not loading the configuration multiple times per
* page load.
*
* @usage <code>$config = new Config($filename);</code>
*/
class Config
class Config extends Object
{
/**
* Instance of the Config object
*
* @static
* @access private
* @var object
*/
private static $instance;
/**
* Config data
*
@ -44,20 +40,22 @@ class Config
/**
* Constructor
*
* Calls the parent constructor and loads the pass file
* Calls the parent constructor and loads the passed file.
*
* @param string $filename optional Filename of the config
*/
public function __construct($filename = '../config.ini')
{
parent::__construct();
$this->load($filename);
}
/**
* Loads a configuration file
*
* Handles the potential loading of the configuration file and
* sanitizing the boolean strings into actual boolean values.
* Handles the potential loading of the configuration file and sanitizing
* the boolean strings into actual boolean values.
*
* @param string $filename filename of the config file
* @return boolean Success of the load process
@ -80,20 +78,15 @@ class Config
/**
* Get instance of the object
*
* Instantiates a new object if one isn't already available, then
* returns the instance.
* Let's the parent class do all the work
*
* @static
* @return object self::$instance instance of the Config
* @param string $class name of the class to instantiate
* @return object self::$instance instance of the Config class
*/
public static function getInstance()
public static function getInstance($class = 'Config')
{
if (!isset(self::$instance) || empty(self::$instance))
{
self::$instance = new Config();
}
return self::$instance;
return parent::getInstance($class);
}
/**

View file

@ -18,26 +18,15 @@
/**
* Database Abstraction Layer for MySQL
*
* All database usage inside PICKLES-based sites should be done
* via the database object that is a part of every model ($this->db).
* Because the database object can execute raw SQL, there should be
* no limitations.
* All database usage inside PICKLES-based sites should be done via the
* database object that is a part of every model ($this->db). Because the
* database object can execute raw SQL, there should be no limitations. Within
* the PICKLES system, the database instance is shared, but the Database
* constructor is not private, just in case someone wants to create new
* Database objects all willy nilly like.
*/
class Database extends Object
{
/**
* Instance of the Database object
*
* Within the PICKLES system, the database instance is shared, but the
* Database constructor is not private, just in case someone wants to
* create new Database objects.
*
* @static
* @access private
* @var object
*/
private static $instance;
/**
* Hostname for the MySQL Server
*
@ -130,20 +119,15 @@ class Database extends Object
/**
* Get instance of the object
*
* Instantiates a new object if one isn't already available, then
* returns the instance.
* Let's the parent class do all the work
*
* @static
* @return object self::$instance instance of the Database
* @param string $class name of the class to instantiate
* @return object self::$instance instance of the Config class
*/
public static function getInstance()
public static function getInstance($class = 'Database')
{
if (!isset(self::$instance) || empty(self::$instance))
{
self::$instance = new Database();
}
return self::$instance;
return parent::getInstance($class);
}
/**
@ -201,8 +185,8 @@ class Database extends Object
/**
* Executes an SQL Statement
*
* Executes a standard or prepared query based on passed parameters.
* All queries are logged to a file as well as timed and logged in the
* Executes a standard or prepared query based on passed parameters. All
* queries are logged to a file as well as timed and logged in the
* execution time is over 1 second.
*
* @param string $sql statement to execute
@ -321,8 +305,8 @@ class Database extends Object
/**
* Fetch a single column from the database
*
* This method assumes you want the first column in your select.
* If you need 2 or more columns you should simply use fetch()
* This method assumes you want the first column in your select. If you
* need 2 or more columns you should simply use fetch().
*
* @param string $sql statement to be executed
* @param array $input_parameters optional key/values to be bound

View file

@ -61,12 +61,13 @@ class Display_PHP extends Display_Common
ob_start();
// Puts the class variables in local scope for the template
$form_class = (isset($this->config->pickles['form']) ? $this->config->pickles['form'] : 'Form');
$__config = $this->config;
$__module = $this->module_return;
$__css_class = $this->css_class;
$__js_file = $this->js_basename;
$form_class = (isset($this->config->pickles['form']) ? $this->config->pickles['form'] : 'Form');
$__form = new $form_class();
$__form = $form_class::getInstance();
// Loads the template
if ($this->parent_template != null)

View file

@ -20,7 +20,7 @@
*
* Standardized error reporting, mostly used to display fatal errors.
*/
class Error extends Object
class Error
{
/**
* Fatal Error

View file

@ -21,8 +21,22 @@
* This class contains methods for easily generating form elements. There is a
* heavy focus on select boxes as they have the most overhead for a developer.
*/
class Form extends Object
class Form extends Singleton
{
/**
* Get Instance
*
* Gets an instance of the Form class
*
* @static
* @param string $class name of the class to get an instance of
* @return object instance of the class
*/
public static function getInstance($class = 'Form')
{
return parent::getInstance($class);
}
/**
* Input
*

View file

@ -20,7 +20,7 @@
*
* Standardized logging methods for ease of reporting.
*/
class Log extends Object
class Log
{
/**
* Log Information

View file

@ -18,14 +18,38 @@
/**
* Object Class
*
* Every non-Singleton-based class needs to extend this class.
* Any models will extend the Model class which entends the Object
* class already. This class handles getting an instance of the
* Config object so that it's available. Also provides a getter
* and setter for variables.
* Every instantiated class in PICKLES should be extending this class. By doing
* so the class is automatically hooked into the profiler, and the object will
* have access to some common components as well.
*
* That all being said, PICKLES does have 4 distinct class types, and not all
* need this class. First, there are true Singleton's which extend the class of
* the same name. There are also instantiated classes (like Module and Model)
* which need to be instantiated to be used. Those classes need to extend this
* class. The 3rd type of class is a static non-Singleton class. They are
* generally seen with no parent class, and are used for performing stateless
* actions. Now we have our 4th type of class, and this is where it gets fun!
* The last class type is a instantiated class with Singleton tendencies. So
* yeah, lines get blurred for the sake of building a very bendable system.
* These "hybrid" classes extend the Object class, but can be instantiated or
* accessed via getInstance(). Why? Well that's simple, I do that so that I can
* share a single instance of the object within the core of PICKLES, but still
* have the availability to create new instances of the object for use outside
* of the core. The Config and Database classes are examples of this type.
*
* So guess what, I bend the rules.
*/
class Object
{
/**
* Object Instances
*
* @static
* @access private
* @var mixed
*/
private static $instances = array();
/**
* Instance of the Config object
*
@ -41,22 +65,48 @@ class Object
*/
public function __construct()
{
$this->config = Config::getInstance();
if (get_class($this) == 'Config')
{
$this->config = true;
}
else
{
$this->config = Config::getInstance();
}
// Optionally logs the constructor to the profiler
if (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] == true)
if ($this->config == true || (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] == true))
{
Profiler::log($this, '__construct');
}
}
/**
* Get Instance
*
* Gets an instance of the passed class.
*
* @static
* @param string $class name of the class
* @return object instance of the class
*/
public static function getInstance($class)
{
if (!isset(self::$instances[$class]))
{
self::$instances[$class] = new $class();
}
return self::$instances[$class];
}
/**
* Destructor
*/
public function __destruct()
{
// Optionally logs the destructor to the profiler
if (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] == true)
if ($this->config == true || (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] == true))
{
Profiler::log($this, '__destruct');
}

94
classes/Singleton.php Normal file
View file

@ -0,0 +1,94 @@
<?php
/**
* Singleton Class File for PICKLES
*
* PHP version 5
*
* Licensed under the GNU General Public License Version 3
* Redistribution of these files must retain the above copyright notice.
*
* @package PICKLES
* @author Josh Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
* @license http://www.gnu.org/licenses/gpl.html GPL v3
* @link http://phpwithpickles.org
*/
/**
* Singleton Class
*
* This class is pretty much just a clone of Object, but with the constructor
* set to private. Due to the nature of static naming in PHP, each Singleton
* class will still need to have a getInstance() method unless you'd prefer to
* pass the name of the class in when you call the parent's getInstance()
* method directly.
*/
class Singleton
{
/**
* Object Instances
*
* @static
* @access private
* @var mixed
*/
private static $instances = array();
/**
* Constructor
*
* Establishes a Config instance for all children to enjoy
*/
protected function __construct()
{
// Logs the action to the profiler. This is done all the time as the
// Config object is a Singleton and created some lovely infinite loops
// when attempting to check the config variables.
Profiler::log($this, '__construct');
}
/**
* Get Instance
*
* Gets an instance of the class that is passed in. Functions more like a
* factory since static classes only know themselves. Example, if you
* replaced $class with __CLASS__ you'd end up instantiating the parent
* Singleton class. So for now, this is how we're doing it.
*
* @static
* @param string $class name of the class we want to create
* @return object instance of the class we requested
*/
public static function getInstance($class)
{
if (!isset(self::$instances[$class]))
{
self::$instances[$class] = new $class();
}
return self::$instances[$class];
}
/**
* Clone
*
* Throws an error as the whole point of a Singleton is to have a single
* instance of the class at any given moment.
*/
public function __clone()
{
trigger_error('Can\'t clone a Singleton.', E_USER_ERROR);
}
/**
* Destructor
*
* This is where the destructor would be, but it was never firing. I think
* it had something to do with the way the instance of the object is being
* stored in the object itself, perhaps under those circumstances the
* destructor is never executed. If you know, get in touch.
*/
}
?>