* @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @version $Id$ * @link http://typeoneerror.com/asra * @category Asra * @package Export */ /** * Provides a static method for adding new export * formats by class to the Asra installation. * * @author Ben Borowski * @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @version $Id$ * @link http://typeoneerror.com/asra * @category Asra * @package Export */ final class Asra_Export_Registrar { //--------------------------------------- // CLASS CONSTANTS //--------------------------------------- const KEY_CLASS = 'class'; const KEY_PATH = 'path'; /** * Default export type * * @var string */ public static $defaultType = 'json'; //--------------------------------------- // PRIVATE VARIABLES //--------------------------------------- /** * Available export methods: * *

key => array( 'class' => 'ClassName' [, 'path' => 'ClassPath] )

* * @var array(string => array(string => string [, string => string]) ) */ private static $__formats = array( 'json' => array(self::KEY_CLASS => 'Asra_Export_Format_Json'), 'serial' => array(self::KEY_CLASS => 'Asra_Export_Format_Serial'), 'vars' => array(self::KEY_CLASS => 'Asra_Export_Format_Vars'), 'xml' => array(self::KEY_CLASS => 'Asra_Export_Format_Xml'), 'yaml' => array(self::KEY_CLASS => 'Asra_Export_Format_Yaml'), ); //--------------------------------------- // CONSTRUCTOR //--------------------------------------- /** * Don't allow clones */ // @codeCoverageIgnoreStart final private function __clone() { } /** * Don't allow construction */ final private function __construct() { } // @codeCoverageIgnoreEnd //--------------------------------------- // METHODS //--------------------------------------- /** * Removes all registered formats from the registry. * * @return void */ public static function clear() { self::$__formats = array(); } /** * Gets the class name associated with the format key. * * @param string $key The format key to retrieve class for. * @return string|array Name of the class or an array of [name, classpath]. */ public static function getClassName($key) { if (self::hasFormat($key)) { $format = isset(self::$__formats[$key][self::KEY_CLASS]) ? self::$__formats[$key][self::KEY_CLASS] : null; return $format; } else { require_once "Asra/Export/Exception.php"; throw new Asra_Export_Exception('No format found by that key'); } } /** * Gets the class path associated with the format key. * * @param string $key The format key to retrieve class for. * @return string|array Name of the class or an array of [name, classpath]. */ public static function getClassPath($key) { if (self::hasFormat($key)) { if (!array_key_exists("path", self::$__formats[$key])) return null; return self::$__formats[$key]['path']; } else { require_once "Asra/Export/Exception.php"; throw new Asra_Export_Exception('No format found by that key'); } } /** * Return registered formats. * * @return array */ public static function getFormats() { return self::$__formats; } /** * Test to see if a key is registered. * * @param string $key The format key to test for * @return bool */ public static function hasFormat($key) { $key = self::sanitize($key); return array_key_exists($key, self::$__formats); } /** * Register a key with a class for export. After * registering, the key and class with be available for * export (assuming the class is in the same classpath as the Asra * library). An explicit path can be added with the third parameter * or you can add a classpath with set_include_path in your bootstrap. * * @param string $key Key to activate the export type in the url. * @param string $className Name of export class. * @param string $classPath Full path to class. * @return void */ public static function register($key, $className, $classPath = null) { $key = self::sanitize($key); self::$__formats[$key] = array( self::KEY_CLASS => $className, self::KEY_PATH => $classPath, ); } /** * Register Asra formats by a pipe delimited string. * * @param string $types A pipe delmited string of types. * @param bool $clear Clears the registered types if true. * @return void */ public static function registerAsString($types, $clear = true) { if ($clear) Asra_Export_Registrar::clear(); if (strlen($types)) { $parts = explode("|", $types); foreach($parts as $key => $format) { $format = Asra_Export_Registrar::sanitize($format); if (!strlen($format)) unset($parts[$key]); $className = 'Asra_Export_Format_' . ucfirst($format); Asra_Export_Registrar::register($format, $className); } if (count($parts)) { Asra_Export_Registrar::setDefaultType($parts[0]); } } } /** * Sanitize format key. * * @param string $key Key to format. * @return string */ public static function sanitize($key) { return strtolower($key); } /** * Sets default exports for Asra * * @return void */ public static function setDefaultFormats() { self::$__formats = array( 'json' => array(self::KEY_CLASS => 'Asra_Export_Format_Json'), 'serial' => array(self::KEY_CLASS => 'Asra_Export_Format_Serial'), 'vars' => array(self::KEY_CLASS => 'Asra_Export_Format_Vars'), 'xml' => array(self::KEY_CLASS => 'Asra_Export_Format_Xml'), 'yaml' => array(self::KEY_CLASS => 'Asra_Export_Format_Yaml'), ); } /** * Override the default export type. * * @param string $type The export type of your choice. * @return void */ public static function setDefaultType($type) { Asra_Export_Registrar::$defaultType = $type; } /** * Remove a key from the formats. * * @param string $key Key to unregister. * @return void */ public static function unregister($key) { $key = self::sanitize($key); if (self::hasFormat($key)) { unset(self::$__formats[$key]); } } }