Re: Namespaced code with SabreDAV

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Oct 6, 2011 at 1:45 PM, Tommy Pham <tommyhp2@xxxxxxxxx> wrote:

> On Thu, Oct 6, 2011 at 7:37 AM, Andrew Mason <slackmase2@xxxxxxxxx> wrote:
>
>> Hello all,
>> I am trying to use the wonderful SabreDAV library to create a webdav
>> share. I have a demo up and running however the framework / class i'm
>> using is namespaced, and SabreDAV unfortunately does not have a 5.3
>> style namespace declaration.
>>
>> I have an spl_autoload function registered in my base controller and
>> so long as I prefix the classname with a \ it all works:
>>
>>
>>  $rootDirectory = new \Sabre_DAV_FS_Directory($rootDir);
>>
>> // The server object is responsible for making sense out of the WebDAV
>> protocol
>> $server = new \Sabre_DAV_Server($rootDirectory);
>>
>>
>> However, SabreDAV it's self has a large amount of other PHP classes
>> which it calls which obviously aren't prefixed with '\'
>>
>> Is there a way i can tell PHP any class name that get's instanciated
>> with 'Sabre_' should resolve to '\Sabre' ?
>>
>> Many thanks
>> Andrew
>>
>>
> If my memory serves regarding PHP's namespace, you could build something
> like this
>
>         $file = APP_DIR.'\'.$class;
>         if( file_exists( $file ) ) require_once $file;
>
> into your autoloader.  You may have to adjust '/' to/from '\\' depending on
> your platform or you can use the constant DIRECTORY_SEPARATOR in the full
> path to the class.  Then you don't need add \ before your class to
> instantiate.  I don't know how the SabreDAV framework looks like but you may
> want to look at how Zend framework loads their class as to give you some
> idea what you may need to do in your autoload to circumvent SabreDAV being
> not 5.3 namespace declaration.  You may also want to take a look at
> CodeIgniter's autoloading mechanism.
>
> Regards,
> Tommy
>

Here's another idea that you could do with the autoloader.  The autoloader
is slightly modified from PureMVC's site to suit my needs.  Using namespace
and everything that's PHP v5.3+, this is part of PureMVC framework that I've
ported over from the original Flash code a while back.

define( 'PMVC_BASE_DIR', str_replace( '\\', '/', __DIR__ ).'/' );
function __autoload( $class )
{
    if( stristr( $class, '\\' ) )
    {
        $file = APP_DIR.str_replace( '\\', '/', $class ).EXT;
        if( file_exists( $file ) ) require_once $file;
        else  throw new \Exception( 'Unable to find class '.$class.
                ' in the expected location '.$file );
    } else
    {
        $_basePaths = array(
PMVC_BASE_DIR.'org/puremvc/php/patterns/facade/',
            PMVC_BASE_DIR.'org/puremvc/php/interfaces/',
            PMVC_BASE_DIR.'org/puremvc/php/core/',
            PMVC_BASE_DIR.'org/puremvc/php/patterns/',
            PMVC_BASE_DIR.'org/puremvc/php/patterns/command/',
            PMVC_BASE_DIR.'org/puremvc/php/patterns/mediator/',
            PMVC_BASE_DIR.'org/puremvc/php/patterns/observer/',
            PMVC_BASE_DIR.'org/puremvc/php/patterns/proxy/' );
        $classPaths = array_merge( explode( PATH_SEPARATOR,
get_include_path() ), $_basePaths );
        foreach( $classPaths as $classPath )
        {
            $path = $classPath.$class.EXT;
            if( file_exists( $path ) )
            {
                require_once $path;
                return;
            }
        }
        throw new \Exception( 'Unable to find class '.$class.' in '.
            implode( PATH_SEPARATOR, $classPaths ) );
    }
}

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux