Example #1
0
 public function startVisitingClass(PhpClass $class)
 {
     if ($namespace = $class->getNamespace()) {
         $this->writer->write('namespace ' . $namespace . ';' . "\n\n");
     }
     if ($files = $class->getRequiredFiles()) {
         foreach ($files as $file) {
             if ($file instanceof RelativePath) {
                 $this->writer->writeln('require_once __DIR__ . ' . var_export('/' . $file->getRelativePath(), true) . ';');
                 continue;
             }
             $this->writer->writeln('require_once ' . var_export($file, true) . ';');
         }
         $this->writer->write("\n");
     }
     if ($useStatements = $class->getUseStatements()) {
         foreach ($useStatements as $alias => $namespace) {
             $this->writer->write('use ' . $namespace);
             if (substr($namespace, strrpos($namespace, '\\') + 1) !== $alias) {
                 $this->writer->write(' as ' . $alias);
             }
             $this->writer->write(";\n");
         }
         $this->writer->write("\n");
     }
     if ($docblock = $class->getDocblock()) {
         $this->writer->write($docblock);
     }
     if ($class->isAbstract()) {
         $this->writer->write('abstract ');
     }
     if ($class->isFinal()) {
         $this->writer->write('final ');
     }
     // TODO: Interfaces should be modeled as separate classes.
     $this->isInterface = $class->getAttributeOrElse('interface', false);
     $this->writer->write($this->isInterface ? 'interface ' : 'class ');
     $this->writer->write($class->getShortName());
     if (!$this->isInterface) {
         if ($parentClassName = $class->getParentClassName()) {
             $this->writer->write(' extends ' . ('\\' === $parentClassName[0] ? $parentClassName : '\\' . $parentClassName));
         }
     }
     $interfaceNames = $class->getInterfaceNames();
     if (!empty($interfaceNames)) {
         $interfaceNames = array_unique($interfaceNames);
         $interfaceNames = array_map(function ($name) {
             if ('\\' === $name[0]) {
                 return $name;
             }
             return '\\' . $name;
         }, $interfaceNames);
         $this->writer->write($this->isInterface ? ' extends ' : ' implements ');
         $this->writer->write(implode(', ', $interfaceNames));
     }
     $this->writer->write("\n{\n")->indent();
 }
 public function startVisitingClass(PhpClass $class)
 {
     if ($namespace = $class->getNamespace()) {
         $this->writer->write('namespace ' . $namespace . ';' . "\n\n");
     }
     if ($files = $class->getRequiredFiles()) {
         foreach ($files as $file) {
             $this->writer->writeln('require_once ' . var_export($file, true) . ';');
         }
         $this->writer->write("\n");
     }
     if ($useStatements = $class->getUseStatements()) {
         foreach ($useStatements as $alias => $namespace) {
             $this->writer->write('use ' . $namespace);
             if (substr($namespace, strrpos($namespace, '\\') + 1) !== $alias) {
                 $this->writer->write(' as ' . $alias);
             }
             $this->writer->write(";\n");
         }
         $this->writer->write("\n");
     }
     if ($docblock = $class->getDocblock()) {
         $this->writer->write($docblock);
     }
     $this->writer->write('class ' . $class->getShortName());
     if ($parentClassName = $class->getParentClassName()) {
         $this->writer->write(' extends ' . ('\\' === $parentClassName[0] ? $parentClassName : '\\' . $parentClassName));
     }
     $interfaceNames = $class->getInterfaceNames();
     if (!empty($interfaceNames)) {
         $interfaceNames = array_unique($interfaceNames);
         $interfaceNames = array_map(function ($name) {
             if ('\\' === $name[0]) {
                 return $name;
             }
             return '\\' . $name;
         }, $interfaceNames);
         $this->writer->write(' implements ' . implode(', ', $interfaceNames));
     }
     $this->writer->write("\n{\n")->indent();
 }
Example #3
0
 /**
  * Cria a classe com base no Nó do XSD.
  * @param \DOMElement $node
  * @param string $namespace
  * @param string $dirTarget 
  */
 public function createClassFromNode(\DOMElement $node = null, $namespace = null, $dirTarget = null, $overwrite = false)
 {
     if ($node === null) {
         $node = $this->getLoadedSchema()->documentElement;
     }
     if ($namespace === NULL) {
         $namespace = $this->getDefaultNamespace();
     }
     if ($dirTarget === NULL) {
         $dirTarget = $this->getDirTarget();
     }
     $class = new \PhpClass();
     if (!is_null($namespace)) {
         $class->setNamespace(new \PhpClass_Namespace(array('path' => $namespace)));
     }
     $class->setExtends('\\Sped\\Components\\Xml\\Element');
     $class->setName($node->getAttribute('name'));
     $class->setDescription($this->getDocumentation($node));
     $class->addCommentTag(new \PhpClass_DocBlock_Tag(array('name' => 'category', 'description' => 'Sped')));
     $class->addCommentTag(new \PhpClass_DocBlock_Tag(array('name' => 'package', 'description' => 'Sped')));
     $class->addCommentTag(new \PhpClass_DocBlock_Tag(array('name' => 'copyright', 'description' => 'Copyright (c) 2012 Antonio Spinelli')));
     $class->addCommentTag(new \PhpClass_DocBlock_Tag(array('name' => 'license', 'description' => 'http://www.gnu.org/licenses/gpl.html GNU/GPL v.3')));
     $nodeName = $node->getAttribute('name');
     //verifica se o elemento está apenas no segundo nível do documento
     if ($node->localName == 'element' and $this->getNodeLevel($node) === 2) {
         $class->getConstants()->clear();
         $class->getMethods()->clear();
         $class->setExtends('\\Sped\\Components\\Xml\\Document');
         $class->setDescription('Documento ' . $class->getDocBlock()->getDescription());
         $class->setName('Document' . ucfirst($nodeName));
         if ($node->hasAttribute('type')) {
             $prefix = $node->prefix;
             if ($prefix != 'xs') {
                 $namespaceURI = $this->getLoadedSchema()->lookupNamespaceUri($prefix);
             } else {
                 $namespaceURI = $this->getLoadedSchema()->getTargetNamespace();
             }
             $methodName = $this->getSufixName($nodeName);
             $methodNamespace = $class->getNamespace()->getPath() . '\\' . $this->getSufixName($node->getAttribute('type'));
         } elseif ($node->hasAttribute('ref')) {
             $prefix = $this->getPrefixName($node->getAttribute('ref'));
             $namespaceURI = $this->getLoadedSchema()->lookupNamespaceUri($prefix);
             $methodName = $this->getSufixName($node->getAttribute('ref'));
             $methodNamespace = $class->getNamespace()->getPath() . '\\' . $methodName;
         }
         $class->addConstant(new \PhpClass_Constant(array('name' => $nodeName, 'value' => $nodeName)));
         $class->addMethod($this->createElementGetMethod(array('name' => $methodName, 'description' => $this->getDocumentation($node), 'type' => $methodNamespace, 'hasIndex' => false, 'isElement' => false)));
         $class->addMethod($this->createElementAddMethod(array('name' => $methodName, 'description' => $this->getDocumentation($node), 'type' => $methodNamespace, 'namespaceURI' => $namespaceURI)));
         $class->addMethod($this->createElementSetMethod(array('name' => $methodName, 'description' => $this->getDocumentation($node), 'type' => $methodNamespace)));
     }
     if (!preg_match('/^Document/', $class->getName())) {
         $this->createClassMethodsFromNode($class, $node);
     }
     $class->save($dirTarget, null, $this->isOverwrite());
     var_dump($class->getFullName() . ' is generated.');
 }