1: <?php
2: /**
3: * CWebServiceAction class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CWebServiceAction implements an action that provides Web services.
13: *
14: * CWebServiceAction serves for two purposes. On the one hand, it displays
15: * the WSDL content specifying the Web service APIs. On the other hand, it
16: * invokes the requested Web service API. A GET parameter named <code>ws</code>
17: * is used to differentiate these two aspects: the existence of the GET parameter
18: * indicates performing the latter action.
19: *
20: * By default, CWebServiceAction will use the current controller as
21: * the Web service provider. See {@link CWsdlGenerator} on how to declare
22: * methods that can be remotely invoked.
23: *
24: * Note, PHP SOAP extension is required for this action.
25: *
26: * @property CWebService $service The Web service instance.
27: *
28: * @author Qiang Xue <qiang.xue@gmail.com>
29: * @package system.web.services
30: * @since 1.0
31: */
32: class CWebServiceAction extends CAction
33: {
34: /**
35: * @var mixed the Web service provider object or class name.
36: * If specified as a class name, it can be a path alias.
37: * Defaults to null, meaning the current controller is used as the service provider.
38: * If the provider implements the interface {@link IWebServiceProvider},
39: * it will be able to intercept the remote method invocation and perform
40: * additional tasks (e.g. authentication, logging).
41: */
42: public $provider;
43: /**
44: * @var string the URL for the Web service. Defaults to null, meaning
45: * the URL for this action is used to provide Web services.
46: * In this case, a GET parameter named {@link serviceVar} will be used to
47: * deteremine whether the current request is for WSDL or Web service.
48: */
49: public $serviceUrl;
50: /**
51: * @var string the URL for WSDL. Defaults to null, meaning
52: * the URL for this action is used to serve WSDL document.
53: */
54: public $wsdlUrl;
55: /**
56: * @var string the name of the GET parameter that differentiates a WSDL request
57: * from a Web service request. If this GET parameter exists, the request is considered
58: * as a Web service request; otherwise, it is a WSDL request. Defaults to 'ws'.
59: */
60: public $serviceVar='ws';
61: /**
62: * @var array a list of PHP classes that are declared as complex types in WSDL.
63: * This should be an array with WSDL types as keys and names of PHP classes as values.
64: * A PHP class can also be specified as a path alias.
65: * @see http://www.php.net/manual/en/soapclient.soapclient.php
66: */
67: public $classMap;
68: /**
69: * @var array the initial property values for the {@link CWebService} object.
70: * The array keys are property names of {@link CWebService} and the array values
71: * are the corresponding property initial values.
72: */
73: public $serviceOptions=array();
74:
75: private $_service;
76:
77:
78: /**
79: * Runs the action.
80: * If the GET parameter {@link serviceVar} exists, the action handle the remote method invocation.
81: * If not, the action will serve WSDL content;
82: */
83: public function run()
84: {
85: $hostInfo=Yii::app()->getRequest()->getHostInfo();
86: $controller=$this->getController();
87: if(($serviceUrl=$this->serviceUrl)===null)
88: $serviceUrl=$hostInfo.$controller->createUrl($this->getId(),array($this->serviceVar=>1));
89: if(($wsdlUrl=$this->wsdlUrl)===null)
90: $wsdlUrl=$hostInfo.$controller->createUrl($this->getId());
91: if(($provider=$this->provider)===null)
92: $provider=$controller;
93:
94: $this->_service=$this->createWebService($provider,$wsdlUrl,$serviceUrl);
95:
96: if(is_array($this->classMap))
97: $this->_service->classMap=$this->classMap;
98:
99: foreach($this->serviceOptions as $name=>$value)
100: $this->_service->$name=$value;
101:
102: if(isset($_GET[$this->serviceVar]))
103: $this->_service->run();
104: else
105: $this->_service->renderWsdl();
106:
107: Yii::app()->end();
108: }
109:
110: /**
111: * Returns the Web service instance currently being used.
112: * @return CWebService the Web service instance
113: */
114: public function getService()
115: {
116: return $this->_service;
117: }
118:
119: /**
120: * Creates a {@link CWebService} instance.
121: * You may override this method to customize the created instance.
122: * @param mixed $provider the web service provider class name or object
123: * @param string $wsdlUrl the URL for WSDL.
124: * @param string $serviceUrl the URL for the Web service.
125: * @return CWebService the Web service instance
126: */
127: protected function createWebService($provider,$wsdlUrl,$serviceUrl)
128: {
129: return new CWebService($provider,$wsdlUrl,$serviceUrl);
130: }
131: }