Loading EWSTypes #35

Closed
ityndall opened this Issue Apr 26, 2012 · 3 comments

Comments

Projects
None yet
3 participants

Personally, it's a PIA to load each individual EWSType. In addition, I don't want to load all of them by looping over all the classes in EWSType directory.

<?php
/**
 * load the needed EWSTypes
 * 
 * @author Ian Tyndall <http://blog.ianty.com>
 */
class EWSType_Factory {
    /**
     * type loader
     */
    public static function type($type)
    {
        if (include_once 'EWSType/' . $type . 'Type.php') {
            $classname = "EWSType_$type" . 'Type';
            return new $classname;
        } else {
            throw new Exception('Type not found');
        }
    }
} // end class EWSType_Factory

This updates the first example you provided to this:

<?php
require_once('ExchangeWebServices.php');
require_once('EWS_Exception.php');
require_once('EWSType.php');
require_once('EWSType_Factory.php');
require_once('NTLMSoapClient.php');
require_once('NTLMSoapClient/Exchange.php');

$host = 'foo-ews.mysite.com';
$username = 'username'; 
$password = 'password';

$ews = new ExchangeWebServices($host, $username, $password);
$traversal = EWSType_Factory::type('FolderQueryTraversal');
$shape_names = EWSType_Factory::type('DefaultShapeNames');
$folderid_names = EWSType_Factory::type('DistinguishedFolderIdName');

// start building the find folder request
$request = EWSType_Factory::type('FindFolder');
$request->Traversal = $traversal::SHALLOW;
$request->FolderShape = EWSType_Factory::type('FolderResponseShape');
$request->FolderShape->BaseShape = $shape_names::ALL_PROPERTIES;

// configure the view
$request->IndexedPageFolderView = EWSType_Factory::type('IndexedPageView');
$request->IndexedPageFolderView->BasePoint = 'Beginning';
$request->IndexedPageFolderView->Offset = 0;

// set the starting folder as the inbox
$request->ParentFolderIds = EWSType_Factory::type('NonEmptyArrayOfBaseFolderIds');
$request->ParentFolderIds->DistinguishedFolderId = EWSType_Factory::type('DistinguishedFolderId');
$request->ParentFolderIds->DistinguishedFolderId->Id = $folderid_names::INBOX;

// make the actual call
$response = $ews->FindFolder($request);
echo '<pre>'.print_r($response, true).'</pre>';
Owner

jamesiarmes commented Apr 26, 2012

A much better way to tackle this issue would be to provide an autoload implementation that could be registered with spl_autoload_register(). I have planned to tackle this after issue #4, but if somebody would like to contribute one via a pull request in the interim I would be open to reviewing and possibly merging it.

ameir commented Aug 23, 2012

I use the following; feel free to use or adapt it if you'd like:
#36 (comment)

jamesiarmes added this to the Version 1.0 milestone Dec 19, 2014

Owner

jamesiarmes commented Jul 30, 2016

This can be done via composer with b227db4 in the psr-4 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment