Use class of Zend where proxy is integrated to deliver .NET compatible service

This commit is contained in:
Michael RICOIS 2012-06-01 15:17:49 +00:00
parent bbfe653421
commit 6b699461ea

View File

@ -3,35 +3,31 @@ require_once 'framework/fwk.php';
class ServiceController extends Zend_Controller_Action class ServiceController extends Zend_Controller_Action
{ {
public function init(){}
public function indexAction() public function indexAction()
{ {
$this->_helper->layout->disableLayout(); $this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(); $this->_helper->viewRenderer->setNoRender();
require_once 'Zend/Exception.php';
require_once 'Zend/Soap/AutoDiscover.php';
require_once 'Zend/Soap/Server.php';
require_once 'WsScore/WrappedServiceProxy.php';
$request = $this->getRequest(); $request = $this->getRequest();
//Nom du service //Get the service name, make sure the string is in lower case
$serviceName = $request->getParam('service', 'Entreprise'); $serviceName = strtolower($request->getParam('service', 'Entreprise'));
//Chemin du fichier de configuration des versions //ClassName and Directory with first letter capitalized
$pathServiceVersion = 'WsScore/'.ucfirst($serviceName).'/Versions.ini'; $serviceClassName = ucfirst($serviceName);
//Service spécifique client //path of file where versions of service is store
if (strtolower($serviceName) == 'clients') { $pathServiceVersion = 'WsScore/'.$serviceClassName.'/Versions.ini';
$client = $request->getParam('client', '');
//Liste des clients //Customize service for customers
if ('clients' == $serviceName ) {
$client = strtolower($request->getParam('client', ''));
$clientClassName = ucfirst($client);
//Get list of customers
$clients = array(); $clients = array();
$listeClients = new Zend_Config_Ini('WsScore/Clients/Clients.ini'); $listeClients = new Zend_Config_Ini('WsScore/Clients/Clients.ini');
foreach ( $listeClients->toArray() as $section => $params ){ foreach ( $listeClients->toArray() as $section => $params ){
if ($params['actif']){ if ($params['actif']) {
$clients[] = $section; $clients[] = $section;
} }
} }
@ -39,9 +35,9 @@ class ServiceController extends Zend_Controller_Action
echo 'Service clients introuvable !'; echo 'Service clients introuvable !';
exit; exit;
} }
$pathServiceVersion = 'WsScore/Clients/'.ucfirst($client).'/Versions.ini'; $pathServiceVersion = 'WsScore/Clients/'.$clientClassName.'/Versions.ini';
} }
// Liste des versions // Liste des versions
$configServiceVersions = new Zend_Config_Ini($pathServiceVersion); $configServiceVersions = new Zend_Config_Ini($pathServiceVersion);
foreach( $configServiceVersions->toArray() as $section => $params ){ foreach( $configServiceVersions->toArray() as $section => $params ){
@ -52,7 +48,7 @@ class ServiceController extends Zend_Controller_Action
} }
$version = $request->getParam('version', 'v'.$defautVersion); $version = $request->getParam('version', 'v'.$defautVersion);
$version = substr($version, 1); $version = substr($version, 1);
// Version inexistante // Version inexistante
if ( !array_key_exists($version, $serviceVersions) ) { if ( !array_key_exists($version, $serviceVersions) ) {
echo "Version inexistante."; echo "Version inexistante.";
@ -63,14 +59,14 @@ class ServiceController extends Zend_Controller_Action
echo "Version désactivée."; echo "Version désactivée.";
exit; exit;
} }
// Charger les classes et les types pour le service suivant la version // Charger les classes et les types pour le service suivant la version
if (strtolower($serviceName) == 'clients') { if ('clients' == $serviceName) {
$pathServiceClassIni = 'WsScore/Clients/'.ucfirst($client).'/v'.$version.'/Entreprise.ini'; $pathServiceClassIni = 'WsScore/Clients/'.$clientClassName.'/v'.$version.'/Entreprise.ini';
$pathServiceClassPhp = 'WsScore/Clients/'.ucfirst($client).'/v'.$version.'/Entreprise.php'; $pathServiceClassPhp = 'WsScore/Clients/'.$clientClassName.'/v'.$version.'/Entreprise.php';
//Gestion du mode de génération du wsdl //Gestion du mode de génération du wsdl
if ( APPLICATION_ENV == 'development' if ( APPLICATION_ENV == 'development'
|| array_key_exists('mode', $serviceVersions[$version]) || array_key_exists('mode', $serviceVersions[$version])
&& $serviceVersions[$version]['mode']=='auto') { && $serviceVersions[$version]['mode']=='auto') {
$pathServiceUrl = 'clients/'.$client.'/v'.$version.'?wsdl-auto'; $pathServiceUrl = 'clients/'.$client.'/v'.$version.'?wsdl-auto';
} else { } else {
@ -78,21 +74,21 @@ class ServiceController extends Zend_Controller_Action
} }
//On redéfini le nom du service //On redéfini le nom du service
$serviceName = 'Entreprise'; $serviceName = 'Entreprise';
$fichierWsdl = ucfirst($client).'-'.$serviceName.'-'.$version.'.wsdl'; $fichierWsdl = $clientClassName.'-'.$serviceName.'-'.$version.'.wsdl';
} else { } else {
$pathServiceClassIni = 'WsScore/'.ucfirst($serviceName).'/v'.$version.'/'.ucfirst($serviceName).'.ini'; $pathServiceClassIni = 'WsScore/'.$serviceClassName.'/v'.$version.'/'.$serviceClassName.'.ini';
$pathServiceClassPhp = 'WsScore/'.ucfirst($serviceName).'/v'.$version.'/'.ucfirst($serviceName).'.php'; $pathServiceClassPhp = 'WsScore/'.$serviceClassName.'/v'.$version.'/'.$serviceClassName.'.php';
//Gestion du mode de génération du wsdl //Gestion du mode de génération du wsdl
if ( APPLICATION_ENV == 'development' if ( APPLICATION_ENV == 'development'
|| array_key_exists('mode', $serviceVersions[$version]) || array_key_exists('mode', $serviceVersions[$version])
&& $serviceVersions[$version]['mode']=='auto') { && $serviceVersions[$version]['mode']=='auto') {
$pathServiceUrl = $serviceName.'/v'.$version.'?wsdl-auto'; $pathServiceUrl = $serviceName.'/v'.$version.'?wsdl-auto';
} else { } else {
$pathServiceUrl = $serviceName.'/v'.$version.'?wsdl'; $pathServiceUrl = $serviceName.'/v'.$version.'?wsdl';
} }
$fichierWsdl = ucfirst($serviceName).'-'.$version.'.wsdl'; $fichierWsdl = $serviceClassName.'-'.$version.'.wsdl';
} }
//Génération du tableau de mapping //Génération du tableau de mapping
$classmap = array(); $classmap = array();
$wsConfig = new Zend_Config_Ini($pathServiceClassIni); $wsConfig = new Zend_Config_Ini($pathServiceClassIni);
@ -103,25 +99,25 @@ class ServiceController extends Zend_Controller_Action
} }
//Inclusion des classes de données //Inclusion des classes de données
require_once $pathServiceClassPhp; require_once $pathServiceClassPhp;
// Fourniture du wsdl // Fourniture du wsdl
if ( isset($_GET['wsdl']) && file_exists($fichierWsdl) ) { if ( isset($_GET['wsdl']) && file_exists($fichierWsdl) ) {
if (!headers_sent()) { if (!headers_sent()) {
header('Content-Type: text/xml'); header('Content-Type: text/xml');
} }
echo file_get_contents($fichierWsdl); echo file_get_contents($fichierWsdl);
} elseif ( isset($_GET['wsdl']) && !file_exists($fichierWsdl) } elseif ( isset($_GET['wsdl']) && !file_exists($fichierWsdl)
|| isset($_GET['wsdl-generate']) || isset($_GET['wsdl-generate'])
|| isset($_GET['wsdl-auto']) ) { || isset($_GET['wsdl-auto']) ) {
// Définition du webservice // Définition du webservice
$wsdl = new Zend_Soap_AutoDiscover(); $wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setComplexTypeStrategy('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence'); $wsdl->setComplexTypeStrategy('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence');
$wsdl->setOperationBodyStyle( array('use' => 'literal') ); $wsdl->setOperationBodyStyle( array('use' => 'literal') );
$wsdl->setBindingStyle( array('style' => 'document') ); $wsdl->setBindingStyle( array('style' => 'document') );
$wsdl->setClass(ucfirst($serviceName)); $wsdl->setClass($serviceClassName);
// Enregistrement du WSDL dans un fichier // Enregistrement du WSDL dans un fichier
if ( isset($_GET['wsdl-generate']) ) { if ( isset($_GET['wsdl-generate']) ) {
@ -138,12 +134,12 @@ class ServiceController extends Zend_Controller_Action
header('Content-Type: text/xml'); header('Content-Type: text/xml');
} }
echo file_get_contents($fichierWsdl); echo file_get_contents($fichierWsdl);
// Envoi sur la sortie standard le wsdl sans enregistrement dans un fichier // Envoi sur la sortie standard le wsdl sans enregistrement dans un fichier
} elseif ( isset($_GET['wsdl-auto']) ){ } elseif ( isset($_GET['wsdl-auto']) ){
$wsdl->handle(); $wsdl->handle();
} }
// Fourniture du service // Fourniture du service
} else { } else {
@ -154,20 +150,21 @@ class ServiceController extends Zend_Controller_Action
$hostName = $this->getRequest()->getHttpHost(); $hostName = $this->getRequest()->getHttpHost();
$server = new Zend_Soap_Server('http://'.$hostName.'/'.$pathServiceUrl); $server = new Zend_Soap_Server('http://'.$hostName.'/'.$pathServiceUrl);
} }
$proxy = new WrappedService_Proxy(ucfirst($serviceName), array(), array('wrappedParts' => true));
$server->setSoapFeatures(SOAP_USE_XSI_ARRAY_TYPE + SOAP_SINGLE_ELEMENT_ARRAYS); $server->setSoapFeatures(SOAP_USE_XSI_ARRAY_TYPE + SOAP_SINGLE_ELEMENT_ARRAYS);
$server->setClassmap($classmap); $server->setClassmap($classmap);
$server->setEncoding('UTF-8'); $server->setEncoding('UTF-8');
$server->registerFaultException(array('WsScores_Exception')); $server->registerFaultException(array('WsScores_Exception'));
$server->setObject($proxy); $server->setWsiCompliant(true);
$server->setObject(new $serviceClassName());
$server->handle(); $server->handle();
//Pour débuggage ultime //Pour débuggage ultime
$debug = false; $debug = false;
if ($debug){ if ($debug){
$request = $server->getLastRequest(); $request = $server->getLastRequest();
file_put_contents(APPLICATION_PATH . '/../request.log', $request); file_put_contents(APPLICATION_PATH . '/../request.log', $request);
$response = $server->getLastResponse(); $response = $server->getLastResponse();
file_put_contents(APPLICATION_PATH . '/../response.log', $response); file_put_contents(APPLICATION_PATH . '/../response.log', $response);
} }
} }