Re: Namespaced code with SabreDAV

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

 



On 6 October 2011 15:37, 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


I've just downloaded SabreDav 1.5.3

Put Sabre directory in my include path.

And using the following code, ...

<?php
namespace GoingToBeUsingSabre;

require_once 'Sabre/autoload.php';

var_dump(new \Sabre_DAV_FS_Directory('D:/'));
print_r(array_filter(get_declared_classes(), function($class){ return
'Sabre_' === substr($class, 0, 6);}));
print_r(get_included_files());
?>

outputs ...

object(Sabre_DAV_FS_Directory)#1 (1) {
  ["path":protected]=>
  string(3) "D:/"
}
Array
(
    [166] => Sabre_DAV_FS_Node
    [167] => Sabre_DAV_FS_Directory
)
Array
(
    [0] => Z:\UsingSabre.php
    [1] => D:\PHP\Includes\Sabre\autoload.php
    [2] => D:\PHP\Includes\Sabre\DAV\FS\Directory.php
    [3] => D:\PHP\Includes\Sabre\DAV\FS\Node.php
    [4] => D:\PHP\Includes\Sabre\DAV\INode.php
    [5] => D:\PHP\Includes\Sabre\DAV\ICollection.php
    [6] => D:\PHP\Includes\Sabre\DAV\IQuota.php
)

They provide their own autoloader. As you can see, I've included their
autoloader and made 1 call to a root namespaced class. The registered
autoloader took care of the rest and loaded the additional classes as
needed.

Take a look at the Zend Framework's autoloader
(http://framework.zend.com/manual/en/zend.loader.autoloader.html)

Here is the same working example, but using ZF's autoloader ...

<?php
namespace GoingToBeUsingSabre;

require_once 'Zend/Loader/Autoloader.php';
$o_ZendLoaderAutoLoader = \Zend_Loader_Autoloader::getInstance();
$o_ZendLoaderAutoLoader->registerNamespace('Sabre_');

var_dump(new \Sabre_DAV_FS_Directory('D:/'));
print_r(array_filter(get_declared_classes(), function($class){ return
'Sabre_' === substr($class, 0, 6);}));
print_r(get_included_files());
?>

output now is ...

object(Sabre_DAV_FS_Directory)#2 (1) {
  ["path":protected]=>
  string(3) "D:/"
}
Array
(
    [168] => Sabre_DAV_FS_Node
    [169] => Sabre_DAV_FS_Directory
)
Array
(
    [0] => Z:\UsingSabre.php
    [1] => D:\PHP\Includes\Zend\Loader\Autoloader.php
    [2] => D:\PHP\Includes\Zend\Loader.php
    [3] => D:\PHP\Includes\Sabre\DAV\FS\Directory.php
    [4] => D:\PHP\Includes\Sabre\DAV\FS\Node.php
    [5] => D:\PHP\Includes\Sabre\DAV\INode.php
    [6] => D:\PHP\Includes\Sabre\DAV\ICollection.php
    [7] => D:\PHP\Includes\Sabre\DAV\IQuota.php
)

Regards,

Richard.

-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[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