This repository was archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathEntity.php
More file actions
298 lines (264 loc) · 8.71 KB
/
Copy pathEntity.php
File metadata and controls
298 lines (264 loc) · 8.71 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* Copyright 2013 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* Represents an entity accessible through Splunk's REST API.
*
* @package Splunk
*/
class Splunk_Entity extends Splunk_Endpoint implements ArrayAccess
{
private $loaded = FALSE;
private $entry;
private $namespace;
private $content;
/**
* @internal
*
* @param Splunk_Service $service
* @param string $path
* @param SimpleXMLElement|NULL $entry
* (optional) The <entry> for this
* entity, as received from the REST API.
* If omitted, will be loaded on demand.
* @param Splunk_Namespace|NULL $namespace
* (optional) The namespace from which to
* load this entity, or NULL to use the
* $service object's default namespace.
* Does not apply if this entity is
* already loaded (i.e. if $entry is not
* NULL).
*/
public function __construct($service, $path, $entry=NULL, $namespace=NULL)
{
parent::__construct($service, $path);
$this->entry = $entry;
if ($this->entry != NULL)
{
if ($namespace !== NULL)
throw new InvalidArgumentException(
'Cannot specify an entry and a namespace simultaneously. ' .
'The namespace will be inferred from the entry.');
$this->parseContentsFromEntry();
$this->namespace = $this->getNamespace(); // extract from 'eai:acl'
}
else
{
$this->namespace = $namespace;
}
}
// === Load ===
/**
* Loads this resource if not already done. Returns self.
*
* @return Splunk_Entity This entity.
* @throws Splunk_IOException
*/
protected function validate($fetchArgs=array())
{
if (!$this->loaded)
{
$this->load($fetchArgs);
assert($this->loaded);
}
return $this;
}
/**
* Loads this resource.
*
* @throws Splunk_IOException
*/
private function load($fetchArgs)
{
$response = $this->fetch($fetchArgs);
$xml = new SimpleXMLElement($response->body);
$this->entry = $this->extractEntryFromRootXmlElement($xml);
$this->parseContentsFromEntry();
}
/**
* Fetches this entity's Atom feed from the Splunk server.
*
* @throws Splunk_IOException
*/
protected function fetch($fetchArgs)
{
return $this->sendGet('');
}
/** Returns the <entry> element inside the root element. */
protected function extractEntryFromRootXmlElement($xml)
{
if (!Splunk_XmlUtil::isSingleElement($xml->entry))
{
// Extract name from path since we can't extract it from the
// entity content here.
$pathComponents = explode('/', $this->path);
$name = $pathComponents[count($pathComponents) - 1];
throw new Splunk_AmbiguousEntityNameException($name);
}
return $xml->entry;
}
/** Parses the entry's contents. */
private function parseContentsFromEntry()
{
$this->content = Splunk_AtomFeed::parseValueInside($this->entry->content);
$this->loaded = TRUE;
}
/** Returns a value that indicates whether the entity has been loaded. */
protected function isLoaded()
{
return $this->loaded;
}
/**
* Refreshes this entity's properties from the Splunk server.
*
* @return Splunk_Entity This entity.
* @throws Splunk_IOException
*/
public function refresh()
{
if ($this->loaded)
{
// Remember this entity's exact namespace, so that a reload
// will occur in the correct namespace.
$this->namespace = $this->getNamespace();
}
$this->loaded = FALSE;
return $this->validate();
}
// === Accessors ===
/**
* Gets an array that contains the properties of this entity.
*
* @return array The properties of this entity.
*/
public function getContent()
{
return $this->validate()->content;
}
/**
* Gets the name of this entity.
*
* @return string The name of this entity.
* This name can be used to lookup this entity
* from its collection.
*/
public function getName()
{
return $this->getTitle();
}
/**
* Gets the title of this entity in the REST API.
*
* @return string The title of this entity in the REST API.
*/
protected function getTitle()
{
return (string) $this->validate()->entry->title;
}
/** Gets the namespace in which this entity resides. */
protected function getSearchNamespace()
{
return $this->namespace;
}
/**
* Gets the non-wildcarded namespace in which this entity resides.
*
* @return Splunk_Namespace The non-wildcarded namespace in which this
* entity resides.
*/
public function getNamespace()
{
// If this is an entity reference with an exact namespace, return it.
if (!$this->loaded)
{
$effectiveNamespace = $this->namespace;
if ($effectiveNamespace === NULL)
$effectiveNamespace = $this->service->getNamespace();
if ($effectiveNamespace->isExact())
return $effectiveNamespace;
}
// Extract the namespace from this entity's content.
$acl = $this['eai:acl'];
return Splunk_Namespace::createExact(
$acl['owner'], $acl['app'], $acl['sharing']);
}
// === ArrayAccess Methods ===
/**
* Gets the value of the specified entity property.
*
* @param string $key The name of an entity property.
* @return string The value of the specified entity property.
*/
public function offsetGet($key)
{
return $this->validate()->content[$key];
}
/** @internal */
public function offsetSet($key, $value)
{
throw new Splunk_UnsupportedOperationException();
}
/** @internal */
public function offsetUnset($key)
{
throw new Splunk_UnsupportedOperationException();
}
/**
* Gets a value that indicates whether the specified entity property exists.
*
* @param string $key The name of an entity property.
* @return string Whether the specified entity property exists.
*/
public function offsetExists($key)
{
return isset($this->validate()->content[$key]);
}
// === Operations ===
/**
* Deletes this entity.
*
* @throws Splunk_IOException
*/
public function delete()
{
$this->sendDelete('');
}
/**
* Updates this entity's properties.
*
* Note that the "name" property cannot be updated.
*
* @param array $args Dictionary of properties that will be changed,
* along with their new values.
* @return Splunk_Entity This entity.
* @throws Splunk_IOException
*/
public function update($args)
{
if (array_key_exists('name', $args))
throw new InvalidArgumentException(
'Cannot update the name of an entity.');
if (array_key_exists('namespace', $args))
throw new InvalidArgumentException(
'Cannot override the entity\'s namespace.');
// Update entity on server
$this->sendPost('', $args);
// Update cached content of entity
if ($this->loaded)
$this->content = array_merge($this->content, $args);
return $this;
}
}