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_Api2_Model_Config extends Varien_Simplexml_Config
35: {
36: 37: 38:
39: const NODE_RESOURCE_GROUPS = 'resource_groups';
40:
41: 42: 43:
44: const CACHE_ID = 'config_api2';
45:
46: 47: 48:
49: const CACHE_TAG = 'CONFIG_API2';
50:
51: 52: 53: 54: 55:
56: protected $_resourcesGrouped = false;
57:
58: 59: 60: 61: 62: 63: 64:
65: public function __construct($sourceData = null)
66: {
67: parent::__construct($sourceData);
68:
69: $canUserCache = Mage::app()->useCache('config');
70: if ($canUserCache) {
71: $this->setCacheId(self::CACHE_ID)
72: ->setCacheTags(array(self::CACHE_TAG))
73: ->setCacheChecksum(null)
74: ->setCache(Mage::app()->getCache());
75:
76: if ($this->loadCache()) {
77: return;
78: }
79: }
80:
81:
82: $config = Mage::getConfig()->loadModulesConfiguration('api2.xml');
83: $this->setXml($config->getNode('api2'));
84:
85: if ($canUserCache) {
86: $this->saveCache();
87: }
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: public function getRoutes($apiType)
98: {
99:
100: $helper = Mage::helper('api2');
101: if (!$helper->isApiTypeSupported($apiType)) {
102: throw new Mage_Api2_Exception(sprintf('API type "%s" is not supported', $apiType),
103: Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
104: }
105:
106: $routes = array();
107: foreach ($this->getResources() as $resourceKey => $resource) {
108: if (!$resource->routes) {
109: continue;
110: }
111:
112:
113: foreach ($resource->routes->children() as $route) {
114: $arguments = array(
115: Mage_Api2_Model_Route_Abstract::PARAM_ROUTE => (string)$route->route,
116: Mage_Api2_Model_Route_Abstract::PARAM_DEFAULTS => array(
117: 'model' => (string)$resource->model,
118: 'type' => (string)$resourceKey,
119: 'action_type' => (string)$route->action_type
120: )
121: );
122:
123: $routes[] = Mage::getModel('api2/route_' . $apiType, $arguments);
124: }
125: }
126: return $routes;
127: }
128:
129: 130: 131: 132: 133:
134: public function getResources()
135: {
136: return $this->getNode('resources')->children();
137: }
138:
139: 140: 141: 142: 143:
144: public function getResourcesTypes()
145: {
146: $list = array();
147:
148: foreach ($this->getResources() as $resourceType => $resourceCfg) {
149: $list[] = (string) $resourceType;
150: }
151: return $list;
152: }
153:
154: 155: 156: 157: 158:
159: public function getResourceGroups()
160: {
161: $groups = $this->getXpath('//' . self::NODE_RESOURCE_GROUPS);
162: if (!$groups) {
163: return false;
164: }
165:
166:
167: $groups = $groups[0];
168:
169: if (!$this->_resourcesGrouped) {
170:
171: foreach ($this->getResources() as $node) {
172: $result = $node->xpath('group');
173: if (!$result) {
174: continue;
175: }
176: $groupName = (string) $result[0];
177: if ($groupName) {
178: $result = $groups->xpath('.//' . $groupName);
179: if (!$result) {
180: continue;
181: }
182:
183:
184: $group = $result[0];
185:
186: if (!isset($group->children)) {
187: $children = new Varien_Simplexml_Element('<children />');
188: } else {
189: $children = $group->children;
190: }
191: $node->resource = 1;
192: $children->appendChild($node);
193: $group->appendChild($children);
194: }
195: }
196: }
197: return $groups;
198: }
199:
200: 201: 202: 203: 204: 205:
206: public function getResourceGroup($name)
207: {
208: $group = $this->getResourceGroups()->xpath('.//' . $name);
209: if (!$group) {
210: return false;
211: }
212: return $group[0];
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function getResource($node)
222: {
223: return $this->getNode('resources/' . $node);
224: }
225:
226: 227: 228: 229: 230: 231:
232: public function getResourceAttributes($node)
233: {
234: $attributes = $this->getNode('resources/' . $node . '/attributes');
235: return $attributes ? $attributes->asCanonicalArray() : array();
236: }
237:
238: 239: 240: 241: 242: 243: 244: 245:
246: public function getResourceExcludedAttributes($resource, $userType, $operation)
247: {
248: $node = $this->getNode('resources/' . $resource . '/exclude_attributes/' . $userType . '/' . $operation);
249: $exclAttributes = array();
250:
251: if ($node) {
252: foreach ($node->children() as $attribute => $status) {
253: if ((string) $status) {
254: $exclAttributes[] = $attribute;
255: }
256: }
257: }
258: return $exclAttributes;
259: }
260:
261: 262: 263: 264: 265: 266: 267:
268: public function getResourceForcedAttributes($resource, $userType)
269: {
270: $node = $this->getNode('resources/' . $resource . '/force_attributes/' . $userType);
271: $forcedAttributes = array();
272:
273: if ($node) {
274: foreach ($node->children() as $attribute => $status) {
275: if ((string) $status) {
276: $forcedAttributes[] = $attribute;
277: }
278: }
279: }
280: return $forcedAttributes;
281: }
282:
283: 284: 285: 286: 287: 288: 289: 290:
291: public function getResourceIncludedAttributes($resource, $userType, $operationType)
292: {
293: $node = $this->getNode('resources/' . $resource . '/include_attributes/' . $userType . '/' . $operationType);
294: $inclAttributes = array();
295:
296: if ($node) {
297: foreach ($node->children() as $attribute => $status) {
298: if ((string) $status) {
299: $inclAttributes[] = $attribute;
300: }
301: }
302: }
303: return $inclAttributes;
304: }
305:
306: 307: 308: 309: 310: 311: 312: 313:
314: public function getResourceEntityOnlyAttributes($resource, $userType, $operationType)
315: {
316: $node = $this->getNode('resources/' . $resource . '/entity_only_attributes/' . $userType . '/' .
317: $operationType);
318: $entityOnlyAttributes = array();
319:
320: if ($node) {
321: foreach ($node->children() as $attribute => $status) {
322: if ((string) $status) {
323: $entityOnlyAttributes[] = $attribute;
324: }
325: }
326: }
327: return $entityOnlyAttributes;
328: }
329:
330: 331: 332: 333: 334: 335:
336: public function getResourceWorkingModel($node)
337: {
338: return (string)$this->getNode('resources/' . $node . '/working_model');
339: }
340:
341: 342: 343: 344: 345: 346: 347:
348: public function getVersions($node)
349: {
350: $element = $this->getNode('resources/' . $node . '/versions');
351: if (!$element) {
352: throw new Exception(
353: sprintf('Resource "%s" does not have node <versions> in config.', htmlspecialchars($node))
354: );
355: }
356:
357: $versions = explode(',', (string)$element);
358: if (count(array_filter($versions, 'is_numeric')) != count($versions)) {
359: throw new Exception(sprintf('Invalid resource "%s" versions in config.', htmlspecialchars($node)));
360: }
361:
362: rsort($versions, SORT_NUMERIC);
363:
364: return $versions;
365: }
366:
367: 368: 369: 370: 371: 372:
373: public function getResourceModel($node)
374: {
375: return (string)$this->getNode('resources/' . $node . '/model');
376: }
377:
378: 379: 380: 381: 382: 383: 384:
385: public function getResourceUserPrivileges($resource, $userType)
386: {
387: $attributes = $this->getNode('resources/' . $resource . '/privileges/' . $userType);
388: return $attributes ? $attributes->asCanonicalArray() : array();
389: }
390:
391: 392: 393: 394: 395: 396:
397: public function getResourceSubresources($node)
398: {
399: $subresources = $this->getNode('resources/' . $node . '/subresources');
400: return $subresources ? $subresources->asCanonicalArray() : array();
401: }
402:
403: 404: 405: 406: 407: 408: 409:
410: public function getValidationConfig($resourceType, $validatorType)
411: {
412: $config = $this->getNode('resources/' . $resourceType . '/validators/' . $validatorType);
413: return $config ? $config->asCanonicalArray() : array();
414: }
415:
416: 417: 418: 419: 420: 421: 422:
423: public function getResourceLastVersion($resourceType, $lowerOrEqualsTo = null)
424: {
425: $availVersions = $this->getVersions($resourceType);
426: $useVersion = reset($availVersions);
427:
428: if (null !== $lowerOrEqualsTo) {
429: foreach ($availVersions as $availVersion) {
430: if ($availVersion <= $lowerOrEqualsTo) {
431: $useVersion = $availVersion;
432: break;
433: }
434: }
435: }
436: return (int)$useVersion;
437: }
438:
439: 440: 441: 442: 443: 444:
445: public function getRouteWithEntityTypeAction($node)
446: {
447: return (string)$this->getNode('resources/' . $node . '/routes/route_entity/route');
448: }
449: }
450: