Re: [PHP] Namespaced code with SabreDAV

2011-10-07 Thread Richard Quadling
On 6 October 2011 15:37, Andrew Mason slackma...@gmail.com 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



[PHP] Namespaced code with SabreDAV

2011-10-06 Thread Andrew Mason
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

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



Re: [PHP] Namespaced code with SabreDAV

2011-10-06 Thread Tommy Pham
On Thu, Oct 6, 2011 at 7:37 AM, Andrew Mason slackma...@gmail.com 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


Re: [PHP] Namespaced code with SabreDAV

2011-10-06 Thread Tommy Pham
On Thu, Oct 6, 2011 at 1:45 PM, Tommy Pham tommy...@gmail.com wrote:

 On Thu, Oct 6, 2011 at 7:37 AM, Andrew Mason slackma...@gmail.com 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 ) );
}
}