summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2015-12-04 13:26:55 -0400
committerArnold Daniels <arnold@jasny.net>2015-12-04 13:26:55 -0400
commite713ee6e31bf5941832dfb8226cb39909a97daa5 (patch)
tree82df2a5a1a0ac1a5878ac6796f98e9aee94f475c
parentc425a192ccf6fd5b4fde82bd6844efc54e74e20b (diff)
downloadsso-e713ee6e31bf5941832dfb8226cb39909a97daa5.zip
sso-e713ee6e31bf5941832dfb8226cb39909a97daa5.tar.gz
sso-e713ee6e31bf5941832dfb8226cb39909a97daa5.tar.bz2
Run arbitrary command through magic __call method in Brokerv0.1.6
By default functions are done as POST request: eg POST /sso?command=foo A function named `getFoo()` does the request GET /sso?command=foo A function named `deleteFoo()` does the request DELETE /sso?command=foo
-rw-r--r--src/Broker.php23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/Broker.php b/src/Broker.php
index df8b07c..eaeb43e 100644
--- a/src/Broker.php
+++ b/src/Broker.php
@@ -182,6 +182,7 @@ class Broker
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
if ($method === 'POST' && !empty($data)) {
$post = is_string($data) ? $data : http_build_query($data);
@@ -241,6 +242,8 @@ class Broker
/**
* Get user information.
+ *
+ * @return object|null
*/
public function getUserInfo()
{
@@ -250,6 +253,26 @@ class Broker
return $this->userinfo;
}
+
+ /**
+ * Magic method to do arbitrary request
+ *
+ * @param string $fn
+ * @param array $args
+ * @return mixed
+ */
+ public function __call($fn, $args)
+ {
+ $sentence = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $fn));
+ $parts = explode(' ', $sentence);
+
+ $method = count($parts) > 1 && in_array(strtoupper($parts[0]), ['GET', 'DELETE'])
+ ? strtoupper(array_shift($parts))
+ : 'POST';
+ $command = join('-', $parts);
+
+ return $this->request($method, $command, $args);
+ }
/**