webservice/application/controllers/ServiceController.php

123 lines
3.7 KiB
PHP
Raw Normal View History

2010-08-30 07:49:44 +00:00
<?php
ini_set("soap.wsdl_cache_enabled", "0");
require_once 'framework/fwk.php';
2010-08-30 07:49:44 +00:00
class ServiceController extends Zend_Controller_Action
{
protected $classmap = array();
2011-02-03 14:04:40 +00:00
public function init(){}
2010-08-30 07:49:44 +00:00
public function indexAction()
{
$this->_helper->viewRenderer->setNoRender();
require_once 'Zend/Exception.php';
2010-08-30 07:49:44 +00:00
require_once 'Zend/Soap/AutoDiscover.php';
require_once 'Zend/Soap/Server.php';
require_once 'WsScore/WrappedServiceProxy.php';
2011-02-03 14:04:40 +00:00
$request = $this->getRequest();
// Liste des versions
$configServiceVersions = new Zend_Config_Ini('WsScore/Entreprise/Versions.ini');
foreach( $configServiceVersions->toArray() as $section => $params ){
$serviceVersions[$section] = $params;
if ($params['actif']) {
$defautVersion = $section;
}
}
$version = $request->getParam('version', 'v'.$defautVersion);
$version = substr($version, 1);
// Version inexistante
if ( !array_key_exists($version, $serviceVersions) ) {
echo "Version inexistante.";
2011-02-03 14:04:40 +00:00
exit;
}
// Version désactivé
if ( !$serviceVersions[$version]['actif'] ) {
echo "Version désactivée.";
2011-02-03 14:04:40 +00:00
exit;
}
// Charger les classes et les types pour le service suivant la version
$pathClassService = 'WsScore/Entreprise/v'.$version.'/';
$wsConfig = new Zend_Config_Ini($pathClassService.'Entreprise.ini');
2011-02-03 14:04:40 +00:00
foreach($wsConfig->Type->toArray() as $Type){
$this->classmap[$Type] = $Type;
}
require_once $pathClassService.'Entreprise.php';
$fichierWsdl = 'Entreprise-'.$version.'.wsdl';
2010-08-30 07:49:44 +00:00
// Fourniture du wsdl
2011-02-03 14:04:40 +00:00
if (isset($_GET['wsdl'])) {
if (file_exists($fichierWsdl)) {
2010-08-30 07:49:44 +00:00
if (!headers_sent()) {
header('Content-Type: text/xml');
}
echo file_get_contents($fichierWsdl);
2011-02-03 14:04:40 +00:00
} else {
echo "Erreur WSDL inexistant ";
2010-08-30 07:49:44 +00:00
}
2010-10-18 07:25:31 +00:00
}
2010-08-30 07:49:44 +00:00
// Génération du wsdl
2011-02-03 14:04:40 +00:00
elseif ( isset($_GET['wsdl-generate']) || isset($_GET['wsdl-auto']) ) {
2010-08-30 07:49:44 +00:00
// Définition du webservice
$wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setComplexTypeStrategy('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence');
2010-08-30 07:49:44 +00:00
$wsdl->setOperationBodyStyle( array('use' => 'literal') );
$wsdl->setBindingStyle( array('style' => 'document') );
$wsdl->setClass('Entreprise');
2010-08-30 07:49:44 +00:00
// Enregistrement du WSDL dans un fichier
2011-02-03 14:04:40 +00:00
if ( isset($_GET['wsdl-generate']) ) {
if (!file_exists($fichierWsdl)) {
$wsdl->dump($fichierWsdl);
echo "Le fichier $fichierWsdl a été généré";
2011-02-03 14:04:40 +00:00
} else {
echo "ERREUR : Le fichier $fichierWsdl est déjà présent !";
}
2010-10-18 07:25:31 +00:00
}
2010-08-30 07:49:44 +00:00
// Envoi sur la sortie standard le wsdl
2011-02-03 14:04:40 +00:00
elseif ( isset($_GET['wsdl-auto']) ){
2010-08-30 07:49:44 +00:00
$wsdl->handle();
}
// Fourniture du service
} else {
// Données de configuration placé dans le registre - a supprimer
$webservicesConfig = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/webservices.ini', APPLICATION_ENV);
Zend_Registry::set('webservicesConfig',$webservicesConfig);
2010-08-30 07:49:44 +00:00
// traitement
2010-10-18 07:25:31 +00:00
try
{
if (APPLICATION_ENV == 'production') {
// @todo Vérifier la présence du fichier sinon envoyer erreur
2011-01-19 17:10:26 +00:00
$server = new Zend_Soap_Server($fichierWsdl);
} else {
$hostName = $this->getRequest()->getHttpHost();
$server = new Zend_Soap_Server('http://'.$hostName.'/service?wsdl-auto');
}
$proxy = new WrappedService_Proxy('Entreprise', array(), array('wrappedParts' => true));
2010-08-30 07:49:44 +00:00
$server->setObject($proxy);
$server->setClassmap($this->classmap);
2010-11-08 17:23:08 +00:00
$server->handle();
}
catch (Zend_Exception $e)
{
Zend_Registry::get('log')->err("Service - ".$e->getMessage());
2010-08-30 07:49:44 +00:00
}
}
exit;
}
2010-08-30 07:49:44 +00:00
}