Skip to content

Commit

Permalink
Added new features for class filename suffix, service name, and skip
Browse files Browse the repository at this point in the history
dependency inclusions.  Fixed issues with suffix/prefix not reflected in
phpdoc type hinting.  Added handling and phpdoc for anyType.
  • Loading branch information
brunnels committed Oct 30, 2012
1 parent 4d17b83 commit ad5d37d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 30 deletions.
15 changes: 12 additions & 3 deletions generate.php
Expand Up @@ -61,13 +61,16 @@ function gettext($str)
$cli->addFlag('-e', _('If all classes should be guarded with if(!class_exists) statements'), true, false);
$cli->addFlag('-t', _('If no type constructor should be generated'), true, false);
$cli->addFlag('-s', _('If the output should be a single file'), true, false);
$cli->addFlag('-d', _('Skips addiing dependency includes'), true, false);
$cli->addFlag('-g', _('The service class name'), false, false);
$cli->addFlag('-v', _('If the output to the console should be verbose'), true, false);
$cli->addFlag('-i', _('The input wsdl file'), false, true);
$cli->addFlag('-o', _('The output directory or file if -s is used (in that case, .php will be appened to file name)'), false, true);
$cli->addFlag('-n', _('Use namespace with the name'), false, false);
$cli->addFlag('-c', _('A comma separated list of classnames to generate. If this is used only classes that exist in the list will be generated. If the service is not in this list and the -s flag is used the filename will be the name of the first class that is generated'), false, false);
$cli->addFlag('-p', _('The prefix to use for the generated classes'), false, false);
$cli->addFlag('-q', _('The suffix to use for the generated classes'), false, false);
$cli->addFlag('-f', _('The filename suffix to use for the generated classes'), false, false);
$cli->addFlag('--singleElementArrays', _('Adds the option to use single element arrays to the client'), true, false);
$cli->addFlag('--xsiArrayType', _('Adds the option to use xsi arrays to the client'), true, false);
$cli->addFlag('--waitOneWayCalls', _('Adds the option to use wait one way calls to the client'), true, false);
Expand All @@ -82,6 +85,8 @@ function gettext($str)
$cli->addAlias('-e', '--classExists');
$cli->addAlias('-e', '--exists');
$cli->addAlias('-t', '--noTypeConstructor');
$cli->addAlias('-d', '--skipAddDependencies');
$cli->addAlias('-g', '--serviceClassName');
$cli->addAlias('-s', '--singleFile');
$cli->addAlias('-v', '--verbose');
$cli->addAlias('-i', '--input');
Expand All @@ -92,15 +97,16 @@ function gettext($str)
$cli->addAlias('-c', '--classList');
$cli->addAlias('-p', '--prefix');
$cli->addAlias('-q', '--suffix');
$cli->addAlias('-f', '--filenameSuffix');
$cli->addAlias('-h', '--help');
$cli->addAlias('-h', '--h');

$cli->validate($argv);

$singleFile = $cli->getValue('-s');
$oneFile = $cli->getValue('-s');
$classNames = trim($cli->getValue('-c'));

if ($singleFile && strlen($classNames) > 0)
if ($oneFile && strlen($classNames) > 0)
{
// Print different messages based on if more than one class is requested for generation
if (strpos($classNames, ',') !== false)
Expand Down Expand Up @@ -133,6 +139,9 @@ function gettext($str)
}

$classExists = $cli->getValue('-e');
$classFileSuffix = $cli->getValue('-f');
$serviceClassName = $cli->getValue('-g');
$skipAddDependencies = $cli->getValue('-d');
$verbose = $cli->getValue('-v');
$noTypeConstructor = $cli->getValue('-t');
$inputFile = $cli->getValue('-i');
Expand Down Expand Up @@ -178,7 +187,7 @@ function gettext($str)
$gzip = 'SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP';
}

$config = new Config($inputFile, $outputDir, $verbose, $singleFile, $classExists, $noTypeConstructor, $namespaceName, $optionsArray, $wsdlCache, $gzip, $classNames, $prefix, $suffix);
$config = new Config($inputFile, $outputDir, $verbose, $oneFile, $skipAddDependencies, $classExists, $classFileSuffix, $serviceClassName, $noTypeConstructor, $namespaceName, $optionsArray, $wsdlCache, $gzip, $classNames, $prefix, $suffix);

