Location: PHPKode > scripts > RRoEmbed > romac-RRoEmbed-d4f44ea/Classes/Autoloader.class.php
<?php

declare( ENCODING = 'utf-8' );

/*
 * Copyright (c) 2010 Romain Ruetschi <hide@address.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Source file containing class RRoEmbed_Autoloader.
 * 
 * @package    RRoEmbed
 * @license    http://opensource.org/licenses/mit-license.html MIT License
 * @author     Romain Ruetschi <hide@address.com>
 * @version    0.2
 * @see        RRoEmbed_Autoloader
 */

/**
 * Class RRoEmbed_Autoloader.
 * 
 * @todo       Description for class RRoEmbed_Autoloader.
 *
 * @package    RRoEmbed
 * @license    http://opensource.org/licenses/mit-license.html MIT License
 * @author     Romain Ruetschi <hide@address.com>
 * @version    0.2
 */
class RRoEmbed_Autoloader
{
    
    /**
     * A reference to the unique class instance.
    *
     * @var RRoEmbed_Autoloader object.
     */
    private static $_instance    = NULL;
    
    /**
     * The path to the directory where the classes are stored.
     *
     * @var string
     */
    protected $_classesDirectory = '';


    /**
     * Class constructor.
     * The constructor of this class cannot be used to instanciate a RRoEmbed_Autoloader object.
     * This class is a singleton class, this means that there is only one instance
     * of the RRoEmbed_Autoloader class and this instance is shared by every one who does a call
     * to RRoEmbed_Autoloader::getInstance().
     *
     * @see getInstance to know how to create the RRoEmbed_Autoloader instance or to get a reference to it.
     */
    private function __construct()
    {
        $this->_classesDirectory = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
    }

    /**
     * Creates or returns the only instance of the RRoEmbed_Autoloader class.
     *
     * @return RRoEmbed_Autoloader the only instance of the RRoEmbed_Autoloader class.
     */
    public static function getInstance()
    {
        if( !is_object( self::$_instance ) )
        {
            self::$_instance = new self();
        }
        
        return self::$_instance;
    }

    /**
     * Create a clone of the instance.
     * Because this class is a singleton class, there can exists only one
     * instance of this class.
     * So this method will throw an exception if someone tries to call it.
     *
     * @throws BadMethodCallException An new BadMethodCallException instance
     * @return void
     */
    final public function __clone()
    {
        throw new BadMethodCallException(
            'This class is a singleton class, you are not allowed to clone it.' . "\n" .
            'Please call ' . get_class( $this ) . '::getInstance() to get a reference to ' .
            'the only instance of this class.'
        );
    }

    /**
     * This method is called when unserialize is called on a serialized representation
     * of an instance of this class.
     * Because this class is a singleton class, there can exists only one
     * instance of this class.
     * So this method will throw an exception if someone tries to call it.
     *
     * @throws BadMethodCallException An new BadMethodCallException instance
     * @return void
     */
    final public function __wakeup()
    {
        throw new BadMethodCallException(
            'This class is a singleton class, you are not allowed to unserialize ' .
            'it as this could create a new instance of it.' . "\n" .
            'Please call ' . get_class( $this ) . '::getInstance() to get a reference to ' .
            'the only instance of this class.'
        );
    }
    
    /**
     * Register the loadClass method of this class as an SPL Autoload callback.
     *
     * @return boolean
     * @author Romain Ruetschi <hide@address.com>
     */
    public function register()
    {
        if( !function_exists( 'spl_autoload_register' ) )
        {
            require_once( $this->_classesDirectory . 'Exception.class.php' );
            
            throw new RRoEmbed_Exception(
                'The SPL extension is not loaded, you need to load it in order '
              . 'to use this library.'
            );
        }
        
        return spl_autoload_register( array( $this, 'loadClass' ) );
    }
    
    /**
     * Unregister the SPL Autoload callback.
     *
     * @return boolean
     * @author Romain Ruetschi <hide@address.com>
     */
    public function unregister()
    {
        if( !function_exists( 'spl_autoload_unregister' ) )
        {
            require_once( $this->_classesDirectory . 'Exception.class.php' );
            
            throw new RRoEmbed_Exception(
                'The SPL extension is not loaded, you need to load it in order '
              . 'to use this library.'
            );
        }
        
        return spl_autoload_unregister( array( $this, 'loadClass' ) );
    }
    
    /**
     * Load the specified RRoEmbed class.
     *
     * @param  string $className The name of the class to load.
     * @return boolean TRUE if the class has been successfully loaded, FAlSE otherwise.
     * @author Romain Ruetschi <hide@address.com>
     */
    public function loadClass( $className )
    {
        if( strpos( $className, 'RRoEmbed' ) !== 0 )
        {
            return FALSE;
        }
        
        if( class_exists( $className, FALSE ) || interface_exists( $className, FALSE ) )
        {
            return TRUE;
        }
        
        $fileName = $this->_classesDirectory
                  . str_replace( '_', DIRECTORY_SEPARATOR, substr( $className, 9 ) )
                  . '.class.php';
        
        if( !file_exists( $fileName ) )
        {
            return FALSE;
        }

        require_once( $fileName );
        
        return class_exists( $className, FALSE ) || interface_exists( $className, FALSE );
    }
    
}
Return current item: RRoEmbed