webservice/application/controllers/ServiceController.php

116 lines
3.1 KiB
PHP
Raw Normal View History

2010-08-30 07:49:44 +00:00
<?php
ini_set("soap.wsdl_cache_enabled", "0");
class ServiceController extends Zend_Controller_Action
{
protected $wsdlName = 'WsEntreprise';
protected $wsdlVersion = '0.1';
protected $classmap = array();
2010-08-30 07:49:44 +00:00
public function init()
{
$this->_helper->viewRenderer->setNoRender();
}
public function preDispatch()
{
$wsConfig = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/WsEntreprise.ini');
foreach ($wsConfig->Type as $type){
$this->classmap[$type] = $type;
}
}
2010-08-30 07:49:44 +00:00
public function indexAction()
{
// action body
require_once 'Zend/Exception.php';
require_once 'Zend/Soap/AutoDiscover.php';
require_once 'Zend/Soap/Server.php';
require_once 'WsScore/WsEntreprise.php';
require_once 'WsScore/WrappedServiceProxy.php';
$fichierWsdl = $this->wsdlName.'-'.$this->wsdlVersion.'.wsdl';
// Fourniture du wsdl
2010-10-18 07:25:31 +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);
2010-10-18 07:25:31 +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
2010-10-18 07:25:31 +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();
2010-09-15 16:18:38 +00:00
$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('WsEntreprise');
// Enregistrement du WSDL dans un fichier
2010-10-18 07:25:31 +00:00
if ( isset($_GET['wsdl-generate']) )
{
if (!file_exists($fichierWsdl))
{
$wsdl->dump($fichierWsdl);
echo "Le fichier $fichierWsdl a été généré";
2010-10-18 07:25:31 +00:00
}
else
{
// @todo envové un mail en mode production
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
2010-10-18 07:25:31 +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
$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
$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('WsEntreprise', array(), array('wrappedParts' => true));
2010-08-30 07:49:44 +00:00
$server->setObject($proxy);
$server->setClassmap($this->classmap);
2010-10-18 07:25:31 +00:00
//@todo Vérifier l'utilisateur, sinon renvoyé erreur soap
//$server->fault();
2010-08-30 07:49:44 +00:00
$server->handle();
2010-10-18 07:25:31 +00:00
2010-08-30 07:49:44 +00:00
} catch (Zend_Exception $e) {
Zend_Registry::get('log')->err("Service - ".$e->getMessage());
2010-08-30 07:49:44 +00:00
}
}
exit;
}
}