$generator = Generator::instance();
$generator->generate($config);
Expand Down
3 changes: 2 additions & 1 deletion lib/phpSource/PhpFile.php
Expand Up @@ -99,7 +99,8 @@ public function getSource()
*/
public function save($directory)
{
file_put_contents($directory.DIRECTORY_SEPARATOR.$this->name.'.php', $this->getSource());
$config = Generator::getInstance()->getConfig();
file_put_contents($directory.DIRECTORY_SEPARATOR.$this->name . $config->getClassFileSuffix() . '.php', $this->getSource());
}

/**
Expand Down
55 changes: 54 additions & 1 deletion src/Config.php
Expand Up @@ -25,6 +25,13 @@ class Config
* @access private
*/
private $oneFile;

/**
*
* @var bool Skips adding dependency includes if true
* @access private
*/
private $skipAddDependencies;

/**
*
Expand Down Expand Up @@ -59,6 +66,19 @@ class Config
* @var string The wsdl cache to use if any. Possible values WSDL_CACHE_NONE, WSDL_CACHE_DISK, WSDL_CACHE_MEMORY or WSDL_CACHE_BOTH
*/
private $wsdlCache;

/**
*
* @var string The suffix for the class file name. ie "class" outputs ClassName.class.php
*/
private $classFileSuffix;

/**
* The name of the service class
*
* @var string
*/
private $serviceClassName;

/**
*
Expand Down Expand Up @@ -111,7 +131,10 @@ class Config
* @param string $outputDir
* @param bool $verbose
* @param bool $oneFile
* @param bool $skipAddDependencies
* @param bool $classExists
* @param string $classFileSuffix
* @param string $serviceClassName
* @param bool $noTypeConstructor
* @param string $namespaceName
* @param array $optionsFeatures
Expand All @@ -122,12 +145,15 @@ class Config
* @param string $suffix
* @param string $sharedTypes
*/
public function __construct($inputFile, $outputDir, $verbose = false, $oneFile = false, $classExists = false, $noTypeConstructor = false, $namespaceName = '', $optionsFeatures = array(), $wsdlCache = '', $compression = '', $classNames = '', $prefix = '', $suffix = '', $sharedTypes = false)
public function __construct($inputFile, $outputDir, $verbose = false, $oneFile = false, $skipAddDependencies = false, $classExists = false, $classFileSuffix = '', $serviceClassName = null, $noTypeConstructor = false, $namespaceName = '', $optionsFeatures = array(), $wsdlCache = '', $compression = '', $classNames = '', $prefix = '', $suffix = '', $sharedTypes = false)
{
$this->namespaceName = trim($namespaceName);
$this->oneFile = $oneFile;
$this->skipAddDependencies = $skipAddDependencies;
$this->verbose = $verbose;
$this->classExists = $classExists;
$this->classFileSuffix = $classFileSuffix;
$this->serviceClassName = $serviceClassName;
$this->noTypeConstructor = $noTypeConstructor;
$this->outputDir = trim($outputDir);
if (is_array($inputFile))
Expand Down Expand Up @@ -166,6 +192,15 @@ public function getOneFile()
{
return $this->oneFile;
}

/**
* @return bool Returns if the dependency includes should be added
* @access public
*/
public function getSkipAddDependencies()
{
return $this->skipAddDependencies;
}

/**
* @return bool Returns if the output should be protected with class_exists statements
Expand All @@ -175,6 +210,24 @@ public function getClassExists()
{
return $this->classExists;
}

/**
* @return string Returns the configured class file suffix
* @access public
*/
public function getClassFileSuffix()
{
return $this->classFileSuffix;
}

/**
* @return string Returns the configured service class name
* @access public
*/
public function getServiceClassName()
{
return $this->serviceClassName;
}

/**
*
Expand Down
12 changes: 6 additions & 6 deletions src/Generator.php
Expand Up @@ -141,8 +141,8 @@ public function generate(Config $config)
$wsdl = $this->config->getInputFile();
if (is_array($wsdl))
foreach($wsdl as $ws)
$this->load($ws);
else
$this->load($ws);
else
$this->load($wsdl);

$this->savePhp();
Expand Down Expand Up @@ -228,10 +228,10 @@ 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" );
$wsdlNewline = ( strpos( $typeStr, "\r\n" ) ? "\r\n" : "\n" );
$parts = explode($wsdlNewline, $typeStr);
$tArr = explode(" ", $parts[0]);
$restriction = $tArr[0];
Expand Down Expand Up @@ -336,9 +336,9 @@ private function savePhp()
{
$types[] = $class;

if ($this->config->getOneFile() == false)
if ($this->config->getOneFile() == false && $this->config->getSkipAddDependencies() == false)
{
$service->addDependency($class->getIdentifier().'.php');
$service->addDependency($class->getIdentifier() . $this->config->getClassFileSuffix() . '.php');
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/OutputManager.php
Expand Up @@ -85,9 +85,9 @@ public function save(PhpClass $service, array $types)
}

if (!is_dir($this->config->getOutputDir()))
$this->file->save(dirname($this->config->getOutputDir()));
$this->file->save(dirname($this->config->getOutputDir()));
else
$this->file->save($this->dir);
$this->file->save($this->dir);
}
else
{
Expand Down
22 changes: 13 additions & 9 deletions src/Service.php
Expand Up @@ -91,18 +91,22 @@ public function getClass()
public function generateClass()
{
$config = Generator::getInstance()->getConfig();

// Add prefix and suffix
$name = $config->getPrefix().$this->identifier.$config->getSuffix();

// Generate a valid classname
try

if($config->getServiceClassName())
{
$name = Validator::validateClass($name);
$name = $config->getServiceClassName();
}
catch (ValidationException $e)
else
{
$name .= 'Custom';
// Generate a valid classname
try
{
$name = Validator::validateClass($this->identifier);
}
catch (ValidationException $e)
{
$name .= 'Custom';
}
}

// Create the class object
Expand Down
5 changes: 0 additions & 5 deletions src/Type.php
Expand Up @@ -59,11 +59,6 @@ public function __construct($name, $datatype)
$this->datatype = $datatype;
$this->identifier = $name;

$config = Generator::getInstance()->getConfig();

// Add prefix and suffix
$name = $config->getPrefix().$this->identifier.$config->getSuffix();

try
{
$name = Validator::validateClass($name);
Expand Down
23 changes: 20 additions & 3 deletions src/Validator.php
Expand Up @@ -138,6 +138,8 @@ public static function validateNamingConvention($name)
}

return preg_replace('/[^a-zA-Z0-9_x7f-xff]*/', '', preg_replace('/^[^a-zA-Z_x7f-xff]*/', '', $name));


}

/**
Expand All @@ -163,7 +165,7 @@ public static function isPrimitive($str)
*/
private static function validateClassName($className)
{
$validClassName = self::validateNamingConvention($className);
$validClassName = self::applyPrefixAndSuffix(self::validateNamingConvention($className));

if (class_exists($validClassName))
{
Expand Down Expand Up @@ -194,7 +196,7 @@ private static function validateTypeName($type)
if (substr($type, -2) == "[]" || strtolower(substr($type, 0, 7)) == "arrayof")
{
return 'array';
}
}

switch (strtolower($type))
{
Expand All @@ -212,9 +214,13 @@ private static function validateTypeName($type)
case "string": case "token": case "normalizedstring": case "hexbinary":
return 'string';
break;

case "anytype":
return 'SoapVar';
break;

default:
$validType = self::validateNamingConvention($type);
$validType = self::applyPrefixAndSuffix(self::validateNamingConvention($type));
break;
}

Expand All @@ -236,5 +242,16 @@ private static function isKeyword($str)
{
return in_array(strtolower($str), self::$keywords);
}

/**
* Applies config prefix and suffix to a string
*
* @param string $str
*/
private static function applyPrefixAndSuffix($str)
{
$config = Generator::getInstance()->getConfig();
return $config->getPrefix() . (($config->getPrefix()) ? ucfirst($str) : $str) . $config->getSuffix();
}
}

0 comments on commit ad5d37d

Please sign in to comment.