Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed calls to sfInflector

Stopped class generation for ComplexTypes with no parameters
Added comments for array property setters
Renamed Enum and Pattern to EnumType and PatternType to be inline with
ComplexType
  • Loading branch information...
commit 360d33eb8cde5b4267e361820801db1b98427ffb 1 parent 6779c9b
@brunnels authored
View
1  TODO
@@ -1 +0,0 @@
-Figure out how to camelize the variable names during runtime and remove the sfInflector calls
View
108 src/ComplexType.php
@@ -27,32 +27,19 @@ class ComplexType extends Type
*
* @var array The members in the type
*/
- private $members;
+ private $members = array();
/**
*
* @var array The primitive types
*/
- private $primitive;
-
- /**
- * Construct the object
- *
- * @param string $name The identifier for the class
- * @param string $restriction The restriction(datatype) of the values
- */
- function __construct($name)
- {
- parent::__construct($name, null);
- $this->members = array();
- $this->primitives = array(
- 'int',
- 'float',
- 'string',
- 'bool',
- 'mixed'
- );
- }
+ private $primitives = array(
+ 'int',
+ 'float',
+ 'string',
+ 'bool',
+ 'mixed'
+ );
private function generateParameterConstructor($class)
{
@@ -75,10 +62,10 @@ private function generateParameterConstructor($class)
$type .= 'Custom';
}
- $name = Validator::validateNamingConvention($member->getName());
- $constructorSource .= " call_user_func(array(\$this, 'set' . ucfirst(sfInflector::camelize('$name'))), $$name);".PHP_EOL;
+ $name = str_replace('__', '', Validator::validateNamingConvention($member->getName()));
+ $constructorSource .= " call_user_func(array(\$this, 'set" . ucfirst($this->camelize($name)) . "'), $$name);".PHP_EOL;
$constructorComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
- $constructorParameters .= ((!in_array($type, $this->primitives)) ? ", $type " : ', ') . "$$name";
+ $constructorParameters .= ((!in_array($type, $this->primitives)) ? ", $type " : ', ') . "$$name = NULL";
$i++;
}
@@ -89,23 +76,62 @@ private function generateParameterConstructor($class)
return $class;
}
- private function generateArrayConstructor($class)
+ private function generateArrayParameterConstructor($class)
{
$constructorComment = new PhpDocComment();
- $constructorSource = ' foreach($properties as $key => $value)'.PHP_EOL;
- $constructorSource .= ' {'.PHP_EOL;
- $constructorSource .= ' call_user_func(array($this, \'set\' . ucfirst(sfInflector::camelize($key)), $value));'.PHP_EOL;
- $constructorSource .= ' }'.PHP_EOL;
+ $constructorSource = '';
+ $constructorParameters = '';
+ $propertiesComment = "Properties array\n *\n * Form the \$properties array like this:\n * <code>\n * \$properties = arrray(\n";
+
+ foreach ($this->members as $member)
+ {
+ $type = '';
+
+ try
+ {
+ $type = Validator::validateType($member->getType());
+ }
+ catch (ValidationException $e)
+ {
+ $type .= 'Custom';
+ }
+
+ $name = str_replace('__', '', Validator::validateNamingConvention($member->getName()));
+
+ $propertiesComment .= " * '$name' => 'value' // $type\n";
+
+ $constructorSource .= " call_user_func(array(\$this, 'set" . ucfirst($this->camelize($name)) . "'), \$properties['$name']);".PHP_EOL;
+ }
+
+ $propertiesComment .= " * );\n *\n * </code>\n *";
+
+ $constructorComment->addParam(PhpDocElementFactory::getParam('array[mixed]', 'properties', str_replace("\n", PHP_EOL, $propertiesComment)));
- $constructorParameters = 'array $properties = array()';
+ $constructorParameters = substr($constructorParameters, 2); // Remove first comma
$constructorFunction = new PhpFunction('public', '__construct', $constructorParameters, $constructorSource, $constructorComment);
$class->addFunction($constructorFunction);
-
return $class;
}
+// private function generateArrayConstructor($class)
+// {
+// $constructorComment = new PhpDocComment();
+
+// $constructorSource = ' foreach($properties as $key => $value)'.PHP_EOL;
+// $constructorSource .= ' {'.PHP_EOL;
+// $constructorSource .= ' call_user_func(array($this, \'set\' . str_replace("__", "_", ucfirst(sfInflector::camelize($key)))), $value);'.PHP_EOL;
+// $constructorSource .= ' }'.PHP_EOL;
+
+// $constructorParameters = 'array $properties = array()';
+
+// $constructorFunction = new PhpFunction('public', '__construct', $constructorParameters, $constructorSource, $constructorComment);
+// $class->addFunction($constructorFunction);
+
+// return $class;
+// }
+
/**
* Implements the loading of the class object using setters and getters
* @throws Exception if the class is already generated(not null)
@@ -126,7 +152,7 @@ protected function generateClass()
// Only add the constructor if type constructor is selected and not a response class
if ($config->getNoTypeConstructor() == false && $isResponseClass == false)
{
- $class = (count($this->members) > 5) ? $this->generateArrayConstructor($class) : $this->generateParameterConstructor($class);
+ $class = (count($this->members) > 5) ? $this->generateArrayParameterConstructor($class) : $this->generateParameterConstructor($class);
}
// Add member variables
@@ -143,7 +169,8 @@ protected function generateClass()
$type .= 'Custom';
}
- $name = Validator::validateNamingConvention($member->getName());
+ $varName = Validator::validateNamingConvention($member->getName());
+ $name = str_replace('__', '', $varName);
// if a variable is all uppercase make only the first character upper for the method name
$methodName = (strtoupper($name) == $name) ? ucfirst(strtolower($name)) : $name;
@@ -152,8 +179,8 @@ protected function generateClass()
$methodName = (strpos($methodName, '_') !== false) ? $this->camelize($methodName) : $methodName;
$classComment = new PhpDocComment();
- $classComment->setVar(PhpDocElementFactory::getVar($type, $name, ''));
- $classVar = new PhpVariable('private', $name, '', $classComment);
+ $classComment->setVar(PhpDocElementFactory::getVar($type, $varName, ''));
+ $classVar = new PhpVariable('private', $varName, '', $classComment);
$class->addVariable($classVar);
// dont add setters for response and result classes
@@ -161,7 +188,7 @@ protected function generateClass()
if (!$isResponseClass)
{
$setterParameters = ((!in_array($type, $this->primitives)) ? "$type " : '') . "$$name";
- $setterSource = " \$this->$name = $$name;".PHP_EOL;
+ $setterSource = " \$this->$varName = $$name;".PHP_EOL;
$setterComment = new PhpDocComment();
$setterComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
$setterFunction = new PhpFunction('public', 'set' . ucfirst($methodName), $setterParameters, $setterSource, $setterComment);
@@ -189,6 +216,15 @@ public function addMember($type, $name)
$this->members[$name] = new Variable($type, $name);
}
+ /**
+ *
+ * @return int
+ */
+ public function getMemberCount()
+ {
+ return count($this->members);
+ }
+
private function camelize($lower_case_and_underscored_word)
{
$tmp = $lower_case_and_underscored_word;
View
4 src/Enum.php → src/EnumType.php
@@ -10,13 +10,13 @@
require_once dirname(__FILE__).'/Type.php';
/**
- * Enum represents a simple type with enumerated values
+ * EnumType represents a simple type with enumerated values
*
* @package Wsdl2PhpGenerator
* @author Fredrik Wallgren <fredrik.wallgren@gmail.com>
@license http://www.opensource.org/licenses/mit-license.php MIT License
*/
-class Enum extends Type
+class EnumType extends Type
{
/**
*
View
47 src/Generator.php
@@ -9,9 +9,9 @@
require_once dirname(__FILE__).'/Config.php';
require_once dirname(__FILE__).'/Validator.php';
require_once dirname(__FILE__).'/Variable.php';
-require_once dirname(__FILE__).'/Enum.php';
+require_once dirname(__FILE__).'/EnumType.php';
require_once dirname(__FILE__).'/ComplexType.php';
-require_once dirname(__FILE__).'/Pattern.php';
+require_once dirname(__FILE__).'/PatternType.php';
require_once dirname(__FILE__).'/DocumentationManager.php';
require_once dirname(__FILE__).'/Service.php';
require_once dirname(__FILE__).'/OutputManager.php';
@@ -24,7 +24,7 @@
*
* @package Wsdl2PhpGenerator
* @author Fredrik Wallgren <fredrik.wallgren@gmail.com>
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ *@licensehttp://www.opensource.org/licenses/mit-license.php MITLicense
*/
class Generator
{
@@ -81,7 +81,7 @@ class Generator
* @var displayCallback The function called to display output internally. Initially set to gettext if set
*/
private $displayCallback;
-
+
/**
* Construct the generator
*/
@@ -99,21 +99,21 @@ private function __construct()
/**
* Initializes the single instance if it hasn't been, and returns it if it has.
*/
- public static function instance()
+ public static function instance()
{
- if( self::$instance === null )
+ if( self::$instance === null )
{
self::$instance = new Generator();
}
return self::$instance;
}
-
+
/**
* Sets the display callback to an anonymous function, or a string referring to a built-in callable
*
* @param callable $callback
*/
- public function setDisplayCallback( $callback )
+ public function setDisplayCallback( $callback )
{
$this->displayCallback = $callback;
}
@@ -123,12 +123,12 @@ public function setDisplayCallback( $callback )
*
* @param string $string
*/
- private function display( $string )
+ private function display( $string )
{
$disp = $this->displayCallback;
return $disp( $string );
}
-
+
/**
* Generates php source code from a wsdl file
*
@@ -238,7 +238,7 @@ private function loadTypes()
$this->log($this->display('Loading types'));
$types = $this->client->__getTypes();
-
+
foreach($types as $typeStr)
{
$wsdlNewline = ( strpos( $typeStr, "\r\n" ) ? "\r\n" : "\n" );
@@ -270,7 +270,7 @@ private function loadTypes()
$type->addMember($typename, $name);
}
}
- else // Enum or Pattern
+ else // EnumType or PatternType
{
$typenode = $this->findTypenode($className);
@@ -281,7 +281,7 @@ private function loadTypes()
$patternList = $typenode->getElementsByTagName('pattern');
if ($enumerationList->length > 0)
{
- $type = new Enum($className, $restriction);
+ $type = new EnumType($className, $restriction);
$this->log($this->display('Loading enum ').$type->getPhpIdentifier());
foreach ($enumerationList as $enum)
{
@@ -290,7 +290,7 @@ private function loadTypes()
}
else if ($patternList->length > 0)// If pattern
{
- $type = new Pattern($className, $restriction);
+ $type = new PatternType($className, $restriction);
$this->log($this->display('Loading pattern ').$type->getPhpIdentifier());
$type->setValue($patternList->item(0)->attributes->getNamedItem('value')->nodeValue);
}
@@ -305,9 +305,9 @@ private function loadTypes()
{
$already_registered = FALSE;
if ($this->config->getSharedTypes())
- foreach ($this->types as $registered_types)
+ foreach ($this->types as $registered_types)
{
- if ($registered_types->getIdentifier() == $type->getIdentifier())
+ if ($registered_types->getIdentifier() == $type->getIdentifier())
{
$already_registered = TRUE;
break;
@@ -342,14 +342,17 @@ private function savePhp()
$types = array();
foreach ($this->types as $type)
{
- $class = $type->getClass();
- if ($class != null)
+ if($type instanceof ComplexType && $type->getMemberCount())
{
- $types[] = $class;
-
- if ($this->config->getOneFile() == false && $this->config->getSkipAddDependencies() == false)
+ $class = $type->getClass();
+ if ($class != null)
{
- $service->addDependency($class->getIdentifier() . $this->config->getClassFileSuffix() . '.php');
+ $types[] = $class;
+
+ if ($this->config->getOneFile() == false && $this->config->getSkipAddDependencies() == false)
+ {
+ $service->addDependency($class->getIdentifier() . $this->config->getClassFileSuffix() . '.php');
+ }
}
}
}
View
16 src/Operation.php
@@ -112,13 +112,18 @@ public function getParamString(array $validTypes)
*
* @param string $name The param to get
* @param array An array of Type objects with valid types for typehinting
- * @return array A array with three keys 'type' => the typehint to use 'name' => the name of the param and 'desc' => A description of the param
+ * @return array A array with four keys
+ * 'type' => the typehint to use
+ * 'name' => the name of the param
+ * 'desc' => A description of the param
+ * 'params' => number of parameters the type has (for ComplexType)
*/
public function getPhpDocParams($name, array $validTypes)
{
$ret = array();
$ret['desc'] = '';
+ $ret['params'] = NULL;
$paramType = '';
foreach ($this->params as $value => $typeHint)
@@ -135,7 +140,7 @@ public function getPhpDocParams($name, array $validTypes)
{
if ($paramType == $type->getIdentifier())
{
- if ($type instanceof Pattern)
+ if ($type instanceof PatternType)
{
$ret['type'] = $type->getDatatype();
$ret['desc'] = _('Restriction pattern: ').$type->getValue();
@@ -144,7 +149,12 @@ public function getPhpDocParams($name, array $validTypes)
{
$ret['type'] = $type->getPhpIdentifier();
- if ($type instanceof Enum)
+ if ($type instanceof ComplexType)
+ {
+ $ret['params'] = $type->getMemberCount();
+ }
+
+ if ($type instanceof EnumType)
{
$ret['desc'] = _('Constant: ').$type->getDatatype() .' - '. _('Valid values: ').$type->getValidValues();
}
View
4 src/Pattern.php → src/PatternType.php
@@ -9,13 +9,13 @@
require_once dirname(__FILE__).'/Type.php';
/**
- * Pattern represents a simple type with restriction and a pattern
+ * PatternType represents a simple type with restriction and a pattern
*
* @package Wsdl2PhpGenerator
* @author Fredrik Wallgren <fredrik.wallgren@gmail.com>
@license http://www.opensource.org/licenses/mit-license.php MIT License
*/
-class Pattern extends Type
+class PatternType extends Type
{
/**
*
View
25 src/Service.php
@@ -42,7 +42,7 @@ class Service
/**
*
- * @var array An array containing the operations of the service
+ * @var array[Operation] An array containing the operations of the service
*/
private $operations;
@@ -143,7 +143,10 @@ public function generateClass()
{
if($type instanceof ComplexType)
{
- $init .= " '".$type->getIdentifier()."' => '".$type->getPhpIdentifier()."',".PHP_EOL;
+ if($type->getMemberCount())
+ {
+ $init .= " '".$type->getIdentifier()."' => '".$type->getPhpIdentifier()."',".PHP_EOL;
+ }
}
}
$init = substr($init, 0, strrpos($init, ','));
@@ -163,13 +166,23 @@ public function generateClass()
foreach ($operation->getParams() as $param => $hint)
{
$arr = $operation->getPhpDocParams($param, $this->types);
- $comment->addParam(PhpDocElementFactory::getParam($arr['type'], '$' . $arr['type'], $arr['desc']));
+ if($arr['params'] !== 0)
+ {
+ $comment->addParam(PhpDocElementFactory::getParam($arr['type'], '$' . $arr['type'], $arr['desc']));
+ }
$comment->setReturn(PhpDocElementFactory::getReturn($arr['type'] . 'Response', ''));
}
- $source = ' return $this->__soapCall(\''.$name.'\', array($' . $arr['type'] . '));' . PHP_EOL;
-
- $paramStr = $operation->getParamString($this->types);
+ if($arr['params'] === 0)
+ {
+ $source = ' return $this->__soapCall(\''.$name.'\', array());' . PHP_EOL;
+ $paramStr = '';
+ }
+ else
+ {
+ $source = ' return $this->__soapCall(\''.$name.'\', array($' . $arr['type'] . '));' . PHP_EOL;
+ $paramStr = $operation->getParamString($this->types);
+ }
$function = new PhpFunction('public', $name, $paramStr, $source, $comment);
Please sign in to comment.
Something went wrong with that request. Please try again.