-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientDetector.php
More file actions
116 lines (102 loc) · 2.4 KB
/
ClientDetector.php
File metadata and controls
116 lines (102 loc) · 2.4 KB
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
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Base;
use Hubzero\Http\Request;
/**
* Client detector
*
* Inspired by Laravel's environment detector
* http://laravel.com
*/
class ClientDetector
{
/**
* Request URI
*/
private $request = null;
/**
* Create a new application instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Detect the application's current client.
*
* @param array $environments
* @return object
*/
public function detect($environments)
{
if ($this->detectConsoleClient($environments))
{
return ClientManager::client('cli', true);
}
return $this->detectWebClient($environments);
}
/**
* Determine client for a web request.
*
* @param array $environments
* @return object
*/
protected function detectWebClient($environments)
{
$default = ClientManager::client('site', true);
// To determine the current client, we'll simply iterate through the possible
// clients and look for the one that matches the path for the request we
// are currently processing here, then return back that client.
foreach ($environments as $environment => $url)
{
if ($client = ClientManager::client($environment, true))
{
if ($client->name == 'cli')
{
continue;
}
// Legacy check based on file path
// Ex: JPATH_API would be set from ROOT/api/index.php
// @TODO: Remove need for this code
$const = 'JPATH_' . strtoupper($environment);
if (defined($const)
&& defined('JPATH_BASE')
&& JPATH_BASE == constant($const))
{
return $client;
}
if (defined($const)
&& !defined(JPATH_BASE)
&& JPATH_ROOT . $this->request->base(true) == constant($const))
{
return $client;
}
// Check based on request path
// Ex: http://somehub.org/api
if ($this->request->segment(1) == $url
|| $this->request->segment(1) == $client->name
|| $this->request->segment(1) == $client->url)
{
return $client;
}
}
}
return $default;
}
/**
* Determine if the client is command-line
*
* @param array $environments
* @return bool
*/
protected function detectConsoleClient($environments)
{
return (php_sapi_name() == 'cli');
}
}