Location: PHPKode > projects > FreeORM > FreeORM/SessionFactory.php
<?php

/**
 * Initialize Database parameters, logger mode and the orm mode. 
 * User can get the session objects from this class.
 * 
 * @package		com.freeorm
 * @author		Yide Zou
 * @link		http://www.freeorm.com
 * @copyright	Copyright (c) 2010 Yide Zou <hide@address.com>. 
 * 				All Rights Reserved.
 * @license		This software is released under the terms of the GNU Lesser General Public License
 * 				A copy of which is available from http://www.gnu.org/copyleft/lesser.html
 */



require_once 'SessionXML.php';
require_once 'exceptions/NoXMLMapping.php';
require_once 'exceptions/NotImplemented.php';
require_once 'GlobalConstant.php';
require_once 'databases/DBMySQL.php';


class SessionFactory
{
	private $logger = null;
	private $ormMode = ORM_MODE_XML;
	private $xmlcfg = null;
	/*
	 * Database 
	 */
	private $dbType = DB_TYPE_MYSQL;
	private $db_host;
	private $db_port;
	private $db_username;
	private $db_password; 
	private $db_dbname;
	
	public function __construct($dbType, $db_host, $db_port, $db_username, $db_password, $db_dbname, Logger $logger, $ormMode=ORM_MODE_XML)
	{
		$this->dbType = $dbType;
		$this->db_host = $db_host;
		$this->db_port = $db_port;
		$this->db_username = $db_username;
		$this->db_password = $db_password;
		$this->db_dbname = $db_dbname;
		
		$this->logger = $logger;
		$this->ormMode = $ormMode;
	}
	
	/**
	 * Should call this function before open a session with XML mode
	 * @param $ormxml
	 * @return unknown_type
	 */
	public function setXMLMapping($ormxml)
	{
		$this->xmlcfg = simplexml_load_file($ormxml);
	}
	
	/**
	 * Get a new session
	 * @return a session object
	 * @throws NoXMLMapping if the XML file not yet be setted for the XML orm mode
	 */
	public function openSession()
	{
		switch ($this->dbType)
		{
			case DB_TYPE_MYSQL:
			default:
				$database = new DBMySQL(
					$this->db_host, $this->db_port, $this->db_username, $this->db_password, $this->db_dbname, $this->logger);
		}
		switch ($this->ormMode)
		{
			case ORM_MODE_ANNOTATION:
				throw new NotImplemented();
				break;
			case ORM_MODE_XML:
			default:
				if ($this->xmlcfg)
					return new SessionXML($database, $this->logger, $this->xmlcfg);
				else
					throw new NoXMLMapping();
		}
		return false;
	}
}
Return current item: FreeORM