1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\System;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2019, Afterlogic Corp.
14: *
15: * @package Api
16: */
17: class Router
18: {
19: /**
20: * @var array
21: */
22: protected $aRoutes;
23:
24: protected static $self = null;
25:
26: public function __construct()
27: {
28: $this->aRoutes = [];
29: }
30:
31: /**
32: * @return Aurora\System\Router
33: */
34: public static function getInstance()
35: {
36: if (is_null(self::$self)) {
37: self::$self = new self();
38: }
39:
40: return self::$self;
41: }
42:
43: public function register($sModule, $sName, $mCallbak)
44: {
45: if (!isset($this->aRoutes[$sName][$sModule])) {
46: $this->aRoutes[$sName][$sModule] = $mCallbak;
47: }
48: }
49:
50: public function registerArray($sModule, $aRoutes)
51: {
52: foreach ($aRoutes as $sName => $mCallbak) {
53: $this->register($sModule, $sName, $mCallbak);
54: }
55: }
56:
57: /**
58: *
59: * @param stranig $sName
60: * @return mixed
61: */
62: public function getCallback($sName)
63: {
64: $mResult = false;
65: if (isset($this->aRoutes[$sName])) {
66: $mResult = $this->aRoutes[$sName];
67: }
68:
69: return $mResult;
70: }
71:
72: public function hasCallback($mCallbak)
73: {
74: $aCallbacks = [];
75: foreach ($this->aRoutes as $sRoute => $aRoutes) {
76: foreach ($aRoutes as $sModule => $aRoute) {
77: if (!in_array($aRoute[1], $aCallbacks)) {
78: $aCallbacks[] = $aRoute[1];
79: }
80: }
81: }
82:
83: return in_array($mCallbak, $aCallbacks);
84: }
85:
86: public function hasRoute($sName)
87: {
88: return isset($this->aRoutes[$sName]);
89: }
90:
91: public function route($sName)
92: {
93: $mResult = false;
94: $oHttp = \MailSo\Base\Http::SingletonInstance();
95:
96: $mMethod = $this->getCallback($sName);
97: if ($mMethod) {
98: foreach ($mMethod as $sModule => $mCallbak) {
99: if (\Aurora\System\Api::GetModuleManager()->IsAllowedModule($sModule)) {
100: \Aurora\System\Api::Log(" ");
101: \Aurora\System\Api::Log(" ===== ENTRY: " . $sModule . '::' . $sName);
102: \Aurora\System\Api::Log(" URL: " . $oHttp->GetUrl());
103: $mResult .= call_user_func_array(
104: $mCallbak,
105: []
106: );
107: }
108: }
109: }
110:
111: return $mResult;
112: }
113:
114: public function removeRoute($sName)
115: {
116: unset($this->aRoutes[$sName]);
117: }
118:
119: /**
120: * @return array
121: */
122: public static function getItems()
123: {
124: static $aResult = null;
125: if ($aResult === null) {
126: $aResult = array();
127:
128: $oHttp = \MailSo\Base\Http::SingletonInstance();
129:
130: $sQuery = \trim(\trim(urldecode($oHttp->GetQueryString())), ' /');
131:
132: $iPos = \strpos($sQuery, '&');
133: if (0 < $iPos) {
134: $sQuery = \substr($sQuery, 0, $iPos);
135: }
136: $aQuery = \explode('/', $sQuery);
137: foreach ($aQuery as $sQueryItem) {
138: $iPos = \strpos($sQueryItem, '=');
139: $aResult[] = (!$iPos) ? $sQueryItem : \substr($sQueryItem, 0, $iPos);
140: }
141: }
142:
143: return $aResult;
144: }
145:
146: /**
147: *
148: * @param int $iIndex
149: */
150: public static function getItemByIndex($iIndex, $mDefaultValue = null)
151: {
152: $aPath = self::getItems();
153:
154: return !empty($aPath[$iIndex]) ? $aPath[$iIndex] : $mDefaultValue;
155: }
156: }
157: