Permalink
Browse files

New pluggable type system for Entities

 * New 'typeHanlder' method on \Spot\Config to set/get type handlers
 * Defaults: string, int, float, boolean, datetime, date, timestamp,
   year, month, day
 * Whitespace trimming in various files
  • Loading branch information...
1 parent 4668589 commit a353f2d590ccb0167664a57c8448a1afd96911fd @vlucas committed May 31, 2012
View
@@ -3,115 +3,161 @@
/**
* @package Spot
- * @link http://spot.os.ly
*/
class Config implements \Serializable
{
- protected $_defaultConnection;
- protected $_connections = array();
-
-
- /**
- * Add database connection
- *
- * @param string $name Unique name for the connection
- * @param string $dsn DSN string for this connection
- * @param array $options Array of key => value options for adapter
- * @param boolean $defaut Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false.
- * @return Spot_Adapter_Interface Spot adapter instance
- * @throws Spot_Exception
- */
- public function addConnection($name, $dsn, array $options = array(), $default = false)
- {
- // Connection name must be unique
- if(isset($this->_connections[$name])) {
- throw new Exception("Connection for '" . $name . "' already exists. Connection name must be unique.");
+ protected $_defaultConnection;
+ protected $_connections = array();
+ protected static $_typeHandlers = array();
+
+ public function __construct()
+ {
+ // Setup default type hanlders
+ $this->typeHandler('string', '\Spot\Type\String');
+ $this->typeHandler('text', '\Spot\Type\String');
+
+ $this->typeHandler('int', '\Spot\Type\Integer');
+ $this->typeHandler('integer', '\Spot\Type\Integer');
+
+ $this->typeHandler('float', '\Spot\Type\Float');
+ $this->typeHandler('double', '\Spot\Type\Float');
+ $this->typeHandler('decimal', '\Spot\Type\Float');
+
+ $this->typeHandler('bool', '\Spot\Type\Boolean');
+ $this->typeHandler('boolean', '\Spot\Type\Boolean');
+
+ $this->typeHandler('datetime', '\Spot\Type\DateTime');
+ $this->typeHandler('date', '\Spot\Type\DateTime');
+ $this->typeHandler('timestamp', '\Spot\Type\Integer');
+ $this->typeHandler('year', '\Spot\Type\Integer');
+ $this->typeHandler('month', '\Spot\Type\Integer');
+ $this->typeHandler('day', '\Spot\Type\Integer');
}
-
- $dsnp = \Spot\Adapter\AdapterAbstract::parseDSN($dsn);
- $adapterClass = "\\Spot\\Adapter\\" . ucfirst($dsnp['adapter']);
- $adapter = new $adapterClass($dsn, $options);
-
- // Set as default connection?
- if(true === $default || null === $this->_defaultConnection) {
- $this->_defaultConnection = $name;
+
+ /**
+ * Add database connection
+ *
+ * @param string $name Unique name for the connection
+ * @param string $dsn DSN string for this connection
+ * @param array $options Array of key => value options for adapter
+ * @param boolean $defaut Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false.
+ * @return Spot_Adapter_Interface Spot adapter instance
+ * @throws Spot_Exception
+ */
+ public function addConnection($name, $dsn, array $options = array(), $default = false)
+ {
+ // Connection name must be unique
+ if(isset($this->_connections[$name])) {
+ throw new Exception("Connection for '" . $name . "' already exists. Connection name must be unique.");
+ }
+
+ $dsnp = \Spot\Adapter\AdapterAbstract::parseDSN($dsn);
+ $adapterClass = "\\Spot\\Adapter\\" . ucfirst($dsnp['adapter']);
+ $adapter = new $adapterClass($dsn, $options);
+
+ // Set as default connection?
+ if(true === $default || null === $this->_defaultConnection) {
+ $this->_defaultConnection = $name;
+ }
+
+ // Store connection and return adapter instance
+ $this->_connections[$name] = $adapter;
+ return $adapter;
}
-
- // Store connection and return adapter instance
- $this->_connections[$name] = $adapter;
- return $adapter;
- }
-
-
- /**
- * Get connection by name
- *
- * @param string $name Unique name of the connection to be returned
- * @return Spot_Adapter_Interface Spot adapter instance
- * @throws Spot_Exception
- */
- public function connection($name = null)
- {
- if(null === $name) {
- return $this->defaultConnection();
+
+
+ /**
+ * Get connection by name
+ *
+ * @param string $name Unique name of the connection to be returned
+ * @return Spot_Adapter_Interface Spot adapter instance
+ * @throws Spot_Exception
+ */
+ public function connection($name = null)
+ {
+ if(null === $name) {
+ return $this->defaultConnection();
+ }
+
+ // Connection name must be unique
+ if(!isset($this->_connections[$name])) {
+ return false;
+ }
+
+ return $this->_connections[$name];
}
-
- // Connection name must be unique
- if(!isset($this->_connections[$name])) {
- return false;
+
+
+ /**
+ * Get type handler class by type
+ *
+ * @param string $type Field type (i.e. 'string' or 'int', etc.)
+ * @return Spot_Adapter_Interface Spot adapter instance
+ */
+ public function typeHandler($type, $class = null)
@TheSavior

TheSavior Jul 20, 2012

Contributor

Since this isn't static, and it is called from a static context, PHP provides warnings about using $this implicitly (unsafely).

@vlucas

vlucas Aug 7, 2012

Owner

This is been fixed in the latest commit 4f2a2a0

+ {
+ if(null === $class) {
+ if(!isset(self::$_typeHandlers[$type])) {
+ throw new \InvalidArgumentException("Type '$type' not registered. Register the type class handler with \Spot\Config::typeHanlder('$type', '\Namespaced\Path\Class').");
+ }
+ return self::$_typeHandlers[$type];
+ }
+
+ if(!class_exists($class)) {
+ throw new \InvalidArgumentException("Second parameter must be valid className with full namespace. Check the className and ensure the class is loaded before registering it as a type handler.");
+ }
+
+ return self::$_typeHandlers[$type] = $class;
+ }
+
+
+ /**
+ * Get default connection
+ *
+ * @return Spot_Adapter_Interface Spot adapter instance
+ * @throws Spot_Exception
+ */
+ public function defaultConnection()
+ {
+ return $this->_connections[$this->_defaultConnection];
+ }
+
+
+ /**
+ * Class loader
+ *
+ * @param string $className Name of class to load
+ */
+ public static function loadClass($className)
+ {
+ $loaded = false;
+
+ // Require Spot namespaced files by assumed folder structure (naming convention)
+ if(false !== strpos($className, "Spot\\")) {
+ $classFile = trim(str_replace("\\", "/", str_replace("_", "/", str_replace('Spot\\', '', $className))), '\\');
+ $loaded = require_once(__DIR__ . "/" . $classFile . ".php");
+ }
+
+ return $loaded;
}
-
- return $this->_connections[$name];
- }
-
-
- /**
- * Get default connection
- *
- * @return Spot_Adapter_Interface Spot adapter instance
- * @throws Spot_Exception
- */
- public function defaultConnection()
- {
- return $this->_connections[$this->_defaultConnection];
- }
-
-
- /**
- * Class loader
- *
- * @param string $className Name of class to load
- */
- public static function loadClass($className)
- {
- $loaded = false;
-
- // Require Spot namespaced files by assumed folder structure (naming convention)
- if(false !== strpos($className, "Spot\\")) {
- $classFile = trim(str_replace("\\", "/", str_replace("_", "/", str_replace('Spot\\', '', $className))), '\\');
- $loaded = require_once(__DIR__ . "/" . $classFile . ".php");
+
+
+ /**
+ * Default serialization behavior is to not attempt to serialize stored
+ * adapter connections at all (thanks @TheSavior re: Issue #7)
+ */
+ public function serialize()
+ {
+ return serialize(array());
+ }
+
+ public function unserialize($serialized)
+ {
}
-
- return $loaded;
- }
-
-
- /**
- * Default serialization behavior is to not attempt to serialize stored
- * adapter connections at all (thanks @TheSavior re: Issue #7)
- */
- public function serialize()
- {
- return serialize(array());
- }
-
- public function unserialize($serialized)
- {
- }
}
/**
* Register 'spot_load_class' function as an autoloader for files prefixed with 'Spot_'
*/
-spl_autoload_register(array('\Spot\Config', 'loadClass'));
+spl_autoload_register(array('\Spot\Config', 'loadClass'));
Oops, something went wrong.

0 comments on commit a353f2d

Please sign in to comment.