Location: PHPKode > projects > Mocovie web framework > mocovi/library/autoload/FSFactory.php
<?php
/**
 *  Copyright (C) 2010  Kai Dorschner
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Kai Dorschner <the-hide@address.com>
 * @copyright Copyright 2010, Kai Dorschner
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @package mocovi
 * @subpackage filesystems
 */

/**
 * Require MVC Exception
 */
class_exists('MvcException')	or require $GLOBALS['library'].'autoload/MvcException.php';


/**
 * Factory creates each Filesystem type.
 *
 * @package mocovi
 * @subpackage filesystems
 */
class FSFactory
{
	protected static $filesystems = array
	( 'xml'		=> 'XMLFS'
	, 'json'	=> 'JsonFS'
	//, 'yaml'	=> 'YAMLFS'
	//, 'dir'		=> 'DirFS'
	, 'mysql'	=> 'MySQLFS'
	);

	protected static $suffix = 'php';

	public static function load($fsfile, $type)
	{
		$type = strtolower($type);
		self::validateParams($fsfile, $type);
		require_once(self::classFile($type));
		$class = self::getClassNameByType($type);
		return new $class($fsfile);
	}

	protected static function validateParams($fsfile, $type)
	{
		if(strlen($type) <= 0)
			throw new MvcException('A filesytem type is essential to determin the corresponding filesystem. Please provide the filesystem type.');
		elseif(!is_file($fsfile))
			throw new MvcException('Path to the filesytem could not be found ('.$fsfile.'). A filesystem is obligatory. Please provide an URL.');
		if(!self::typeExists($type))
			throw new MvcException('"'.$type.'" is not covered by any filesystem or corresponding file does not exist. Available are: '.implode(', ', array_keys(self::$filesystems)));
		return true;
	}

	protected static function getClassNameByType($type)
	{
		return self::$filesystems[$type];
	}

	protected static function typeExists($type)
	{
		return array_key_exists($type, self::$filesystems) && is_file(self::classFile($type));
	}

	protected static function classFile($type)
	{
		return $GLOBALS['filesystemsLibrary'].self::getClassNameByType($type).'.'.self::$suffix;
	}
}
Return current item: Mocovie web framework