-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from veewee/wsdl-tools
Introduce WSDL CLI tools
- Loading branch information
Showing
11 changed files
with
517 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Soap\Wsdl\Console\AppFactory; | ||
|
||
(function () { | ||
$loaded = array_reduce( | ||
[ | ||
__DIR__.'/../vendor/autoload.php', // Used when executed in this package's SRC | ||
__DIR__.'/../../../autoload.php' // Used when executed in vendor/bin of your project | ||
], | ||
static function (?string $loaded, string $file): ?string { | ||
if ( ! $loaded && is_file($file)) { | ||
require_once($file); | ||
|
||
return $file; | ||
} | ||
|
||
return $loaded; | ||
} | ||
); | ||
|
||
if (!$loaded) { | ||
fwrite( | ||
STDERR, | ||
'You must set up the project dependencies, run the following commands:'.PHP_EOL. | ||
'curl -s http://getcomposer.org/installer | php'.PHP_EOL. | ||
'php composer.phar install'.PHP_EOL | ||
); | ||
exit(1); | ||
} | ||
|
||
$app = AppFactory::create(); | ||
$app->run(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,24 @@ | |
"email": "[email protected]" | ||
} | ||
], | ||
"bin": [ | ||
"bin/wsdl" | ||
], | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"require": { | ||
"php": "^8.0", | ||
"ext-dom": "*", | ||
"azjezz/psl": "^1.9", | ||
"league/uri": "^6.5", | ||
"league/uri-components": "^2.4", | ||
"php-soap/xml": "^1.2", | ||
"symfony/console": "^5.4|^6.0", | ||
"veewee/xml": "~1.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5" | ||
"phpunit/phpunit": "^9.5", | ||
"psalm/plugin-symfony": "^3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\Wsdl\Console; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Exception\LogicException; | ||
|
||
final class AppFactory | ||
{ | ||
/** | ||
* @throws LogicException | ||
*/ | ||
public static function create(): Application | ||
{ | ||
$app = new Application('wsdl-tools', '1.0.0'); | ||
$app->addCommands([ | ||
new Command\FlattenCommand(), | ||
new Command\ValidateCommand(), | ||
]); | ||
|
||
return $app; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\Wsdl\Console\Command; | ||
|
||
use Exception; | ||
use Psl\Filesystem; | ||
use Soap\Wsdl\Console\Helper\ConfiguredLoader; | ||
use Soap\Wsdl\Loader\CallbackLoader; | ||
use Soap\Wsdl\Loader\FlatteningLoader; | ||
use Soap\Wsdl\Loader\WsdlLoader; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
final class FlattenCommand extends Command | ||
{ | ||
public static function getDefaultName(): string | ||
{ | ||
return 'flatten'; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setDescription('Flatten a remote or local WSDL file into 1 file that contains all includes.'); | ||
$this->addArgument('wsdl', InputArgument::REQUIRED, 'Provide the URI of the WSDL you want to flatten'); | ||
$this->addArgument('output', InputArgument::REQUIRED, 'Define where the file must be written to'); | ||
$this->addOption('loader', 'l', InputOption::VALUE_REQUIRED, 'Customize the WSDL loader file that will be used'); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$style = new SymfonyStyle($input, $output); | ||
$loader = ConfiguredLoader::createFromConfig( | ||
$input->getOption('loader'), | ||
fn (WsdlLoader $loader) => $this->configureLoader($loader, $style) | ||
); | ||
$wsdl = $input->getArgument('wsdl'); | ||
$output = $input->getArgument('output'); | ||
|
||
$style->info('Flattening WSDL "'.$wsdl.'"'); | ||
$style->warning('This can take a while...'); | ||
$contents = $loader($wsdl); | ||
|
||
$style->info('Downloaded the WSDL. Writing it to "'.$output.'".'); | ||
|
||
Filesystem\write_file($output, $contents); | ||
|
||
$style->success('Succesfully flattened your WSDL!'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function configureLoader(WsdlLoader $loader, SymfonyStyle $style): WsdlLoader | ||
{ | ||
return new FlatteningLoader( | ||
new CallbackLoader(static function (string $location) use ($loader, $style): string { | ||
$style->write('> Loading '.$location . '...'); | ||
|
||
$result = $loader($location); | ||
$style->writeln(' DONE!'); | ||
|
||
return $result; | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\Wsdl\Console\Command; | ||
|
||
use Exception; | ||
use Soap\Wsdl\Console\Helper\ConfiguredLoader; | ||
use Soap\Wsdl\Xml\Validator; | ||
use Soap\Xml\Xpath\WsdlPreset; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use VeeWee\Xml\Dom\Document; | ||
use VeeWee\Xml\ErrorHandling\Issue\IssueCollection; | ||
|
||
final class ValidateCommand extends Command | ||
{ | ||
public static function getDefaultName(): string | ||
{ | ||
return 'validate'; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setDescription('Run validations a (flattened) WSDL file.'); | ||
$this->addArgument('wsdl', InputArgument::REQUIRED, 'Provide the URI of the WSDL you want to validate'); | ||
$this->addOption('loader', 'l', InputOption::VALUE_REQUIRED, 'Customize the WSDL loader file that will be used'); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$style = new SymfonyStyle($input, $output); | ||
$loader = ConfiguredLoader::createFromConfig($input->getOption('loader')); | ||
$wsdl = $input->getArgument('wsdl'); | ||
|
||
$style->info('Loading "'.$wsdl.'"...'); | ||
$document = Document::fromXmlString($loader($wsdl)); | ||
$xpath = $document->xpath(new WsdlPreset($document)); | ||
|
||
$result = $this->runValidationStage( | ||
$style, | ||
'Validating WSDL syntax', | ||
static fn () => $document->validate(new Validator\WsdlSyntaxValidator()) | ||
); | ||
|
||
$result = $result && $this->runValidationStage( | ||
$style, | ||
'Validating XSD types...', | ||
static function () use ($style, $document, $xpath): ?IssueCollection { | ||
$schemas = $xpath->query('//schema:schema'); | ||
if ($schemas->count() !== 1) { | ||
$style->warning('Skipped : XSD types can only be validated if there is one schema element.'); | ||
return null; | ||
} | ||
|
||
return $document->validate(new Validator\SchemaSyntaxValidator()); | ||
} | ||
); | ||
|
||
return $result ? self::SUCCESS : self::FAILURE; | ||
} | ||
|
||
/** | ||
* @param callable(): ?IssueCollection $validator | ||
*/ | ||
private function runValidationStage(SymfonyStyle $style, string $label, callable $validator): bool | ||
{ | ||
$style->info($label.'...'); | ||
$issues = $validator(); | ||
|
||
// Skipped ... | ||
if ($issues === null) { | ||
return true; | ||
} | ||
|
||
if ($issues->count()) { | ||
$style->block($issues->toString()); | ||
$style->error('Validation failed!'); | ||
return false; | ||
} | ||
|
||
$style->success('All good!'); | ||
return true; | ||
} | ||
} |
Oops, something went wrong.