Horde_SyncMl_Command::factory PHP Method

factory() public method

Attempts to return a concrete Horde_SyncMl_Command instance based on $command.
public factory ( string $command, Horde_SyncMl_XmlOutput &$outputHandler ) : Horde_SyncMl_Command
$command string The type of the concrete Horde_SyncMl_Comment subclass to return.
$outputHandler Horde_SyncMl_XmlOutput A Horde_SyncMl_XmlOutput object.
return Horde_SyncMl_Command The newly created concrete Horde_SyncMl_Command instance, or false on error.
    public function &factory($command, &$outputHandler)
    {
        $command = basename($command);
        $class = 'Horde_SyncMl_Command_' . $command;
        if (class_exists($class)) {
            $cmd = new $class($outputHandler);
        } else {
            $msg = 'Class definition of ' . $class . ' not found.';
            $GLOBALS['backend']->logMessage($msg, __FILE__, __LINE__, 'ERR');
            $cmd = PEAR::raiseError($msg);
        }
        return $cmd;
    }

Usage Example

Example #1
0
 /**
  * Callback public function called by WBXML parser.
  */
 public function startElement($uri, $element, $attrs)
 {
     $this->_Stack[] = $element;
     // <SyncML>: don't do anyhting yet
     if (count($this->_Stack) == 1) {
         return;
     }
     // header or body?
     if ($this->_Stack[1] == 'SyncHdr') {
         if (count($this->_Stack) == 2) {
             $this->_currentCommand = new Horde_SyncMl_Command_SyncHdr($this->_xmlWriter);
         }
         $this->_currentCommand->startElement($uri, $element, $attrs);
     } else {
         switch (count($this->_Stack)) {
             case 2:
                 // <SyncBody>: do nothing yet
                 break;
             case 3:
                 // new Command:
                 // <SyncML><SyncBody><[Command]>
                 $this->_currentCommand =& Horde_SyncMl_Command::factory($element, $this->_xmlWriter);
                 $this->_currentCommand->startElement($uri, $element, $attrs);
                 break;
             default:
                 // pass on to current command handler:
                 // <SyncML><SyncBody><Command><...>
                 $this->_currentCommand->startElement($uri, $element, $attrs);
                 break;
         }
     }
 }