VirtualBox

Changeset 28775 in vbox for trunk


Ignore:
Timestamp:
Apr 26, 2010 6:44:11 PM (15 years ago)
Author:
vboxsync
Message:

Webservice: refreshed PHP bindings from James Lucas

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/webservice/websrv-php.xsl

    r27646 r28775  
    270270</xsl:template>
    271271
     272<xsl:template name="structcollection">
     273   <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
     274   <xsl:text>
     275/**
     276* Generated VBoxWebService Struct Collection
     277*/</xsl:text>
     278class <xsl:value-of select="$ifname"/>Collection extends VBox_StructCollection {
     279   protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
     280}
     281</xsl:template>
     282
    272283<xsl:template name="genreq">
    273284       <xsl:param name="wsmap" />
     
    334345</xsl:template>
    335346
     347<xsl:template name="enumcollection">
     348   <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
     349   <xsl:text>
     350/**
     351* Generated VBoxWebService Enum Collection
     352*/</xsl:text>
     353class <xsl:value-of select="$ifname"/>Collection extends VBox_EnumCollection {
     354   protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
     355}
     356</xsl:template>
     357
    336358<xsl:template match="/">
    337359<xsl:text>&lt;?php
     
    360382    protected $handle;
    361383
    362     public function  __construct($soap, $handle = null)
     384    public function __construct($soap, $handle = null)
    363385    {
    364386        $this->connection = $soap;
     
    414436}
    415437
    416 class VBox_ManagedObjectCollection implements Iterator, Countable {
    417     protected $connection;
    418     protected $handles;
    419     protected $_interfaceName = null;
    420 
    421     public function __construct($soap, array $handles = array())
    422     {
    423         $this->connection = $soap;
    424         $this->handles = $handles;
    425     }
    426 
     438abstract class VBox_Collection implements ArrayAccess, Iterator, Countable {
     439    protected $_connection;
     440    protected $_values;
     441    protected $_objects;
     442    protected $_interfaceName;
     443
     444    public function __construct($soap, array $values = array()) {
     445        $this->_connection = $soap;
     446        $this->_values = $values;
     447        $this->_soapToObject();
     448    }
     449
     450    protected function _soapToObject() {
     451        $this->_objects = array();
     452        foreach($this->_values as $value)
     453        {
     454            $this->_objects[] = new $this->_interfaceName($this->_connection, $value);
     455        }
     456    }
     457
     458    /** ArrayAccess Functions **/
     459    public function offsetSet($offset, $value) {
     460        if ($value instanceof $this->_interfaceName)
     461        {
     462            if ($offset)
     463            {
     464                $this->_objects[$offset] = $value;
     465            }
     466            else
     467            {
     468                $this->_objects[] = $value;
     469            }
     470        }
     471        else
     472        {
     473            throw new Exception("Value must be a instance of " . $this->_interfaceName);
     474        }
     475    }
     476
     477    public function offsetExists($offset) {
     478        return isset($this->_objects[$offset]);
     479    }
     480
     481    public function offsetUnset($offset) {
     482        unset($this->_objects[$offset]);
     483    }
     484
     485    public function offsetGet($offset) {
     486        return isset($this->_objects[$offset]) ? $this->_objects[$offset] : null;
     487    }
     488
     489    /** Iterator Functions **/
    427490    public function rewind() {
    428         reset($this->handles);
     491        reset($this->_objects);
    429492    }
    430493
    431494    public function current() {
    432         $handle = current($this->handles);
    433         if ($handle !== false &amp;&amp; !$handle instanceof $this->_interfaceName) {
    434             $handle = new $this->_interfaceName($this->connection, $handle);
    435         }
    436         return $handle;
     495        return current($this->_objects);
    437496    }
    438497
    439498    public function key() {
    440         $handle = key($this->handles);
    441         return $handle;
     499        return key($this->_objects);
    442500    }
    443501
    444502    public function next() {
    445         $handle = next($this->handles);
    446         return $handle;
     503        return next($this->_objects);
    447504    }
    448505
    449506    public function valid() {
    450         $handle = $this->current() !== false;
    451         return $handle;
    452     }
    453 
     507        return ($this->current() !== false);
     508    }
     509
     510    /** Countable Functions **/
    454511    public function count() {
    455         return count($this->handles);
     512        return count($this->_objects);
     513    }
     514}
     515
     516class VBox_ManagedObjectCollection extends VBox_Collection {
     517    protected $_interfaceName = 'VBox_ManagedObject';
     518
     519    // Result is undefined if this is called AFTER any call to VBox_Collection::offsetSet or VBox_Collection::offsetUnset
     520    public function setInterfaceName($interface) {
     521       if (!is_subclass_of($interface, 'VBox_ManagedObject'))
     522       {
     523           throw new Exception('Cannot set collection interface to non-child class of VBox_ManagedObject');
     524       }
     525       $this->_interfaceName = $interface;
     526       $this->_soapToObject();
    456527    }
    457528}
     
    470541}
    471542
     543abstract class VBox_StructCollection extends VBox_Collection {
     544
     545    public function __construct($soap, array $values = array())
     546    {
     547        if (!(array_values($values) === $values))
     548        {
     549            $values = array((object)$values); //Fix for when struct return value only contains one list item (e.g. one medium attachment)
     550        }
     551        parent::__construct($soap, $values);
     552    }
     553}
     554
    472555abstract class VBox_Enum {
    473    protected $handle;
     556   protected $_handle;
    474557
    475558   public function __construct($connection, $handle)
    476559   {
    477560       if (is_string($handle))
    478            $this->handle = $this->ValueMap[$handle];
     561           $this->_handle = $this->ValueMap[$handle];
    479562       else
    480            $this->handle = $handle;
     563           $this->_handle = $handle;
    481564   }
    482565
    483566   public function __toString()
    484567   {
    485        return (string)$this->NameMap[$this->handle];
    486    }
     568       return (string)$this->NameMap[$this->_handle];
     569   }
     570}
     571
     572abstract class VBox_EnumCollection extends VBox_Collection {
    487573}
    488574
     
    494580   <xsl:for-each select="//interface[@wsmap='struct']">
    495581       <xsl:call-template name="interfacestruct"/>
    496        <xsl:call-template name="collection"/>
     582       <xsl:call-template name="structcollection"/>
    497583  </xsl:for-each>
    498584  <xsl:for-each select="//enum">
    499585       <xsl:call-template name="enum"/>
    500        <xsl:call-template name="collection"/>
     586       <xsl:call-template name="enumcollection"/>
    501587  </xsl:for-each>
    502588
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette