Skip to content

Instantly share code, notes, and snippets.

@Gemorroj
Created December 19, 2016 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gemorroj/a786517c73b1c0d689cdc94b2ba5427f to your computer and use it in GitHub Desktop.
Save Gemorroj/a786517c73b1c0d689cdc94b2ba5427f to your computer and use it in GitHub Desktop.
<?php
namespace Example;
use SoftClub\Butb2Bundle\Service\Wsdl\ServiceType\Get;
use SoftClub\Butb2Bundle\Service\Wsdl\StructType\GetClientsListReq;
use SoftClub\Butb2Bundle\Service\Wsdl\StructType\GetClientsList;
class Example
{
/**
* @param int|null $id
* @return \SoftClub\Butb2Bundle\Entity\FirmSelect[]
*/
public function getClientsList($id = null)
{
$req = new GetClientsListReq();
$req->setHeader($this->header);
$req->setId($id);
$type = new GetClientsList($req);
$clients = new Get();
$response = $clients->GetClientsList($type);
$response = Proxy::handle($clients, $req, $response);
return (new Mapper\FirmSelectMapper())->get($response);
}
}
<?php
namespace SoftClub\Butb2Bundle\Command;
use SoftClub\Butb2Bundle\Service\SoapClient;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ProcessBuilder;
use WsdlToPhp\PackageGenerator\Generator\Generator;
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
/**
* Генерация PHP классов на основе Wsdl
*/
class ServiceWsdlUpdateCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('service:wsdl:update')
->setDescription('Wsdl update')
->addArgument(
'bundleName', InputArgument::OPTIONAL, 'Bundle name', 'SoftClubButb2Bundle'
)
->addArgument(
'target', InputArgument::OPTIONAL, 'The target directory', 'Wsdl'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$bundleName = $input->getArgument('bundleName');
$path = $container->get('kernel')->getBundle($bundleName)->getPath();
$wsdlPath = $path . '/Service/' . $input->getArgument('target');
/**
* @var Filesystem $filesystem
*/
//$filesystem = $container->get('filesystem');
//$filesystem->remove($wsdlPath);
//$filesystem->mkdir($wsdlPath, 0755);
$this->build(
$wsdlPath,
$container->getParameter('soap.wsdl'),
$container->getParameter('soap.login'),
$container->getParameter('soap.password')
);
$this->generateAutoload();
$output->writeln('Wsdl updated');
}
/**
* Создать PHP классы на основе Wsdl
*
* @param string $wsdlPath путь к каталогу, для сохранения php классов
* @param string $url адрес к описанию web-сервисов
* @param string $login логин
* @param string $pass пароль
*/
protected function build($wsdlPath, $url, $login, $pass)
{
SoapClient::setContainer($this->getContainer());
// Options definition
$options = GeneratorOptions::instance();
$options
->setSoapClientClass(SoapClient::class)
->setStandalone(false)
->setGenerateTutorialFile(false)
->setNamespace('SoftClub\Butb2Bundle\Service\Wsdl')
->setAddComments(array())
->setOrigin($url)
->setBasicLogin($login)
->setBasicPassword($pass)
->setDestination($wsdlPath);
// Generator instanciation and package generation
$generator = new Generator($options);
$generator->generatePackage();
}
/**
* Генератор автозагрузки классов
*
* @throws \RuntimeException
*/
protected function generateAutoload()
{
$composer = __DIR__ . '/../../../../composer.phar';
if (!$this->getContainer()->get('filesystem')->exists($composer)) {
throw new \RuntimeException('Composer not found');
}
$processBuilder = new ProcessBuilder(['php', $composer, 'dump-autoload']);
$processBuilder->getProcess()->mustRun();
}
}
<?php
namespace SoftClub\Butb2Bundle\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
use WsdlToPhp\PackageBase\AbstractSoapClientBase;
use SoftClub\Butb2Bundle\Service\Wsdl\ClassMap;
abstract class SoapClient extends AbstractSoapClientBase
{
/**
* @var ContainerInterface
*/
private static $container;
/**
* ContainerInterface $container
*/
public static function setContainer(ContainerInterface $container)
{
self::$container = $container;
}
/**
* @param array $options
* @see https://github.com/WsdlToPhp/PackageBase/issues/3
* полностью дублируем метод из-за того, что не можем переопределить self::getDefaultWsdlOptions() (используется родительский метод)
*/
public function initSoapClient(array $options)
{
$wsdlOptions = array();
$defaultWsdlOptions = self::getDefaultWsdlOptions();
foreach ($defaultWsdlOptions as $optioName => $optionValue) {
if (array_key_exists($optioName, $options) && !empty($options[$optioName])) {
$wsdlOptions[str_replace(self::OPTION_PREFIX, '', $optioName)] = $options[$optioName];
} elseif (!empty($optionValue)) {
$wsdlOptions[str_replace(self::OPTION_PREFIX, '', $optioName)] = $optionValue;
}
}
if (array_key_exists(str_replace(self::OPTION_PREFIX, '', self::WSDL_URL), $wsdlOptions)) {
$wsdlUrl = $wsdlOptions[str_replace(self::OPTION_PREFIX, '', self::WSDL_URL)];
unset($wsdlOptions[str_replace(self::OPTION_PREFIX, '', self::WSDL_URL)]);
$soapClientClassName = $this->getSoapClientClassName();
self::setSoapClient(new $soapClientClassName($wsdlUrl, $wsdlOptions));
}
}
/**
* @inheritdoc
*/
public static function getDefaultWsdlOptions()
{
return array(
// Путь к WSDL
self::WSDL_URL => self::$container->getParameter('soap.wsdl'),
// Логин для авторизации в веб-сервисах
self::WSDL_LOGIN => self::$container->getParameter('soap.login'),
// Пароль для авторизации в веб-сервисах
self::WSDL_PASSWORD => self::$container->getParameter('soap.password'),
// Кидать исключения при ошибках (не трогать, всегда true)
self::WSDL_EXCEPTIONS => true,
// Трассировка (true не development сервере, false на production)
self::WSDL_TRACE => self::$container->get('kernel')->isDebugEnvironment(),
// компрессия
self::WSDL_COMPRESSION => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
// кэширование
self::WSDL_CACHE_WSDL => self::$container->get('kernel')->isDebugEnvironment() ? WSDL_CACHE_NONE : WSDL_CACHE_BOTH,
// приводим long в string
self::WSDL_TYPEMAP => array(
array(
'type_ns' => 'http://www.w3.org/2001/XMLSchema',
'type_name' => 'long',
'to_xml' => function ($longVal) {
return '<long>' . $longVal . '</long>';
},
'from_xml' => function ($xmlFragmentString) {
return (string)strip_tags($xmlFragmentString);
},
),
array(
'type_ns' => 'http://www.w3.org/2001/XMLSchema',
'type_name' => 'short',
'to_xml' => function ($shortVal) {
return '<short>' . $shortVal . '</short>';
},
'from_xml' => function ($xmlFragmentString) {
return (string)strip_tags($xmlFragmentString);
},
)
),
self::WSDL_CLASSMAP => ClassMap::get(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment