-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientManager.php
More file actions
168 lines (145 loc) · 3.35 KB
/
ClientManager.php
File metadata and controls
168 lines (145 loc) · 3.35 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
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
162
163
164
165
166
167
168
<?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;
/**
* Manager for application client types
*/
class ClientManager
{
/**
* Client information array
*
* @var array
*/
protected static $_clients = null;
/**
* Gets information on a specific client id. This method will be useful in
* future versions when we start mapping applications in the database.
*
* This method will return a client information array if called
* with no arguments which can be used to add custom application information.
*
* @param integer $id A client identifier
* @param boolean $byName If True, find the client by its name
* @return mixed Object describing the client or false if not known
*/
public static function client($id = null, $byName = false)
{
// Only create the array if it does not exist
if (self::$_clients === null)
{
self::$_clients = array();
include_once __DIR__ . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientInterface.php';
$dirIterator = new \DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'Client');
foreach ($dirIterator as $file)
{
if ($file->isDot() || $file->isDir())
{
continue;
}
$client = preg_replace('#\.[^.]*$#', '', $file->getFilename());
if ($client == 'ClientInterface')
{
continue;
}
$cls = __NAMESPACE__ . '\\Client\\' . ucfirst(strtolower($client));
if (!class_exists($cls))
{
include_once $file->getPathname();
if (!class_exists($cls))
{
throw new \InvalidArgumentException(sprintf('Invalid client type of "%s".', $client));
}
}
$obj = new $cls;
self::$_clients[$obj->id] = $obj;
}
ksort(self::$_clients);
}
// If no client id has been passed return the whole array
if (is_null($id))
{
return self::all();
}
// Are we looking for client information by id or by name?
if (!$byName)
{
if (isset(self::$_clients[$id]))
{
return self::$_clients[$id];
}
}
else
{
foreach (self::$_clients as $client)
{
if ($client->name == strtolower($id))
{
return $client;
}
}
}
return null;
}
/**
* Modify information on a client.
*
* @param integer $client A client identifier
* @param string $property Property to set
* @param mixed $value Value to set
* @return void
*/
public static function modify($client, $property, $value)
{
if ($cl = self::client($client))
{
$cl->$property = $value;
}
}
/**
* Adds information for a client.
*
* @param mixed $client A client identifier either an array or object
* @return boolean True if the information is added. False on error
*/
public static function append($client)
{
if (is_array($client))
{
$client = (object) $client;
}
if (!is_object($client))
{
return false;
}
$info = self::client();
if (!isset($client->id))
{
$client->id = count($info);
}
self::$_clients[$client->id] = clone $client;
return true;
}
/**
* Get all client data
*
* @return mixed
*/
public static function all()
{
return self::$_clients;
}
/**
* Reset the client list
*
* @return void
*/
public static function reset()
{
self::$_clients = null;
}
}