Browse files

Updated service generator to have proper type hinting and more friendly

variable names.
Changed result and response classes to not have constructors or setters.
Changed setters and getters to camelized versions of the variable name
to get rid of underscores in method names.
Added sfInflector::camelize dependancy in constructors for runtime
camelization.  This needs to be removed and a different runtime
camelization method added but works for my current requirements.
  • Loading branch information...
1 parent a45d3d2 commit 6779c9bbde5eb24a93ca4d1b2feef7b5d15576a0 @brunnels committed Feb 27, 2013
Showing with 51 additions and 34 deletions.
  1. +1 −0 TODO
  2. +38 −22 src/ComplexType.php
  3. +4 −3 src/Operation.php
  4. +4 −6 src/Service.php
  5. +4 −3 src/Validator.php
View
1 TODO
@@ -0,0 +1 @@
+Figure out how to camelize the variable names during runtime and remove the sfInflector calls
View
60 src/ComplexType.php
@@ -57,11 +57,11 @@ function __construct($name)
private function generateParameterConstructor($class)
{
$constructorComment = new PhpDocComment();
- $constructorComment->setAccess(PhpDocElementFactory::getPublicAccess());
- $constructorSource = '';
+ $constructorSource = '';
$constructorParameters = '';
+ $i = 0;
foreach ($this->members as $member)
{
$type = '';
@@ -76,11 +76,10 @@ private function generateParameterConstructor($class)
}
$name = Validator::validateNamingConvention($member->getName());
-
- $constructorSource .= ' $this->set' . ucfirst($name) . "($$name);" . PHP_EOL;
+ $constructorSource .= " call_user_func(array(\$this, 'set' . ucfirst(sfInflector::camelize('$name'))), $$name);".PHP_EOL;
$constructorComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
- $constructorComment->setAccess(PhpDocElementFactory::getPublicAccess());
$constructorParameters .= ((!in_array($type, $this->primitives)) ? ", $type " : ', ') . "$$name";
+ $i++;
}
$constructorParameters = substr($constructorParameters, 2); // Remove first comma
@@ -93,15 +92,13 @@ private function generateParameterConstructor($class)
private function generateArrayConstructor($class)
{
$constructorComment = new PhpDocComment();
- $constructorComment->setAccess(PhpDocElementFactory::getPublicAccess());
$constructorSource = ' foreach($properties as $key => $value)'.PHP_EOL;
$constructorSource .= ' {'.PHP_EOL;
- $constructorSource .= ' $setter = \'set\' . ucfirst($key);'.PHP_EOL;
- $constructorSource .= ' $this->$setter($value);'.PHP_EOL;
+ $constructorSource .= ' call_user_func(array($this, \'set\' . ucfirst(sfInflector::camelize($key)), $value));'.PHP_EOL;
$constructorSource .= ' }'.PHP_EOL;
- $constructorParameters = 'Array $properties = array()';
+ $constructorParameters = 'array $properties = array()';
$constructorFunction = new PhpFunction('public', '__construct', $constructorParameters, $constructorSource, $constructorComment);
$class->addFunction($constructorFunction);
@@ -124,10 +121,10 @@ protected function generateClass()
$class = new PhpClass($this->phpIdentifier, $config->getClassExists());
+ $isResponseClass = (strpos($this->phpIdentifier, 'Response') !== false || strpos($this->phpIdentifier, 'Result') !== false) ? true : false;
-
- // Only add the constructor if type constructor is selected
- if ($config->getNoTypeConstructor() == false)
+ // 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);
}
@@ -148,25 +145,33 @@ protected function generateClass()
$name = Validator::validateNamingConvention($member->getName());
+ // if a variable is all uppercase make only the first character upper for the method name
+ $methodName = (strtoupper($name) == $name) ? ucfirst(strtolower($name)) : $name;
+
+ // take variables and camelize them if needed
+ $methodName = (strpos($methodName, '_') !== false) ? $this->camelize($methodName) : $methodName;
+
$classComment = new PhpDocComment();
$classComment->setVar(PhpDocElementFactory::getVar($type, $name, ''));
- $classComment->setAccess(PhpDocElementFactory::getPrivateAccess());
$classVar = new PhpVariable('private', $name, '', $classComment);
$class->addVariable($classVar);
- $setterParameters = ((!in_array($type, $this->primitives)) ? "$type " : '') . "$$name";
- $setterSource = " \$this->$name = $$name;".PHP_EOL;
- $setterComment = new PhpDocComment();
- $setterComment->setAccess(PhpDocElementFactory::getPublicAccess());
- $setterComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
- $setterFunction = new PhpFunction('public', 'set' . ucfirst($name), $setterParameters, $setterSource, $setterComment);
- $class->addFunction($setterFunction);
+ // dont add setters for response and result classes
+
+ if (!$isResponseClass)
+ {
+ $setterParameters = ((!in_array($type, $this->primitives)) ? "$type " : '') . "$$name";
+ $setterSource = " \$this->$name = $$name;".PHP_EOL;
+ $setterComment = new PhpDocComment();
+ $setterComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
+ $setterFunction = new PhpFunction('public', 'set' . ucfirst($methodName), $setterParameters, $setterSource, $setterComment);
+ $class->addFunction($setterFunction);
+ }
$getterSource = " return \$this->$name;".PHP_EOL;
$getterComment = new PhpDocComment();
- $getterComment->setAccess(PhpDocElementFactory::getPublicAccess());
$getterComment->setReturn(PhpDocElementFactory::getReturn($type, ''));
- $getterFunction = new PhpFunction('public', 'get' . ucfirst($name), '', $getterSource, $getterComment);
+ $getterFunction = new PhpFunction('public', 'get' . ucfirst($methodName), '', $getterSource, $getterComment);
$class->addFunction($getterFunction);
}
@@ -183,5 +188,16 @@ public function addMember($type, $name)
{
$this->members[$name] = new Variable($type, $name);
}
+
+ private function camelize($lower_case_and_underscored_word)
+ {
+ $tmp = $lower_case_and_underscored_word;
+
+ $tmp = preg_replace(array_keys(array('#/(.?)#e' => "'::'.strtoupper('\\1')",
+ '/(^|_|-)+(.)/e' => "strtoupper('\\2')")), array_values(array('#/(.?)#e' => "'::'.strtoupper('\\1')",
+ '/(^|_|-)+(.)/e' => "strtoupper('\\2')")), $tmp);
+
+ return $tmp;
+ }
}
View
7 src/Operation.php
@@ -91,6 +91,7 @@ public function getParamString(array $validTypes)
if ($typeHint == $type->getIdentifier())
{
$ret .= $type->getPhpIdentifier().' ';
+ $value = '$' . $type->getPhpIdentifier();
}
}
}
@@ -184,7 +185,7 @@ private function generateParams($paramStr)
foreach (explode(', ', $paramStr) as $param)
{
- $arr = explode(' ', $param);
+ $arr = explode(' ', $param);
// Check if we have type hint. 1 = no type hint
if (count($arr) == 1)
@@ -197,8 +198,8 @@ private function generateParams($paramStr)
else
{
$this->params[$arr[1]] = $arr[0];
- }
- }
+ }
+ }
}
}
View
10 src/Service.php
@@ -91,7 +91,7 @@ public function getClass()
public function generateClass()
{
$config = Generator::getInstance()->getConfig();
-
+
if($config->getServiceClassName())
{
$name = $config->getServiceClassName();
@@ -117,7 +117,6 @@ public function generateClass()
$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)
{
@@ -137,7 +136,6 @@ public function generateClass()
// 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;
@@ -161,15 +159,15 @@ public function generateClass()
$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']));
+ $comment->addParam(PhpDocElementFactory::getParam($arr['type'], '$' . $arr['type'], $arr['desc']));
+ $comment->setReturn(PhpDocElementFactory::getReturn($arr['type'] . 'Response', ''));
}
- $source = ' return $this->__soapCall(\''.$name.'\', array('.$operation->getParamStringNoTypeHints().'));'.PHP_EOL;
+ $source = ' return $this->__soapCall(\''.$name.'\', array($' . $arr['type'] . '));' . PHP_EOL;
$paramStr = $operation->getParamString($this->types);
View
7 src/Validator.php
@@ -220,13 +220,14 @@ private static function validateTypeName($type)
break;
case "anytype":
- return 'Array';
+ case "array":
+ return 'array';
break;
-
+
case "enum":
return 'Enum';
break;
-
+
case "datetime":
return 'DateTime';
break;

0 comments on commit 6779c9b

Please sign in to comment.