1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Api_Model_Server_WSI_Handler extends Mage_Api_Model_Server_Handler_Abstract
35: {
36: protected $_resourceSuffix = '_v2';
37:
38: 39: 40: 41: 42: 43:
44:
45: public function __call ($function, $args)
46: {
47: $args = $args[0];
48:
49:
50: $helper = Mage::helper('api/data');
51:
52: $helper->wsiArrayUnpacker($args);
53: $args = get_object_vars($args);
54:
55: if(isset($args['sessionId'])){
56: $sessionId = $args['sessionId'];
57: unset($args['sessionId']);
58: } else {
59:
60: $sessionId = array_shift( $args );
61: }
62:
63: $apiKey = '';
64: $nodes = Mage::getSingleton('api/config')->getNode('v2/resources_function_prefix')->children();
65: foreach ($nodes as $resource => $prefix) {
66: $prefix = $prefix->asArray();
67: if (false !== strpos($function, $prefix)) {
68: $method = substr($function, strlen($prefix));
69: $apiKey = $resource . '.' . strtolower($method[0]).substr($method, 1);
70: }
71: }
72:
73: list($modelName, $methodName) = $this->_getResourceName($apiKey);
74: $methodParams = $this->getMethodParams($modelName, $methodName);
75:
76: $args = $this->prepareArgs($methodParams, $args);
77:
78: $res = $this->call($sessionId, $apiKey, $args);
79:
80: $obj = $helper->wsiArrayPacker($res);
81: $stdObj = new stdClass();
82: $stdObj->result = $obj;
83:
84: return $stdObj;
85: }
86:
87: 88: 89: 90: 91: 92: 93:
94: public function login($username, $apiKey = null)
95: {
96: if (is_object($username)) {
97: $apiKey = $username->apiKey;
98: $username = $username->username;
99: }
100:
101: $stdObject = new stdClass();
102: $stdObject->result = parent::login($username, $apiKey);
103: return $stdObject;
104: }
105:
106: 107: 108: 109: 110: 111:
112: protected function _getResourceName($apiPath){
113:
114: list($resourceName, $methodName) = explode('.', $apiPath);
115:
116: if (empty($resourceName) || empty($methodName)) {
117: return $this->_fault('resource_path_invalid');
118: }
119:
120: $resourcesAlias = $this->_getConfig()->getResourcesAlias();
121: $resources = $this->_getConfig()->getResources();
122: if (isset($resourcesAlias->$resourceName)) {
123: $resourceName = (string) $resourcesAlias->$resourceName;
124: }
125:
126: $methodInfo = $resources->$resourceName->methods->$methodName;
127:
128: $modelName = $this->_prepareResourceModelName((string) $resources->$resourceName->model);
129:
130: $modelClass = Mage::getConfig()->getModelClassName($modelName);
131:
132: $method = (isset($methodInfo->method) ? (string) $methodInfo->method : $methodName);
133:
134: return array($modelClass, $method);
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function getMethodParams($modelName, $methodName) {
145:
146: $method = new ReflectionMethod($modelName, $methodName);
147:
148: return $method->getParameters();
149: }
150:
151: 152: 153: 154: 155: 156: 157:
158: public function prepareArgs($params, $args) {
159:
160: $callArgs = array();
161:
162:
163: foreach($params AS $parameter){
164: $pName = $parameter->getName();
165: if( isset( $args[$pName] ) ){
166: $callArgs[$pName] = $args[$pName];
167: } else {
168: if($parameter->isOptional()){
169: $callArgs[$pName] = $parameter->getDefaultValue();
170: } else {
171: Mage::logException(new Exception("Required parameter \"$pName\" is missing.", 0));
172: $this->_fault('invalid_request_param');
173: }
174: }
175: }
176: return $callArgs;
177: }
178:
179: }
180: