webservice/application/controllers/ServiceController.php
2011-10-13 18:42:45 +00:00

159 lines
5.4 KiB
PHP

<?php
require_once 'framework/fwk.php';
class ServiceController extends Zend_Controller_Action
{
public function init(){}
public function indexAction()
{
$this->_helper->layout->disableLayout();
$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();
//Nom du service
$serviceName = $request->getParam('service', 'Entreprise');
//Chemin
$pathServiceVersion = 'WsScore/'.ucfirst($serviceName).'/Versions.ini';
//Service spécifique client
if (strtolower($serviceName) == 'clients') {
$client = $request->getParam('client', '');
//Liste des clients
$clients = array();
$listeClients = new Zend_Config_Ini('WsScore/Clients/Clients.ini');
foreach ( $listeClients->toArray() as $section => $params ){
if ($params['actif']){
$clients[] = $section;
}
}
if (!in_array($client, $clients)){
echo 'Service clients introuvable !';
exit;
}
$pathServiceVersion = 'WsScore/Clients/'.ucfirst($client).'/Versions.ini';
}
// Liste des versions
$configServiceVersions = new Zend_Config_Ini($pathServiceVersion);
foreach( $configServiceVersions->toArray() as $section => $params ){
$serviceVersions[$section] = $params;
if ($params['defaut']) {
$defautVersion = $section;
}
}
$version = $request->getParam('version', 'v'.$defautVersion);
$version = substr($version, 1);
// Version inexistante
if ( !array_key_exists($version, $serviceVersions) ) {
echo "Version inexistante.";
exit;
}
// Version désactivé
if ( !$serviceVersions[$version]['actif'] ) {
echo "Version désactivée.";
exit;
}
// Charger les classes et les types pour le service suivant la version
if (strtolower($serviceName) == 'clients') {
$pathServiceClassIni = 'WsScore/Clients/'.ucfirst($client).'/v'.$version.'/Entreprise.ini';
$pathServiceClassPhp = 'WsScore/Clients/'.ucfirst($client).'/v'.$version.'/Entreprise.php';
$pathServiceUrl = 'clients/'.$client.'/v'.$version.'?wsdl-auto';
//On redéfini le nom du service
$serviceName = 'Entreprise';
$fichierWsdl = ucfirst($client).'-'.$serviceName.'-'.$version.'.wsdl';
} else {
$pathServiceClassIni = 'WsScore/'.ucfirst($serviceName).'/v'.$version.'/'.ucfirst($serviceName).'.ini';
$pathServiceClassPhp = 'WsScore/'.ucfirst($serviceName).'/v'.$version.'/'.ucfirst($serviceName).'.php';
$pathServiceUrl = $serviceName.'/v'.$version.'?wsdl-auto';
$fichierWsdl = ucfirst($serviceName).'-'.$version.'.wsdl';
}
//Génération du tableau de mapping
$wsConfig = new Zend_Config_Ini($pathServiceClassIni);
foreach($wsConfig->Type->toArray() as $Type){
$classmap[$Type] = $Type;
}
//Inclusion des classes de données
require_once $pathServiceClassPhp;
// Génération/Founiture du wsdl
if ( isset($_GET['wsdl']) && file_exists($fichierWsdl) ) {
if (!headers_sent()) {
header('Content-Type: text/xml');
}
echo file_get_contents($fichierWsdl);
} elseif ( isset($_GET['wsdl']) && !file_exists($fichierWsdl)
|| isset($_GET['wsdl-generate'])
|| isset($_GET['wsdl-auto']) ) {
// Définition du webservice
$wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setComplexTypeStrategy('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence');
$wsdl->setOperationBodyStyle( array('use' => 'literal') );
$wsdl->setBindingStyle( array('style' => 'document') );
$wsdl->setClass(ucfirst($serviceName));
// Enregistrement du WSDL dans un fichier
if ( isset($_GET['wsdl-generate']) ) {
if (file_exists($fichierWsdl)) {
unlink($fichierWsdl);
}
$wsdl->dump($fichierWsdl);
echo "Le fichier $fichierWsdl a été généré";
} elseif (isset($_GET['wsdl']) && !file_exists($fichierWsdl)) {
$wsdl->dump($fichierWsdl);
if (!headers_sent()) {
header('Content-Type: text/xml');
}
echo file_get_contents($fichierWsdl);
// Envoi sur la sortie standard le wsdl
} elseif ( isset($_GET['wsdl-auto']) ){
$wsdl->handle();
}
// Fourniture du service
} else {
// traitement
if (APPLICATION_ENV == 'production' && file_exists($fichierWsdl)) {
$server = new Zend_Soap_Server($fichierWsdl);
} else {
$hostName = $this->getRequest()->getHttpHost();
//@todo : detection du nom du service
$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->setClassmap($classmap);
$server->setEncoding('UTF-8');
$server->registerFaultException(array('WsScores_Exception'));
$server->setObject($proxy);
$server->handle();
//Pour débuggage ultime
$debug = false;
if ($debug){
$request = $server->getLastRequest();
file_put_contents(APPLICATION_PATH . '/../request.log', $request);
$response = $server->getLastResponse();
file_put_contents(APPLICATION_PATH . '/../response.log', $response);
}
}
}
}