-
Notifications
You must be signed in to change notification settings - Fork 3
/
Router.php
161 lines (136 loc) · 4.08 KB
/
Router.php
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* This code is licensed under AGPLv3 license or Afterlogic Software License
* if commercial version of the product was purchased.
* For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
*/
namespace Aurora\System;
/**
* @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
* @license https://afterlogic.com/products/common-licensing Afterlogic Software License
* @copyright Copyright (c) 2019, Afterlogic Corp.
*
* @package Api
*/
class Router
{
/**
* @var array
*/
protected $aRoutes;
protected static $self = null;
public function __construct()
{
$this->aRoutes = [];
}
/**
* @return \Aurora\System\Router
*/
public static function getInstance()
{
if (is_null(self::$self)) {
self::$self = new self();
}
return self::$self;
}
public function register($sModule, $sName, $mCallbak)
{
if (!isset($this->aRoutes[$sName][$sModule])) {
$this->aRoutes[$sName][$sModule] = $mCallbak;
}
}
public function registerArray($sModule, $aRoutes)
{
foreach ($aRoutes as $sName => $mCallbak) {
$this->register($sModule, $sName, $mCallbak);
}
}
/**
*
* @param string $sName
* @return mixed
*/
public function getCallback($sName)
{
$mResult = false;
if (isset($this->aRoutes[$sName])) {
$mResult = $this->aRoutes[$sName];
}
return $mResult;
}
public function hasCallback($mCallbak)
{
$aCallbacks = [];
foreach ($this->aRoutes as $sRoute => $aRoutes) {
foreach ($aRoutes as $sModule => $aRoute) {
if (!in_array($aRoute[1], $aCallbacks)) {
$aCallbacks[] = $aRoute[1];
}
}
}
return in_array($mCallbak, $aCallbacks);
}
public function hasRoute($sName)
{
return isset($this->aRoutes[$sName]);
}
public function route($sName)
{
$mResult = [];
$oHttp = \MailSo\Base\Http::SingletonInstance();
$mMethod = $this->getCallback($sName);
if ($mMethod) {
foreach ($mMethod as $sModule => $mCallbak) {
if (\Aurora\System\Api::GetModuleManager()->IsAllowedModule($sModule)) {
\Aurora\System\Api::Log(" ");
\Aurora\System\Api::Log(" ===== ENTRY: " . $sModule . '::' . $sName);
\Aurora\System\Api::Log(" URL: " . $oHttp->GetUrl());
$mResult[] = call_user_func_array(
$mCallbak,
[]
);
}
}
}
// This is an unsupported case. Work with multiple callbacks of one an entry point was kept for backward compatibility.
if (count($mResult) > 1) {
\Aurora\System\Api::Log(" WARNING: More than one entry result returned");
}
return $mResult[0] ?? false;
}
public function removeRoute($sName)
{
unset($this->aRoutes[$sName]);
}
/**
* @return array
*/
public static function getItems()
{
static $aResult = null;
if ($aResult === null) {
$aResult = array();
$oHttp = \MailSo\Base\Http::SingletonInstance();
$sQuery = \trim(\trim(urldecode($oHttp->GetQueryString())), ' /');
$iPos = \strpos($sQuery, '&');
if (0 < $iPos) {
$sQuery = \substr($sQuery, 0, $iPos);
}
$aQuery = \explode('/', $sQuery);
foreach ($aQuery as $sQueryItem) {
$iPos = \strpos($sQueryItem, '=');
$aResult[] = (!$iPos) ? $sQueryItem : \substr($sQueryItem, 0, $iPos);
}
}
return $aResult;
}
/**
*
* @param int $iIndex
*/
public static function getItemByIndex($iIndex, $mDefaultValue = null)
{
$aPath = self::getItems();
return !empty($aPath[$iIndex]) ? $aPath[$iIndex] : $mDefaultValue;
}
}