/** * Call the WSDL generator to build class(es) * * @param string $uri * @param string $api */ private function generateClassFromWsdl($uri, $api) { $path = $this->generator->hostPath($uri); $this->output->writeln("<comment>Generating {$api} from {$path}...</comment>"); if (!$this->input->getOption('dryRun')) { $this->wsdl_generator->generate(new Config(['constructorParamsDefaultToNull' => $this->input->getOption('paramsDefaultNull'), 'inputFile' => $path, 'namespaceName' => $this->generator->generatedNamespace(), 'outputDir' => $this->generator->generatedDirectory(), 'sharedTypes' => $this->input->getOption('sharedTypes'), 'soapClientClass' => '\\' . $this->generator->libraryNamespace('SoapClient')])); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $endpoint = $input->getArgument('endpoint'); $directory = $input->getArgument('directory'); $generator_options = ['inputFile' => $endpoint, 'outputDir' => $directory]; if ($input->hasOption('namespace')) { $generator_options['namespaceName'] = $input->getOption('namespace'); } $tmp_directory = $directory . '_tmp'; $fs = new Filesystem(); if ($remove_existing = $input->hasOption('remove-existing')) { $counter = 1; while ($fs->exists($tmp_directory)) { $tmp_directory .= '_' . $counter++; } $output->writeln("Moving the existing directory to the temp directory ({$directory} -> {$tmp_directory})."); $fs->rename($directory, $tmp_directory); } if ($endpoint) { try { $output->writeln("Building WSDL to the directory: {$directory}"); $generator = new WsdlGenerator(); $generator->generate(new WsdlConfig($generator_options)); $output->writeln('WSDL has been built successfully'); if ($remove_existing) { $output->writeln("Removing the temp directory: {$tmp_directory}"); $fs->remove($tmp_directory); } } catch (\Exception $e) { $output->writeln($output->getFormatter()->format('The WSDL was not built due to an error')); $output->writeln($output->getFormatter()->format("Error Code: {$e->getCode()}")); $output->writeln($output->getFormatter()->format("Error Message: {$e->getMessage()}")); if ($remove_existing) { $output->writeln("Moving the temp directory back to the existing location ({$tmp_directory} -> {$directory})"); $fs->rename($tmp_directory, $directory); } } } }
protected function execute(InputInterface $input, OutputInterface $output) { $options = ['inputFile' => $input->getArgument('inputFile'), 'outputDir' => $input->getArgument('outputDir')]; if ($namespaceName = $input->getOption('namespaceName')) { $options['namespaceName'] = $namespaceName; } if ($operationNames = $input->getOption('operationNames')) { $options['operationNames'] = $operationNames; } if ($soapClientClass = $input->getOption('soapClientClass')) { $options['soapClientClass'] = $soapClientClass; } if ($soapClientOptions = $input->getOption('soapClientOptions')) { $parsedOptions = []; foreach ($soapClientOptions as $value) { $valueParts = explode(',', $value); $parsedOptions[$valueParts[0]] = $valueParts[1]; } $options['soapClientOptions'] = $parsedOptions; } $output->writeln('<info>##### Generating for Options #####</info>'); foreach ($options as $optionName => $optionValue) { if (is_array($optionValue)) { $output->writeln("<comment>{$optionName}</comment>"); foreach ($optionValue as $key => $value) { $output->writeln("<comment> {$key} = {$value}</comment>"); } } else { $output->writeln("<comment>{$optionName} = {$optionValue}</comment>"); } } try { $generator = new Generator(); $generator->generate(new Config($options)); $output->writeln('<info>##### Generated!! Check your output directory. #####</info>'); } catch (\Exception $ex) { $output->writeln("<error>" . $ex->getMessage() . "</error>"); } }
<?php /** * @package Wsdl2PhpGenerator */ use Wsdl2PhpGenerator\Console\Application; use Wsdl2PhpGenerator\Console\GenerateCommand; use Wsdl2PhpGenerator\Generator; require 'vendor/autoload.php'; $app = new Application('wsdl2php', '2.4.0'); $command = new GenerateCommand(); $command->setGenerator(Generator::getInstance()); $app->add($command); $app->run();
<?php use Wsdl2PhpGenerator\Generator; use Wsdl2PhpGenerator\Config; include_once 'vendor/autoload.php'; if (!isset($argv[1])) { echo "input file must be provided" . PHP_EOL; return 1; } $inputFile = $argv[1]; $generator = new Generator(); $generator->generate(new Config(['inputFile' => $inputFile, 'outputDir' => __DIR__ . '/src/Ongoing', 'namespaceName' => 'Ongoing'])); $serviceFile = __DIR__ . '/src/Ongoing/Service.php'; file_put_contents($serviceFile, str_replace($inputFile, '', file_get_contents($serviceFile)));
public function main() { try { /* * PROPERTIES VALIDATION */ // check if the proxy flag has been set $proxy = $this->getProject()->getUserProperty('proxy'); $proxy = $proxy !== null ? filter_var($proxy, FILTER_VALIDATE_BOOLEAN) : true; // determine the url of the WSDL document $wsdlUrl = $this->getProject()->getUserProperty('wsdl.url'); $wsdlUrl = $wsdlUrl ? $wsdlUrl : static::WSDL_URL; /* * PROXY CONFIGURATION */ // read the proxy configuration from the environment variables $proxy = $proxy ? current(array_filter(array(getenv('HTTP_PROXY'), getenv('http_proxy')), 'strlen')) : null; // prepare an empty url for the stream context $streamContextProxyUrl = null; // if the proxy is configured in the system if ($proxy) { // parse the WSDL url $parsedWsdlPath = Url::createFromUrl($wsdlUrl); // parse the proxy url $proxy = Url::createFromUrl($proxy); // if not fetching the wsdl file from filesystem and a proxy has been configured if ($parsedWsdlPath->getScheme()->get() !== 'file') { $streamContextProxyUrl = 'tcp://' . $proxy->getAuthority() . $proxy->getRelativeUrl(); libxml_set_streams_context(stream_context_get_default(array($proxy->getScheme()->get() => array('proxy' => $streamContextProxyUrl, 'request_fulluri' => true)))); } } /* * INITIALIZATION */ // prepare the path to the generated code $outputDir = $this->project->getBasedir() . '/src'; $output = new ConsoleOutput(); $progress = new ProgressBar($output, 100); $progress->setFormat(ProgressBar::getFormatDefinition('normal') . ' %message%...'); $progress->setMessage('Starting'); $progress->start(); $progress->setMessage('Cleaning the environment'); // clean the output directory array_map('unlink', glob($outputDir . '/*')); $progress->advance(10); /* * GENERATION */ // prepare the generator configuration $progress->setMessage('Configuring the generator'); $optionFeatures = array(); if ($proxy) { /* @var \League\Url\UrlInterface $proxy */ $optionFeatures['proxy_host'] = $proxy->getHost()->get(); $optionFeatures['proxy_port'] = $proxy->getPort()->get(); } $config = new Config($wsdlUrl, $outputDir); $config->setNoTypeConstructor(true); $config->setOptionFeatures($optionFeatures); $config->setCreateAccessors(false); $config->setWsdlCache(false); $progress->advance(10); // generate the code $progress->setMessage('Generating the code'); $gen = new Generator(); $gen->generate($config); $progress->advance(10); /* * FIX */ // the 'optionFeatures' configuration options is misused by the generator: // it is correctly used as the default 'features' options of the service (its values are bitwised and put // in the service class constructor), but it is also used as the '$options' argument of the \SoapClient // class when it connects to the service to inspect it (hence the need to define the 'proxy_host' and // 'proxy_port' keys). this makes the generated code clash, so we need to fix it, removing the unneeded // values from the bitwise operation. $defaultFeatures = array('SOAP_SINGLE_ELEMENT_ARRAYS', 'SOAP_WAIT_ONE_WAY_CALLS'); // fix the option 'features' management $fileContent = file_get_contents("{$outputDir}/ClabService.php"); $fileContent = preg_replace('/(\\$options\\[\'features\'\\] = ).*/', '$1' . implode(' | ', $defaultFeatures) . ';', $fileContent, -1, $count); // if no features option has been found, they must be added manually if ($count === 0) { $fileContent = preg_replace('/parent::__construct/', "if (isset(\$options['features']) == false) {\n\$options['features'] = " . implode(' | ', $defaultFeatures) . ";\n}\n\nparent::__construct", $fileContent, -1, $count); } file_put_contents("{$outputDir}/ClabService.php", $fileContent); /* * LICENSE MANAGEMENT */ $progress->setMessage('Applying the license to the generated files'); // read the license header $licenseHeader = file_get_contents($this->project->getBasedir() . '/resources/license_header.txt'); // print the license on top of every file foreach (glob($outputDir . '/*.php') as $sourceFile) { $fileContent = file_get_contents($sourceFile); $fileContent = preg_replace('/^(<\\?php)/', "\$1\n\n" . $licenseHeader, $fileContent); file_put_contents($sourceFile, $fileContent); } unset($sourceFile); $progress->advance(10); /* * CODING STANDARDS FIXES */ // create the coding standards fixer $progress->setMessage('Configuring the Coding Standards fixer'); $fixer = new Fixer(); $csConfig = new CSConfig(); $csConfig->setDir($outputDir); $progress->advance(10); // register all the existing fixers $fixer->registerBuiltInFixers(); $progress->advance(10); // register all fixers from all PSR levels /* @var $csFixer \Symfony\CS\FixerInterface */ $fixers = array(); foreach ($fixer->getFixers() as $csFixer) { if ($csFixer->getLevel() === ($csFixer->getLevel() & FixerInterface::PSR2_LEVEL)) { $fixers[] = $csFixer; } } // fix the generated code $progress->setMessage('Fixing the generated code'); $csConfig->fixers($fixers); $progress->advance(10); $fixer->fix($csConfig); $progress->advance(10); $progress->setFormat(ProgressBar::getFormatDefinition('normal') . ' %message%'); $progress->setMessage('Done'); $progress->finish(); $output->writeln(''); } catch (\Exception $e) { $this->log($e->getMessage(), \Project::MSG_ERR); } }