1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Api2
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Webservice api2 router model
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Model_Router
35: {
36: /**
37: * Routes which are stored in module config files api2.xml
38: *
39: * @var array
40: */
41: protected $_routes = array();
42:
43: /**
44: * Set routes
45: *
46: * @param array $routes
47: * @return Mage_Api2_Model_Router
48: */
49: public function setRoutes(array $routes)
50: {
51: $this->_routes = $routes;
52:
53: return $this;
54: }
55:
56: /**
57: * Get routes
58: *
59: * @return array
60: */
61: public function getRoutes()
62: {
63: return $this->_routes;
64: }
65:
66: /**
67: * Route the Request, the only responsibility of the class
68: * Find route that match current URL, set parameters of the route to Request object
69: *
70: * @param Mage_Api2_Model_Request $request
71: * @return Mage_Api2_Model_Request
72: * @throws Mage_Api2_Exception
73: */
74: public function route(Mage_Api2_Model_Request $request)
75: {
76: $isMatched = false;
77:
78: /** @var $route Mage_Api2_Model_Route_Interface */
79: foreach ($this->getRoutes() as $route) {
80: if ($params = $route->match($request)) {
81: $request->setParams($params);
82: $isMatched = true;
83: break;
84: }
85: }
86: if (!$isMatched) {
87: throw new Mage_Api2_Exception('Request does not match any route.', Mage_Api2_Model_Server::HTTP_NOT_FOUND);
88: }
89: if (!$request->getResourceType() || !$request->getModel()) {
90: throw new Mage_Api2_Exception('Matched resource is not properly set.',
91: Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
92: }
93: return $request;
94: }
95:
96: /**
97: * Set API type to request as a result of one pass route
98: *
99: * @param Mage_Api2_Model_Request $request
100: * @param boolean $trimApiTypePath OPTIONAL If TRUE - /api/:api_type part of request path info will be trimmed
101: * @return Mage_Api2_Model_Router
102: * @throws Mage_Api2_Exception
103: */
104: public function routeApiType(Mage_Api2_Model_Request $request, $trimApiTypePath = true)
105: {
106: /** @var $apiTypeRoute Mage_Api2_Model_Route_ApiType */
107: $apiTypeRoute = Mage::getModel('api2/route_apiType');
108:
109: if (!($apiTypeMatch = $apiTypeRoute->match($request, true))) {
110: throw new Mage_Api2_Exception('Request does not match type route.', Mage_Api2_Model_Server::HTTP_NOT_FOUND);
111: }
112: // Trim matched URI path for next routes
113: if ($trimApiTypePath) {
114: $matchedPathLength = strlen('/' . ltrim($apiTypeRoute->getMatchedPath(), '/'));
115:
116: $request->setPathInfo(substr($request->getPathInfo(), $matchedPathLength));
117: }
118: $request->setParam('api_type', $apiTypeMatch['api_type']);
119:
120: return $this;
121: }
122: }
123: