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();
 }