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_Config extends Varien_Simplexml_Config
35: {
36: const CACHE_TAG = 'config_api';
37:
38: 39: 40: 41: 42:
43: public function __construct($sourceData=null)
44: {
45: $this->setCacheId('config_api');
46: $this->setCacheTags(array(self::CACHE_TAG));
47:
48: parent::__construct($sourceData);
49: $this->_construct();
50: }
51:
52: 53: 54: 55: 56:
57: protected function _construct()
58: {
59: if (Mage::app()->useCache('config_api')) {
60: if ($this->loadCache()) {
61: return $this;
62: }
63: }
64:
65: $config = Mage::getConfig()->loadModulesConfiguration('api.xml');
66: $this->setXml($config->getNode('api'));
67:
68: if (Mage::app()->useCache('config_api')) {
69: $this->saveCache();
70: }
71: return $this;
72: }
73:
74: 75: 76: 77: 78:
79: public function getAdapterAliases()
80: {
81: $aliases = array();
82:
83: foreach ($this->getNode('adapter_aliases')->children() as $alias => $adapter) {
84: $aliases[$alias] = array(
85: (string) $adapter->suggest_class,
86: (string) $adapter->suggest_method
87: );
88: }
89: return $aliases;
90: }
91:
92: 93: 94: 95: 96:
97: public function getAdapters()
98: {
99: $adapters = array();
100: foreach ($this->getNode('adapters')->children() as $adapterName => $adapter) {
101:
102: if (isset($adapter->use)) {
103: $adapter = $this->getNode('adapters/' . (string) $adapter->use);
104: }
105: $adapters[$adapterName] = $adapter;
106: }
107: return $adapters;
108: }
109:
110: 111: 112: 113: 114:
115: public function getActiveAdapters()
116: {
117: $adapters = array();
118: foreach ($this->getAdapters() as $adapterName => $adapter) {
119: if (!isset($adapter->active) || $adapter->active == '0') {
120: continue;
121: }
122:
123: if (isset($adapter->required) && isset($adapter->required->extensions)) {
124: foreach ($adapter->required->extensions->children() as $extension=>$data) {
125: if (!extension_loaded($extension)) {
126: continue;
127: }
128: }
129: }
130:
131: $adapters[$adapterName] = $adapter;
132: }
133:
134: return $adapters;
135: }
136:
137: 138: 139: 140: 141:
142: public function getHandlers()
143: {
144: return $this->getNode('handlers')->children();
145: }
146:
147: 148: 149: 150: 151:
152: public function getResources()
153: {
154: return $this->getNode('resources')->children();
155: }
156:
157: 158: 159: 160: 161:
162: public function getResourcesAlias()
163: {
164: return $this->getNode('resources_alias')->children();
165: }
166:
167:
168: 169: 170: 171: 172: 173: 174: 175:
176: public function loadAclResources(Mage_Api_Model_Acl $acl, $resource=null, $parentName=null)
177: {
178: $resourceName = null;
179: if (is_null($resource)) {
180: $resource = $this->getNode('acl/resources');
181: } else {
182: $resourceName = (is_null($parentName) ? '' : $parentName.'/').$resource->getName();
183: $acl->add(Mage::getModel('api/acl_resource', $resourceName), $parentName);
184: }
185:
186: $children = $resource->children();
187:
188: if (empty($children)) {
189: return $this;
190: }
191:
192: foreach ($children as $res) {
193: if ($res->getName() != 'title' && $res->getName() != 'sort_order') {
194: $this->loadAclResources($acl, $res, $resourceName);
195: }
196: }
197: return $this;
198: }
199:
200: 201: 202: 203: 204: 205:
206: public function getAclAssert($name='')
207: {
208: $asserts = $this->getNode('acl/asserts');
209: if (''===$name) {
210: return $asserts;
211: }
212:
213: if (isset($asserts->$name)) {
214: return $asserts->$name;
215: }
216:
217: return false;
218: }
219:
220: 221: 222: 223: 224: 225:
226: public function getAclPrivilegeSet($name='')
227: {
228: $sets = $this->getNode('acl/privilegeSets');
229: if (''===$name) {
230: return $sets;
231: }
232:
233: if (isset($sets->$name)) {
234: return $sets->$name;
235: }
236:
237: return false;
238: }
239:
240: public function getFaults($resourceName=null)
241: {
242: if (is_null($resourceName)
243: || !isset($this->getResources()->$resourceName)
244: || !isset($this->getResources()->$resourceName->faults)) {
245: $faultsNode = $this->getNode('faults');
246: } else {
247: $faultsNode = $this->getResources()->$resourceName->faults;
248: }
249:
250:
251: $translateModule = 'api';
252: if (isset($faultsNode['module'])) {
253: $translateModule = (string) $faultsNode['module'];
254: }
255:
256: $faults = array();
257: foreach ($faultsNode->children() as $faultName => $fault) {
258: $faults[$faultName] = array(
259: 'code' => (string) $fault->code,
260: 'message' => Mage::helper($translateModule)->__((string)$fault->message)
261: );
262: }
263:
264: return $faults;
265: }
266:
267: 268: 269: 270: 271:
272: public function getCache()
273: {
274: return Mage::app()->getCache();
275: }
276:
277: protected function _loadCache($id)
278: {
279: return Mage::app()->loadCache($id);
280: }
281:
282: protected function _saveCache($data, $id, $tags=array(), $lifetime=false)
283: {
284: return Mage::app()->saveCache($data, $id, $tags, $lifetime);
285: }
286:
287: protected function _removeCache($id)
288: {
289: return Mage::app()->removeCache($id);
290: }
291: }
292: