/** * Return generated PHP source code. That's where we generate bindings code * * @return string */ private function getPHP($dir) { $phpfile = $this->getXmlForPhp(); if ($phpfile == '' && $this->getXmlSource() == '') { throw new \RuntimeException('There is no XML generated'); } $dom = new \DOMDocument(); //print_r($this->getXmlSource()); if ($this->getXmlSource() != '') { $dom->loadXML($this->getXmlSource(), LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NOENT | LIBXML_XINCLUDE); } else { $dom->load($phpfile, LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NOENT | LIBXML_XINCLUDE); } $xPath = new \DOMXPath($dom); $classes = $xPath->query('//classes/class'); $sourceCode = array(); foreach ($classes as $class) { $phpClass = new PHPClass(); $phpClass->name = $class->getAttribute('name'); if ($class->getAttribute('type') != '') { $phpClass->type = $class->getAttribute('type'); } if ($class->getAttribute('simpleType') != '') { $phpClass->type = $class->getAttribute('simpleType'); } if ($class->getAttribute('namespace') != '') { $phpClass->namespace = $class->getAttribute('namespace'); } if ($class->getElementsByTagName('extends')->length > 0) { $rawType = $type = $class->getElementsByTagName('extends')->item(0)->getAttribute('name'); $extendsNamespace = $this->namespaceToPhp($class->getElementsByTagName('extends')->item(0)->getAttribute('namespace')); $colonPos = strpos($type, ':'); if (empty($extendsNamespace) && $colonPos !== false) { $ns = substr($type, 0, $colonPos); if (!empty($this->shortNamespaces[$ns]) && $this->shortNamespaces[$ns] == 'http://www.w3.org/2001/XMLSchema') { $type = substr($type, $colonPos + 1); if (empty($phpClass->type)) { $phpClass->type = $type; } } } if (!in_array($type, $this->basicTypes)) { $phpClass->extends = $type; $phpClass->type = $rawType; $phpClass->extendsNamespace = $extendsNamespace; } } $docs = $xPath->query('docs/doc', $class); $docBlock = array(); //if ($phpClass->namespace != $this->xsdNs) { $docBlock['xmlNamespace'] = $this->expandNS($phpClass->namespace); //} $docBlock['xmlType'] = $phpClass->type; $docBlock['xmlName'] = $phpClass->name; if ($phpClass->namespace != '') { $docBlock['var'] = $this->namespaceToPhp($this->expandNS($phpClass->namespace)) . "\\" . $phpClass->name; } else { $docBlock['var'] = $phpClass->name; } foreach ($docs as $doc) { if ($doc->nodeValue != '') { $docBlock["xml" . $doc->getAttribute('name')] = $doc->nodeValue; } elseif ($doc->getAttribute('value') != '') { $docBlock["xml" . $doc->getAttribute('name')] = $doc->getAttribute('value'); } } $phpClass->classDocBlock = $docBlock; $props = $xPath->query('property', $class); $properties = array(); $setterArr = []; $i = 0; $isArray = false; foreach ($props as $prop) { $properties[$i]['name'] = $prop->getAttribute('name'); $docs = $xPath->query('docs/doc', $prop); foreach ($docs as $doc) { $properties[$i]["docs"][$doc->getAttribute('name')] = $doc->nodeValue; } if ($prop->getAttribute('xmlType') != '') { $properties[$i]["docs"]['xmlType'] = $prop->getAttribute('xmlType'); } if ($prop->getAttribute('namespace') != '') { $properties[$i]["docs"]['xmlNamespace'] = $this->expandNS($prop->getAttribute('namespace')); } if ($prop->getAttribute('minOccurs') != '') { $properties[$i]["docs"]['xmlMinOccurs'] = $prop->getAttribute('minOccurs'); } if ($prop->getAttribute('maxOccurs') != '') { $properties[$i]["docs"]['xmlMaxOccurs'] = $prop->getAttribute('maxOccurs'); // If maxOccurs > 1, mark type as an array if ($prop->getAttribute('maxOccurs') > 1) { $isArray = $prop->getAttribute('maxOccurs'); } elseif ($prop->getAttribute('maxOccurs') == 'unbounded') { $isArray = true; } } if ($prop->getAttribute('name') != '') { $properties[$i]["docs"]['xmlName'] = $prop->getAttribute('name'); } //@todo if $prop->getAttribute('maxOccurs') > 1 - var can be an array - in future special accessor cane be implemented if ($prop->getAttribute('type') != '' && $prop->getAttribute('typeNamespace') == '') { /** * In general it's stange to give to Type name's namespace. Reconsider this part */ if ($prop->getAttribute('namespace') != '' && $prop->getAttribute('namespace') != $this->xsdNs) { $ns = ""; if ($prop->getAttribute('namespace') == "#default#") { $ns = $this->namespaceToPhp($this->targetNamespace); } else { $ns = $this->namespaceToPhp($this->expandNS($prop->getAttribute('namespace'))); } $properties[$i]["docs"]['var'] = $ns . '\\' . $prop->getAttribute('type'); } else { $properties[$i]["docs"]['var'] = $prop->getAttribute('type'); } // Is it unbounded array? if ($isArray === true) { $properties[$i]["docs"]['var'] = $properties[$i]["docs"]['var'] . "[]"; $isArray = false; } // Is it array with defined maximum amount of elements? if ($isArray > 1) { $properties[$i]["docs"]['var'] = $properties[$i]["docs"]['var'] . "[" . $isArray . "]"; $isArray = false; } } if ($prop->getAttribute('type') != '' && $prop->getAttribute('typeNamespace') != '') { $ns = ""; if ($prop->getAttribute('typeNamespace') == "#default#") { $ns = $this->namespaceToPhp($this->targetNamespace); } else { $ns = $this->namespaceToPhp($this->expandNS($prop->getAttribute('typeNamespace'))); } if ($prop->getAttribute('typeNamespace') == $this->xsdNs) { $properties[$i]["docs"]['var'] = $this->normalizeType($prop->getAttribute('type')); } else { $properties[$i]["docs"]['var'] = $ns . '\\' . $prop->getAttribute('type'); } } $i++; } $phpClass->classProperties = $properties; $namespaceClause = ''; if ($docBlock['xmlNamespace'] != '') { $namespaceClause = "namespace " . $this->namespaceToPhp($docBlock['xmlNamespace']) . ";\n"; } $sourceCode[$docBlock['xmlName'] . "|" . $phpClass->namespace] = "<?php\n" . $namespaceClause . $phpClass->getPhpCode($dir); } return $sourceCode; }
/** * getMethodsFromLibraryFile * * @param string $file * @return object(PHPClass) $parsedLibrary */ function getMethodsFromLibraryFile($file) { // parse class comments from file $parsedLibrary = new PHPClass(); //$success = $parsedLibrary->parseFromFile ( PATH_CORE . "classes" . PATH_SEP . $file ); $success = $parsedLibrary->parseFromFile($file); return $parsedLibrary; }
public function __toString() { $result = parent::__toString(); $result = preg_replace('/^class /', 'trait ', $result); return $result; }
public function __toString() { $result = 'abstract ' . parent::__toString(); return $result; }