ezcWorkflowDefinitionStorageXml::loadFromDocument PHP Method

loadFromDocument() public method

Load a workflow definition from a DOMDocument.
public loadFromDocument ( DOMDocument $document ) : ezcWorkflow
$document DOMDocument
return ezcWorkflow
    public function loadFromDocument(DOMDocument $document)
    {
        $workflowName = $document->documentElement->getAttribute('name');
        $workflowVersion = (int) $document->documentElement->getAttribute('version');
        // Create node objects.
        $nodes = array();
        $xmlNodes = $document->getElementsByTagName('node');
        foreach ($xmlNodes as $xmlNode) {
            $id = (int) $xmlNode->getAttribute('id');
            $className = 'ezcWorkflowNode' . $xmlNode->getAttribute('type');
            if (class_exists($className)) {
                $configuration = call_user_func_array(array($className, 'configurationFromXML'), array($xmlNode));
                if (is_null($configuration)) {
                    $configuration = ezcWorkflowUtil::getDefaultConfiguration($className);
                }
            }
            $node = new $className($configuration);
            $node->setId($id);
            if ($node instanceof ezcWorkflowNodeFinally && !isset($finallyNode)) {
                $finallyNode = $node;
            } else {
                if ($node instanceof ezcWorkflowNodeEnd && !isset($defaultEndNode)) {
                    $defaultEndNode = $node;
                } else {
                    if ($node instanceof ezcWorkflowNodeStart) {
                        $startNode = $node;
                    }
                }
            }
            $nodes[$id] = $node;
        }
        if (!isset($startNode) || !isset($defaultEndNode)) {
            throw new ezcWorkflowDefinitionStorageException('Could not load workflow definition.');
        }
        // Connect node objects.
        foreach ($xmlNodes as $xmlNode) {
            $id = (int) $xmlNode->getAttribute('id');
            $className = 'ezcWorkflowNode' . $xmlNode->getAttribute('type');
            foreach ($xmlNode->getElementsByTagName('outNode') as $outNode) {
                $nodes[$id]->addOutNode($nodes[(int) $outNode->getAttribute('id')]);
            }
            if (is_subclass_of($className, 'ezcWorkflowNodeConditionalBranch')) {
                foreach (ezcWorkflowUtil::getChildNodes($xmlNode) as $childNode) {
                    if ($childNode->tagName == 'condition') {
                        foreach ($childNode->getElementsByTagName('else') as $elseNode) {
                            foreach ($elseNode->getElementsByTagName('outNode') as $outNode) {
                                $elseId = (int) $outNode->getAttribute('id');
                            }
                        }
                        $condition = self::xmlToCondition($childNode);
                        $xpath = new DOMXPath($childNode->ownerDocument);
                        foreach ($xpath->query('outNode', $childNode) as $outNode) {
                            if (!isset($elseId)) {
                                $nodes[$id]->addConditionalOutNode($condition, $nodes[(int) $outNode->getAttribute('id')]);
                            } else {
                                $nodes[$id]->addConditionalOutNode($condition, $nodes[(int) $outNode->getAttribute('id')], $nodes[$elseId]);
                                unset($elseId);
                            }
                        }
                    }
                }
            }
        }
        if (!isset($finallyNode) || count($finallyNode->getInNodes()) > 0) {
            $finallyNode = null;
        }
        // Create workflow object and add the node objects to it.
        $workflow = new ezcWorkflow($workflowName, $startNode, $defaultEndNode, $finallyNode);
        $workflow->definitionStorage = $this;
        $workflow->version = $workflowVersion;
        // Handle the variable handlers.
        foreach ($document->getElementsByTagName('variableHandler') as $variableHandler) {
            $workflow->addVariableHandler($variableHandler->getAttribute('variable'), $variableHandler->getAttribute('class'));
        }
        // Verify the loaded workflow.
        $workflow->verify();
        return $workflow;
    }