public function visitFunction(PhpFunction $function)
 {
     if ($namespace = $function->getNamespace()) {
         $this->writer->write("namespace {$namespace};\n\n");
     }
     $this->writer->write("function {$function->getName()}(");
     $this->writeParameters($function->getParameters());
     $this->writer->write(")\n{\n")->indent()->writeln($function->getBody())->outdent()->rtrim()->write('}');
 }
Exemple #2
0
   /**
    * Generates the class if not already generated
    */
   public function generateClass()
   {
       $config = Generator::getInstance()->getConfig();
       // Add prefix and suffix
       $name = $config->getPrefix() . $this->identifier . $config->getSuffix();
       // Generate a valid classname
       try {
           $name = Validator::validateClass($name);
       } catch (ValidationException $e) {
           $name .= 'Custom';
       }
       // Create the class object
       $comment = new PhpDocComment($this->description);
       $this->class = new PhpClass($name, $config->getClassExists(), 'SoapClient', $comment);
       // Create the constructor
       $comment = new PhpDocComment();
       $comment->addParam(PhpDocElementFactory::getParam('array', 'config', 'A array of config values'));
       $comment->addParam(PhpDocElementFactory::getParam('string', 'wsdl', 'The wsdl file to use'));
       $comment->setAccess(PhpDocElementFactory::getPublicAccess());
       $source = '  foreach(self::$classmap as $key => $value)
 {
   if(!isset($options[\'classmap\'][$key]))
   {
     $options[\'classmap\'][$key] = $value;
   }
 }
 ' . $this->generateServiceOptions($config) . '
 parent::__construct($wsdl, $options);' . PHP_EOL;
       $function = new PhpFunction('public', '__construct', 'array $options = array(), $wsdl = \'' . $config->getInputFile() . '\'', $source, $comment);
       // Add the constructor
       $this->class->addFunction($function);
       // Generate the classmap
       $name = 'classmap';
       $comment = new PhpDocComment();
       $comment->setAccess(PhpDocElementFactory::getPrivateAccess());
       $comment->setVar(PhpDocElementFactory::getVar('array', $name, 'The defined classes'));
       $init = 'array(' . PHP_EOL;
       foreach ($this->types as $type) {
           if ($type instanceof ComplexType) {
               $init .= "  '" . $type->getIdentifier() . "' => '" . $type->getPhpIdentifier() . "'," . PHP_EOL;
           }
       }
       $init = substr($init, 0, strrpos($init, ','));
       $init .= ')';
       $var = new PhpVariable('private static', $name, $init, $comment);
       // Add the classmap variable
       $this->class->addVariable($var);
       // Add all methods
       foreach ($this->operations as $operation) {
           $name = Validator::validateNamingConvention($operation->getName());
           $comment = new PhpDocComment($operation->getDescription());
           $comment->setAccess(PhpDocElementFactory::getPublicAccess());
           foreach ($operation->getParams() as $param => $hint) {
               $arr = $operation->getPhpDocParams($param, $this->types);
               $comment->addParam(PhpDocElementFactory::getParam($arr['type'], $arr['name'], $arr['desc']));
           }
           $source = '  return $this->__soapCall(\'' . $name . '\', array(' . $operation->getParamStringNoTypeHints() . '));' . PHP_EOL;
           $paramStr = $operation->getParamString($this->types);
           $function = new PhpFunction('public', $name, $paramStr, $source, $comment);
           if ($this->class->functionExists($function->getIdentifier()) == false) {
               $this->class->addFunction($function);
           }
       }
   }
 /**
  * Return class properties from array with indent specified
  *
  * @param array $props  Properties array
  * @param array $indent Indentation in tabs
  *
  * @return string
  */
 public function getClassProperties($props, $indent = "\t")
 {
     $code = $indent . "\n";
     foreach ($props as $prop) {
         if (!empty($prop['docs'])) {
             $code .= $indent . $this->getDocBlock($prop['docs'], $indent);
         }
         $code .= $indent . 'public $' . $prop['name'] . ";\n";
         // Add getter and setters
         if (!isset($prop['docs']['var'])) {
             $typeHint = null;
         } else {
             $typeHint = self::validateTypeHint($prop['docs']['var']);
         }
         $newMember = $this->getVariable($typeHint, $prop['name'], true);
         $setterCommentObj = new PhpDocComment();
         $setterCommentObj->addParam(PhpDocElementFactory::getParam($typeHint, $prop['name'], ''));
         $setterCommentObj->setReturn(PhpDocElementFactory::getReturn('$this', ''));
         $setterCode = $this->getSetterBody($prop['name'], $typeHint, $newMember);
         $setter = new PhpFunction('public', 'set' . ucfirst($prop['name']), $this->buildParametersString([$prop['name'] => $typeHint], true, $newMember->getNullable() && !empty($typeHint)), $setterCode, $setterCommentObj);
         $getterComment = new PhpDocComment();
         $getterComment->setReturn(PhpDocElementFactory::getReturn($typeHint, ''));
         $getterCode = $this->getGetterBody($prop['name'], $typeHint);
         $getter = new PhpFunction('public', 'get' . $prop['name'], '', $getterCode, $getterComment);
         $code = $code . $getter->getSource() . $setter->getSource() . PHP_EOL;
     }
     return $code;
 }
Exemple #4
0
 /**
  * @see \WsdlToPhp\PhpGenerator\Element\AbstractElement::getChildren()
  * @return array
  */
 public function getChildren()
 {
     if ($this->getHasBody() === true) {
         return parent::getChildren();
     }
     return array();
 }
 /**
  * Adds a global function to the file, should not be used, classes rocks :)
  *
  * @param PhpFunction $function The function to add
  * @throws Exception If the function already exists
  */
 public function addFunction(PhpFunction $function)
 {
     if ($this->functionExists($function->getIdentifier())) {
         throw new Exception('A function of the name (' . $function->getIdentifier() . ') does already exist.');
     }
     $this->functions[$function->getIdentifier()] = $function;
 }
 public function visitFunction(PhpFunction $function)
 {
     if ($namespace = $function->getNamespace()) {
         $this->writer->write("namespace {$namespace};\n\n");
     }
     if ($docblock = $function->getDocblock()) {
         $this->writer->writeln($docblock)->rtrim();
     }
     $this->writer->write("function {$function->getName()}(");
     $this->writeParameters($function->getParameters());
     $this->writer->write(')');
     if ($function->hasReturnType()) {
         $type = $function->getReturnType();
         $this->writer->write(': ');
         if (!$function->hasBuiltinReturnType() && '\\' !== $type[0]) {
             $this->writer->write('\\');
         }
         $this->writer->write($type);
     }
     $this->writer->write("\n{\n")->indent()->writeln($function->getBody())->outdent()->rtrim()->write('}');
 }