You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by ro...@apache.org on 2014/11/12 02:15:14 UTC
[09/45] incubator-usergrid git commit: First commit to ApacheUsergrid
for new PHP5 sdks.
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/GuzzleClient.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/GuzzleClient.php b/sdks/php5/apache-usergrid/src/Api/GuzzleClient.php
new file mode 100644
index 0000000..6ef9d8a
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/GuzzleClient.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api;
+
+
+use Guzzle\Service\Client;
+
+/**
+ * Class GuzzleClient
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class GuzzleClient extends Client
+{
+
+ /**
+ * The Usergrid API client instance.
+ *
+ * @var \Apache\Usergrid\Api\Usergrid
+ */
+ protected $apiClient;
+
+ /**
+ * Returns the Usergrid API client instance.
+ *
+ * @return \Apache\Usergrid\Api\Usergrid
+ */
+ public function getApiClient()
+ {
+ return $this->apiClient;
+ }
+
+ /**
+ * Sets the Usergrid API client instance.
+ *
+ * @param \Apache\Usergrid\Api\Usergrid $client
+ * @return void
+ */
+ public function setApiClient(Usergrid $client)
+ {
+ $this->apiClient = $client;
+ }
+
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/Application.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/Application.php b/sdks/php5/apache-usergrid/src/Api/Models/Application.php
new file mode 100644
index 0000000..336d6d9
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/Application.php
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Guzzle\Service\Command\ResponseClassInterface;
+
+/**
+ * Class Application
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Application extends BaseCollection implements ResponseClassInterface
+{
+ use GuzzleCommandTrait;
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/BaseCollection.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/BaseCollection.php b/sdks/php5/apache-usergrid/src/Api/Models/BaseCollection.php
new file mode 100644
index 0000000..e0ee801
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/BaseCollection.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Apache\Usergrid\Api\Usergrid;
+use Illuminate\Support\Collection;
+
+/**
+ * Class BaseCollection
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class BaseCollection extends Collection
+{
+ /**
+ * List of API response properties that'll be
+ * automatically converted into collections.
+ *
+ * @var array
+ */
+ protected $collections = [];
+
+ /**
+ * The Usergrid API client instance.
+ *
+ * @var /Apache\Usergrid\Api\Usergrid
+ */
+ protected $apiClient;
+
+ /**
+ * Returns the Usergrid API client instance.
+ *
+ * @return /Apache\Usergrid\Api\Usergrid /Apache\Usergrid\Api\Usergrid
+ */
+ public function getApiClient()
+ {
+ return $this->apiClient;
+ }
+
+ /**
+ * Sets the Usergrid API client instance.
+ *
+ * @param Usergrid $client
+ * @internal param $ /Apache\Usergrid\Api\Usergrid $client
+ * @return void
+ */
+ public function setApiClient(Usergrid $client)
+ {
+ $this->apiClient = $client;
+ }
+
+ /**
+ * Returns the given key value from the collection.
+ *
+ * @param mixed $key
+ * @return mixed
+ */
+ public function __get($key)
+ {
+ if (in_array($key, $this->collections) || array_key_exists($key, $this->collections)) {
+ if ($mappedKey = array_get($this->collections, $key, [])) {
+ $key = strstr($mappedKey, '.', true);
+
+ $query = ltrim(strstr($mappedKey, '.'), '.');
+
+ $data = array_get($this->get($key), $query, []);
+ } else {
+ $data = $this->get($key, []);
+ }
+
+ return new Collection($data);
+ }
+
+ if (method_exists($this, $method = "{$key}Attribute")) {
+ return $this->{$method}($this->get($key));
+ }
+
+ return $this->get($key, null);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/Collection.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/Collection.php b/sdks/php5/apache-usergrid/src/Api/Models/Collection.php
new file mode 100644
index 0000000..9fb6d5b
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/Collection.php
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Guzzle\Service\Command\ResponseClassInterface;
+
+/**
+ * Class Collection
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Collection extends BaseCollection implements ResponseClassInterface
+{
+ use GuzzleCommandTrait;
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/Entity.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/Entity.php b/sdks/php5/apache-usergrid/src/Api/Models/Entity.php
new file mode 100644
index 0000000..5063fd0
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/Entity.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Guzzle\Service\Command\ResponseClassInterface;
+
+/**
+ * Class Entity
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Entity extends BaseCollection implements ResponseClassInterface
+{
+ use GuzzleCommandTrait;
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/Event.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/Event.php b/sdks/php5/apache-usergrid/src/Api/Models/Event.php
new file mode 100644
index 0000000..2e04595
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/Event.php
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Guzzle\Service\Command\ResponseClassInterface;
+
+/**
+ * Class Event
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Event extends BaseCollection implements ResponseClassInterface
+{
+ use GuzzleCommandTrait;
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/GuzzleCommandTrait.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/GuzzleCommandTrait.php b/sdks/php5/apache-usergrid/src/Api/Models/GuzzleCommandTrait.php
new file mode 100644
index 0000000..f8bd6fa
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/GuzzleCommandTrait.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+use Guzzle\Service\Command\OperationCommand;
+
+/**
+ * Class GuzzleCommandTrait
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+trait GuzzleCommandTrait
+{
+ /**
+ * Create a response model object from a completed command.
+ *
+ * @param OperationCommand $command That serialized the request
+ * @return \Illuminate\Support\Collection
+ */
+ public static function fromCommand(OperationCommand $command)
+ {
+ // Initialize the collection
+ $collection = new self($command->getResponse()->json());
+
+ // Set the Usergrid API client on the collection
+ $collection->setApiClient($command->getClient()->getApiClient());
+
+ // Return the collection
+ return $collection;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/Organization.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/Organization.php b/sdks/php5/apache-usergrid/src/Api/Models/Organization.php
new file mode 100644
index 0000000..adf9833
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/Organization.php
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Guzzle\Service\Command\ResponseClassInterface;
+
+/**
+ * Class Organization
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Organization extends BaseCollection implements ResponseClassInterface
+{
+
+ use GuzzleCommandTrait;
+
+ /**
+ * @var
+ */
+ protected $name;
+
+ /**
+ * Collection Applications
+ *
+ * @var \Apache\Usergrid\Api\Models\Application $properties
+ */
+ protected $applications;
+
+ /** Organization UUID
+ * @var UUID $uuid
+ */
+ protected $uuid;
+
+
+ /**
+ *
+ * @var Array $properties
+ */
+ protected $properties = array();
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Models/User.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Models/User.php b/sdks/php5/apache-usergrid/src/Api/Models/User.php
new file mode 100644
index 0000000..a2d5ac4
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Models/User.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api\Models;
+
+
+use Guzzle\Service\Command\ResponseClassInterface;
+
+/**
+ * Class User
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class User extends BaseCollection implements ResponseClassInterface
+{
+ use GuzzleCommandTrait;
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/QueryAggregator.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/QueryAggregator.php b/sdks/php5/apache-usergrid/src/Api/QueryAggregator.php
new file mode 100644
index 0000000..f1362a7
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/QueryAggregator.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api;
+
+
+use Guzzle\Http\QueryString;
+use Guzzle\Http\QueryAggregator\QueryAggregatorInterface;
+
+/**
+ * Class QueryAggregator
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class QueryAggregator implements QueryAggregatorInterface
+{
+
+ /**
+ * {@inheritDoc}
+ */
+ public function aggregate($key, $value, QueryString $query)
+ {
+ $response = [];
+
+ foreach ($value as $k => $v) {
+ if (is_int($k)) {
+ return [
+ $query->encodeValue("{$key}[]") => $value
+ ];
+ }
+
+ $k = "{$key}[{$k}]";
+
+ if (is_array($v)) {
+ $response = array_merge($response, self::aggregate($k, $v, $query));
+ } else {
+ $response[$query->encodeValue($k)] = $query->encodeValue($v);
+ }
+ }
+
+ return $response;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/ResourceIterator.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/ResourceIterator.php b/sdks/php5/apache-usergrid/src/Api/ResourceIterator.php
new file mode 100644
index 0000000..9eb9065
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/ResourceIterator.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+
+namespace Apache\Usergrid\Api;
+
+use Guzzle\Service\Command\CommandInterface;
+use Guzzle\Service\Resource\ResourceIterator as BaseIterator;
+
+/**
+ * Class ResourceIterator
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class ResourceIterator extends BaseIterator
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function __construct(CommandInterface $command, array $data = [])
+ {
+ parent::__construct($command, $data);
+
+ $this->pageSize = 100;
+ }
+
+ /**
+ * Send a request to retrieve the next page of results. Hook for subclasses to implement.
+ *
+ * @return array Returns the newly loaded resources
+ */
+ protected function sendRequest()
+ {
+ $this->command->set('limit', $this->pageSize);
+
+ if ($this->nextToken) {
+ $this->command->set('cursor', $this->nextToken);
+ }
+
+ $result = $this->command->execute();
+
+ $data = $result['entities'];
+
+ $lastItem = end($data);
+
+ $this->nextToken = $result['has_more'] ? $lastItem['uuid'] : false;
+
+ return $data;
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Api/Usergrid.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Api/Usergrid.php b/sdks/php5/apache-usergrid/src/Api/Usergrid.php
new file mode 100644
index 0000000..5346b2d
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Api/Usergrid.php
@@ -0,0 +1,453 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Api;
+
+
+use Apache\Usergrid\Guzzle\Plugin\Oauth2\Oauth2Plugin;
+use Guzzle\Common\Event;
+use InvalidArgumentException;
+use Guzzle\Service\Description\ServiceDescription;
+use Guzzle\Plugin\ErrorResponse\ErrorResponsePlugin;
+
+/**
+ * Class Usergrid
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Usergrid
+{
+
+ /**
+ * Header Bearer Token
+ * @var
+ */
+ protected $token;
+
+ /**
+ * Grant Type Client Id or password
+ * @var
+ */
+ protected $grantType;
+ /**
+ * The Usergrid API version.
+ *
+ * @var string
+ */
+ protected $version = '1.0.0';
+ /**
+ * The manifests path.
+ *
+ * @var string
+ */
+ protected $manifestPath;
+ /**
+ * The Base URL.
+ *
+ * @var string
+ */
+ protected $baseUrl;
+ /**
+ * Oauth2 Guzzle Plugin.
+ *
+ * @var \Apache\Usergrid\Guzzle\Plugin\Oauth2\Oauth2Plugin
+ */
+ protected $oauth2_plugin;
+ /**
+ * @return string
+ */
+ public function getManifestPath()
+ {
+ return $this->manifestPath;
+ }
+
+ /**
+ * @param string $manifestPath
+ * @return $this
+ */
+ public function setManifestPath($manifestPath)
+ {
+ $this->manifestPath = $manifestPath;
+ return $this;
+ }
+
+ /**
+ * The cached manifests data.
+ *
+ * @var array
+ */
+ protected $manifests = [];
+ /**
+ * The user agent.
+ *
+ * @var string
+ */
+ protected $userAgent = 'BaaS-Usergrid/1.0.0';
+
+ /**
+ * The headers to be sent to the Guzzle client.
+ *
+ * @var array
+ */
+ protected $headers = [];
+
+ private $org_name;
+ private $app_name;
+
+ /**
+ * @param null $orgName
+ * @param null $appName
+ * @param $manifestPath
+ * @param $version
+ * @param $baseUrl
+ * @param Oauth2Plugin $oauth2_plugin
+ */
+ function __construct($orgName = null, $appName = null, $manifestPath, $version, $baseUrl, Oauth2Plugin $oauth2_plugin = null)
+ {
+ //Set Version so its added to header
+ $this->setVersion($version); //TODO: move to constructor param
+
+ $this->baseUrl = $baseUrl;
+
+ $this->org_name = $orgName;
+ $this->app_name = $appName;
+
+ //check if OAuth2 plugin is enabled
+ if($oauth2_plugin != null){
+ $this->oauth2_plugin = $oauth2_plugin;
+ }
+
+ // Set the manifest path
+ $this->setManifestPath($manifestPath ?: __DIR__.'/Manifests');
+ }
+
+ /**
+ * Returns the Guzzle client headers.
+ *
+ * @return array
+ */
+ public function getHeaders()
+ {
+ return $this->headers;
+ }
+
+ /**
+ * Sets the Guzzle client headers.
+ *
+ * @param array $headers
+ * @return $this
+ */
+ public function setHeaders(array $headers = [])
+ {
+ $this->headers = array_merge($this->headers, $headers);
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getVersion()
+ {
+ return $this->version;
+ }
+
+ /**
+ * @param string $version
+ * @return $this
+ */
+ public function setVersion($version)
+ {
+ $this->version = $version;
+
+ $this->setHeaders([
+ 'Usergrid-Version' => (string)$version,
+ ]);
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getUserAgent()
+ {
+ return $this->userAgent;
+ }
+
+ /**
+ * @param string $userAgent
+ * @return $this
+ */
+ public function setUserAgent($userAgent)
+ {
+ $this->userAgent = $userAgent;
+
+ return $this;
+ }
+
+ /**
+ * @param mixed $token
+ * @return $this
+ */
+ public function setToken($token)
+ {
+ $this->token = $token;
+
+ $this->setHeaders([
+ 'Authorization' => (string)'Bearer ' . $token,
+ ]);
+
+ return $this;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getToken()
+ {
+ return $this->token;
+ }
+
+ /**
+ * Dynamically handle missing methods.
+ *
+ * @param string $method
+ * @param array $arguments
+ * @return mixed
+ */
+ public function __call($method, array $arguments = [])
+ {
+ if (substr($method, -8) === 'Iterator') {
+ return $this->handleIteratorRequest($method, $arguments);
+ } elseif ($this->isSingleRequest($method)) {
+ return $this->handleSingleRequest($method, $arguments);
+ }
+
+ return $this->handleRequest($method);
+ }
+
+ /**
+ * Determines if the request is a single request.
+ *
+ * @return bool
+ */
+ protected function isSingleRequest($method)
+ {
+ return (str_singular($method) === $method && $this->manifestExists(str_plural($method)));
+ }
+
+ /**
+ * Handles a single request.
+ *
+ * @param string $method
+ * @param array $arguments
+ * @return \Guzzle\Service\Client
+ * @throws \InvalidArgumentException
+ */
+ protected function handleSingleRequest($method, array $arguments = [])
+ {
+ // Check if we have any arguments
+ if (empty($arguments)) {
+ throw new InvalidArgumentException('Not enough arguments provided!');
+ }
+
+ // Pluralize the method name
+ $pluralMethod = str_plural($method);
+
+ // Get the request manifest payload data
+ $manifest = $this->getRequestManifestPayload($pluralMethod);
+
+ if (!$parameters = array_get($manifest, 'find')) {
+ throw new InvalidArgumentException("Undefined method [{$method}] called.");;
+ }
+
+ // Get the required parameters for the request
+ $required = array_where(array_get($parameters, 'parameters'), function ($key, $value) {
+ return $value['required'] === true;
+ });
+
+ // Prepare the arguments for the request
+ $arguments = array_combine(
+ array_keys($required),
+ count($required) === 1 ? (array)$arguments[0] : $arguments
+ );
+
+ // Execute the request
+ return $this->handleRequest($pluralMethod)->find($arguments);
+ }
+
+ /**
+ * Handles an iterator request.
+ *
+ * @param string $method
+ * @param array $arguments
+ * @return \Apache\Usergrid\Api\ResourceIterator
+ * @throws \InvalidArgumentException
+ */
+ protected function handleIteratorRequest($method, array $arguments = [])
+ {
+ $client = $this->handleRequest(substr($method, 0, -8));
+
+ $command = $client->getCommand('all', array_get($arguments, 0, []));
+
+ return new ResourceIterator($command, array_get($arguments, 1, []));
+ }
+
+ /**
+ * Handles the current request.
+ *
+ * @param string $method
+ * @throws InvalidArgumentException
+ * @return \Guzzle\Service\Client
+ */
+ protected function handleRequest($method)
+ {
+ if (!$this->manifestExists($method)) {
+ throw new InvalidArgumentException("Undefined method [{$method}] called.");
+ }
+
+ // Initialize the Guzzle client
+ $client = new GuzzleClient('',['command.params' => ['app_name_or_uuid' => $this->app_name, 'org_name_or_uuid' => $this->org_name]]);
+
+ // Set our own usergrid api client for internal
+ // usage within our api models.
+ $client->setApiClient($this);
+
+ // Set the client user agent
+ $client->setUserAgent($this->getUserAgent(), true);
+
+
+ // Set the headers
+ $client->setDefaultOption('headers', $this->getHeaders());
+
+ // Get the Guzzle event dispatcher
+ $dispatcher = $client->getEventDispatcher();
+
+ // Register the error response plugin for our custom exceptions
+ $dispatcher->addSubscriber(new ErrorResponsePlugin);
+
+ // Listen to the "command.after_prepare" event fired by Guzzle
+ $dispatcher->addListener('command.after_prepare', function (Event $event) {
+ $request = $event['command']->getRequest();
+
+ $request->getQuery()->setAggregator(new QueryAggregator());
+ });
+
+ //check if Oauth 2 plugin is a instance of Oauth2Plugin
+ if($this->oauth2_plugin instanceof Oauth2Plugin) {
+ $dispatcher->addSubscriber($this->oauth2_plugin);
+ }
+
+
+ // Set the manifest payload into the Guzzle client
+ $client->setDescription(ServiceDescription::factory(
+ $this->buildPayload($method)
+ ));
+
+ // Return the Guzzle client
+ return $client;
+ }
+
+ /**
+ * Returns the full versioned manifests path.
+ *
+ * @return string
+ */
+ protected function getFullManifestPath()
+ {
+ return $this->getManifestPath() . '/' . $this->getVersion();
+ }
+
+ /**
+ * Returns the given request manifest file path.
+ *
+ * @param string $file
+ * @return string
+ */
+ protected function getManifestFilePath($file)
+ {
+ return $this->getFullManifestPath() . '/' . ucwords($file) . '.php';
+ }
+
+ /**
+ * Returns the current request payload.
+ *
+ * @param string $method
+ * @return array
+ */
+ protected function buildPayload($method)
+ {
+ $operations = $this->getRequestManifestPayload($method);
+
+ $manifest = $this->getRequestManifestPayload('manifest', false);
+
+ return array_merge($manifest, compact('operations'));
+ }
+
+ /**
+ * Returns the given file manifest data.
+ *
+ * @param string $file
+ * @param bool $includeErrors
+ * @return array
+ */
+ protected function getRequestManifestPayload($file, $includeErrors = true)
+ {
+ $file = ucwords($file);
+
+ /** @noinspection PhpUnusedLocalVariableInspection */
+ $baseURL = $this->baseUrl;
+
+ if (!$manifest = array_get($this->manifests, $file)) {
+ if ($includeErrors) {
+ /** @noinspection PhpUnusedLocalVariableInspection */
+ $errors = $this->getRequestManifestPayload('errors', false);
+ }
+
+ /** @noinspection PhpIncludeInspection */
+ $manifest = require_once $this->getManifestFilePath($file);
+
+ array_set($this->manifests, $file, $manifest);
+ }
+
+ return $manifest;
+ }
+
+ /**
+ * Checks if the manifest file for the current request exists.
+ *
+ * @param string $file
+ * @return bool
+ */
+ protected function manifestExists($file)
+ {
+ return file_exists($this->getManifestFilePath($file));
+ }
+
+ /**
+ * fetch token from by making api call,
+ * and call $this->setToken which will set the bearer token in the
+ * http header
+ */
+ protected function fetchToken(){
+
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/AuthorizationCode.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/AuthorizationCode.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/AuthorizationCode.php
new file mode 100755
index 0000000..0a31df6
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/AuthorizationCode.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType;
+
+use Guzzle\Common\Collection;
+use Guzzle\Http\ClientInterface;
+use Guzzle\Http\Exception\RequestException;
+
+/**
+ * Authorization code grant type.
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ *
+ * @link http://tools.ietf.org/html/rfc6749#section-4.1
+ */
+class AuthorizationCode implements GrantTypeInterface
+{
+ /** @var ClientInterface The token endpoint client */
+ protected $client;
+
+ /** @var Collection Configuration settings */
+ protected $config;
+
+ public function __construct(ClientInterface $client, $config)
+ {
+ $this->client = $client;
+ $this->config = Collection::fromConfig($config, array(
+ 'client_secret' => '',
+ 'scope' => '',
+ 'redirect_uri' => '',
+ ), array(
+ 'client_id', 'code',
+ ));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getTokenData()
+ {
+ $postBody = array(
+ 'grant_type' => 'authorization_code',
+ 'code' => $this->config['code'],
+ );
+ if ($this->config['scope']) {
+ $postBody['scope'] = $this->config['scope'];
+ }
+ if ($this->config['redirect_uri']) {
+ $postBody['redirect_uri'] = $this->config['redirect_uri'];
+ }
+ $request = $this->client->post(null, array(), $postBody);
+ $request->setAuth($this->config['client_id'], $this->config['client_secret']);
+ $response = $request->send();
+ $data = $response->json();
+
+ $requiredData = array_flip(array('access_token', 'expires_in', 'refresh_token'));
+ return array_intersect_key($data, $requiredData);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/ClientCredentials.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/ClientCredentials.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/ClientCredentials.php
new file mode 100755
index 0000000..bec4968
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/ClientCredentials.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType;
+
+use Guzzle\Common\Collection;
+use Guzzle\Http\ClientInterface;
+use Guzzle\Http\Exception\RequestException;
+
+/**
+ * Client credentials grant type.
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ *
+ * @link http://tools.ietf.org/html/rfc6749#section-4.4
+ */
+class ClientCredentials implements GrantTypeInterface
+{
+ /** @var ClientInterface The token endpoint client */
+ protected $client;
+
+ /** @var Collection Configuration settings */
+ protected $config;
+
+ public function __construct(ClientInterface $client, $config)
+ {
+ $this->client = $client;
+ $this->config = Collection::fromConfig($config, array(
+ 'client_secret' => '',
+ 'scope' => '',
+ ), array(
+ 'client_id',
+ ));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getTokenData()
+ {
+ $postBody = array(
+ 'grant_type' => 'client_credentials',
+ 'client_id' => $this->config['client_id'],
+ 'client_secret' => $this->config['client_secret']
+ );
+ if ($this->config['scope']) {
+ $postBody['scope'] = $this->config['scope'];
+ }
+ $request = $this->client->post(null, array(), $postBody);
+// $request->setAuth($this->config['client_id'], $this->config['client_secret']);
+ $response = $request->send();
+ $data = $response->json();
+
+ $requiredData = array_flip(array('access_token', 'expires_in', 'refresh_token'));
+ return array_intersect_key($data, $requiredData);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/GrantTypeInterface.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/GrantTypeInterface.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/GrantTypeInterface.php
new file mode 100755
index 0000000..99096c0
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/GrantTypeInterface.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType;
+
+
+/**
+ * Interface GrantTypeInterface
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+interface GrantTypeInterface
+{
+ /**
+ * Get the token data returned by the OAuth2 server.
+ *
+ * @return array An array with the following keys:
+ * - access_token: The access token.
+ * - expires_in: The access token lifetime, in seconds.
+ * - refresh_token: The refresh token, if present.
+ */
+ public function getTokenData();
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/HWIOAuthBundleRefreshToken.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/HWIOAuthBundleRefreshToken.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/HWIOAuthBundleRefreshToken.php
new file mode 100755
index 0000000..c0d3713
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/HWIOAuthBundleRefreshToken.php
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType;
+
+use Symfony\Component\Security\Core\SecurityContextInterface;
+use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMap;
+
+/**
+ * HWIOAuthBundle Aware Refresh token grant type.
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ *
+ * @link http://tools.ietf.org/html/rfc6749#section-6
+ */
+class HWIOAuthBundleRefreshToken implements GrantTypeInterface
+{
+ /** @var SecurityContextInterface Symfony2 security component */
+ protected $securityContext;
+
+ /** @var ResourceOwnerMap HWIOAuthBundle OAuth2 ResourceOwnerMap */
+ protected $resourceOwnerMap;
+
+ public function __construct(ResourceOwnerMap $resourceOwnerMap, SecurityContextInterface $securityContext)
+ {
+ $this->securityContext = $securityContext;
+ $this->resourceOwnerMap = $resourceOwnerMap;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getTokenData($refreshToken = null)
+ {
+ $token = $this->securityContext->getToken();
+ $resourceName = $token->getResourceOwnerName();
+ $resourceOwner = $this->resourceOwnerMap->getResourceOwnerByName($resourceName);
+
+ $data = $resourceOwner->refreshAccessToken($refreshToken);
+ $token->setRawToken($data);
+ $requiredData = array_flip(array('access_token', 'expires_in', 'refresh_token'));
+
+ return array_intersect_key($data, $requiredData);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/PasswordCredentials.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/PasswordCredentials.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/PasswordCredentials.php
new file mode 100755
index 0000000..44cf77b
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/PasswordCredentials.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType;
+
+use Guzzle\Common\Collection;
+use Guzzle\Http\ClientInterface;
+use Guzzle\Http\Exception\RequestException;
+
+/**
+ * Resource owner password credentials grant type.
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ *
+ * @link http://tools.ietf.org/html/rfc6749#section-4.3
+ */
+class PasswordCredentials implements GrantTypeInterface
+{
+ /** @var ClientInterface The token endpoint client */
+ protected $client;
+
+ /** @var Collection Configuration settings */
+ protected $config;
+
+ public function __construct(ClientInterface $client, $config)
+ {
+ $this->client = $client;
+ $this->config = Collection::fromConfig($config, array(
+ 'client_secret' => '',
+ 'scope' => '',
+ ), array(
+ 'client_id', 'username', 'password'
+ ));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getTokenData()
+ {
+ $postBody = array(
+ 'grant_type' => 'password',
+ 'username' => $this->config['username'],
+ 'password' => $this->config['password'],
+ );
+ if ($this->config['scope']) {
+ $postBody['scope'] = $this->config['scope'];
+ }
+ $request = $this->client->post(null, array(), $postBody);
+ //Note: Usergrid it not using Oauth2 to spec so it dose not need a Auth type set no need to client id + secret when using password
+// $request->setAuth($this->config['client_id'], $this->config['client_secret']);
+ $response = $request->send();
+ $data = $response->json();
+
+ $requiredData = array_flip(array('access_token', 'expires_in', 'refresh_token'));
+ return array_intersect_key($data, $requiredData);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/RefreshToken.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/RefreshToken.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/RefreshToken.php
new file mode 100755
index 0000000..10999b2
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/GrantType/RefreshToken.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType;
+
+use Guzzle\Common\Collection;
+use Guzzle\Http\ClientInterface;
+use Guzzle\Http\Exception\RequestException;
+
+/**
+ * Refresh token grant type.
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ *
+ * @link http://tools.ietf.org/html/rfc6749#section-6
+ */
+class RefreshToken implements GrantTypeInterface
+{
+ /** @var ClientInterface The token endpoint client */
+ protected $client;
+
+ /** @var Collection Configuration settings */
+ protected $config;
+
+ public function __construct(ClientInterface $client, $config)
+ {
+ $this->client = $client;
+ $this->config = Collection::fromConfig($config, array(
+ 'client_secret' => '',
+ 'refresh_token' => '',
+ 'scope' => '',
+ ), array(
+ 'client_id',
+ ));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getTokenData($refreshToken = null)
+ {
+ $postBody = array(
+ 'grant_type' => 'refresh_token',
+ // If no refresh token was provided to the method, use the one
+ // provided to the constructor.
+ 'refresh_token' => $refreshToken ?: $this->config['refresh_token'],
+ );
+ if ($this->config['scope']) {
+ $postBody['scope'] = $this->config['scope'];
+ }
+ $request = $this->client->post(null, array(), $postBody);
+ $request->setAuth($this->config['client_id'], $this->config['client_secret']);
+ $response = $request->send();
+ $data = $response->json();
+
+ $requiredData = array_flip(array('access_token', 'expires_in', 'refresh_token'));
+ return array_intersect_key($data, $requiredData);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/Oauth2Plugin.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/Oauth2Plugin.php b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/Oauth2Plugin.php
new file mode 100755
index 0000000..e5671f5
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Guzzle/Plugin/Oauth2/Oauth2Plugin.php
@@ -0,0 +1,204 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+namespace Apache\Usergrid\Guzzle\Plugin\Oauth2;
+
+use Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType\GrantTypeInterface;
+use Guzzle\Common\Event;
+use Guzzle\Http\Exception\BadResponseException;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * OAuth2 plugin
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ *
+ * @link http://tools.ietf.org/html/rfc6749
+ */
+class Oauth2Plugin implements EventSubscriberInterface
+{
+
+ /** @var GrantTypeInterface The grant type implementation used to acquire access tokens */
+ protected $grantType;
+
+ /** @var GrantTypeInterface The grant type implementation used to refresh access tokens */
+ protected $refreshTokenGrantType;
+
+ /** @var array An array with the "access_token" and "expires" keys */
+ protected $accessToken;
+
+ /** @var string The refresh token string. * */
+ protected $refreshToken;
+
+ /**
+ * Create a new Oauth2 plugin
+ * @param GrantTypeInterface $grantType
+ * @param GrantTypeInterface $refreshTokenGrantType
+ */
+ public function __construct(GrantTypeInterface $grantType = null, GrantTypeInterface $refreshTokenGrantType = null)
+ {
+ $this->grantType = $grantType;
+ $this->refreshTokenGrantType = $refreshTokenGrantType;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function getSubscribedEvents()
+ {
+ return array(
+ 'request.before_send' => 'onRequestBeforeSend',
+ 'request.error' => 'onRequestError',
+ );
+ }
+
+ /**
+ * Request before-send event handler.
+ *
+ * Adds the Authorization header if an access token was found.
+ *
+ * @param Event $event Event received
+ */
+ public function onRequestBeforeSend(Event $event)
+ {
+ $accessToken = $this->getAccessToken();
+ if ($accessToken) {
+ $event['request']->setHeader('Authorization', 'Bearer ' . $accessToken['access_token']);
+ }
+ }
+
+ /**
+ * Request error event handler.
+ *
+ * Handles unauthorized errors by acquiring a new access token and
+ * retrying the request.
+ *
+ * @param Event $event Event received
+ */
+ public function onRequestError(Event $event)
+ {
+ if ($event['response']->getStatusCode() == 401) {
+ if ($event['request']->getHeader('X-Guzzle-Retry')) {
+ // We already retried once, give up.
+ return;
+ }
+
+ // Acquire a new access token, and retry the request.
+ $newAccessToken = $this->acquireAccessToken();
+ if ($newAccessToken) {
+ $newRequest = clone $event['request'];
+ $newRequest->setHeader('Authorization', 'Bearer ' . $newAccessToken['access_token']);
+ $newRequest->setHeader('X-Guzzle-Retry', '1');
+ $event['response'] = $newRequest->send();
+ $event->stopPropagation();
+ }
+ }
+ }
+
+ /**
+ * Get the access token.
+ *
+ * Handles token expiration for tokens with an "expires" timestamp.
+ * In case no valid token was found, tries to acquire a new one.
+ *
+ * @return array|null
+ */
+ public function getAccessToken()
+ {
+ if (isset($this->accessToken['expires']) && $this->accessToken['expires'] < time()) {
+ // The access token has expired.
+ $this->accessToken = null;
+ }
+ if (!$this->accessToken) {
+ // Try to acquire a new access token from the server.
+ $this->acquireAccessToken();
+ }
+
+ return $this->accessToken;
+ }
+
+ /**
+ * Set the access token.
+ *
+ * @param array|string $accessToken The access token
+ */
+ public function setAccessToken(array $accessToken)
+ {
+ $this->accessToken = $accessToken;
+ }
+
+ /**
+ * Get the refresh token.
+ *
+ * @return string
+ */
+ public function getRefreshToken()
+ {
+ return $this->refreshToken;
+ }
+
+ /**
+ * Set the refresh token.
+ *
+ * @param string $refreshToken The refresh token
+ */
+ public function setRefreshToken($refreshToken)
+ {
+ $this->refreshToken = $refreshToken;
+ }
+
+ /**
+ * Acquire a new access token from the server.
+ *
+ * @return array|null
+ */
+ protected function acquireAccessToken()
+ {
+ if ($this->refreshTokenGrantType && $this->refreshToken) {
+ try {
+ // Get an access token using the stored refresh token.
+ $tokenData = $this->refreshTokenGrantType->getTokenData($this->refreshToken);
+ } catch (BadResponseException $e) {
+ // The refresh token has probably expired.
+ $this->refreshToken = null;
+ }
+ }
+ if ($this->grantType && !isset($tokenData)) {
+ // Get a new access token.
+ $tokenData = $this->grantType->getTokenData();
+ }
+
+ $this->accessToken = null;
+ if (isset($tokenData)) {
+ // Process the returned data. Both expired_in and refresh_token
+ // are optional parameters.
+ $this->accessToken = array(
+ 'access_token' => $tokenData['access_token'],
+ );
+ if (isset($tokenData['expires_in'])) {
+ $this->accessToken['expires'] = time() + $tokenData['expires_in'];
+ }
+ if (isset($tokenData['refresh_token'])) {
+ $this->refreshToken = $tokenData['refresh_token'];
+ }
+ }
+
+ return $this->accessToken;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Laravel/ApacheUsergridServiceProvider.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Laravel/ApacheUsergridServiceProvider.php b/sdks/php5/apache-usergrid/src/Laravel/ApacheUsergridServiceProvider.php
new file mode 100644
index 0000000..5bb4115
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Laravel/ApacheUsergridServiceProvider.php
@@ -0,0 +1,142 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+namespace Apache\Usergrid\Laravel;
+
+
+use Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType\ClientCredentials;
+use Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType\PasswordCredentials;
+use Apache\Usergrid\Guzzle\Plugin\Oauth2\GrantType\RefreshToken;
+use Apache\Usergrid\Guzzle\Plugin\Oauth2\Oauth2Plugin;
+use Guzzle\Http\Client;
+use Illuminate\Support\ServiceProvider;
+
+/**
+ * Class ApacheUsergridServiceProvider
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class ApacheUsergridServiceProvider extends ServiceProvider
+{
+
+ protected $oauth2Plugin = null;
+ /**
+ *
+ */
+ public function boot()
+ {
+ $this->package('apache/usergrid', 'apache/usergrid', __DIR__ . '/..');
+ }
+
+ /**
+ * Register the service provider.
+ *
+ * @return void
+ */
+ public function register()
+ {
+ $enable_oauth2_plugin = $this->app['config']->get('apache/usergrid::usergrid.enable_oauth2_plugin');
+
+ //check if user managed oauth auth flow
+ if($enable_oauth2_plugin){
+ // Create the Oauth2 Guzzle Plugin.
+ $this->createGuzzleOauth2Plugin();
+ }
+ // register Usergrid
+ $this->registerUsergrid();
+
+ }
+
+ protected function registerUsergrid() {
+
+ $this->app['usergrid'] = $this->app->share(function ($app) {
+
+ $baseUrl = $app['config']->get('apache/usergrid::usergrid.url');
+
+ $orgName = $app['config']->get('apache/usergrid::usergrid.orgName');
+
+ $appName = $app['config']->get('apache/usergrid::usergrid.appName');
+
+ $manifestPath = $app['config']->get('apache/usergrid::usergrid.manifestPath');
+
+ $version = $app['config']->get('apache/usergrid::usergrid.version');
+
+ return new Usergrid($orgName, $appName, $manifestPath, $version, $baseUrl, $this->oauth2Plugin);
+ });
+
+ $this->app->alias('usergrid', 'Apache\Usergrid\Api\Usergrid');
+ }
+
+
+ protected function createGuzzleOauth2Plugin(){
+
+ $base_url = $this->app['config']->get('apache/usergrid::usergrid.url');
+
+ $org_name = $this->app['config']->get('apache/usergrid::usergrid.orgName');
+
+ $app_name = $this->app['config']->get('apache/usergrid::usergrid.appName');
+
+ $grant_type = $this->app['config']->get('apache/usergrid::usergrid.grant_type');
+
+ $client_id = $this->app['config']->get('apache/usergrid::usergrid.clientId');
+
+ $client_secret = $this->app['config']->get('apache/usergrid::usergrid.clientSecret');
+
+ $username = $this->app['config']->get('apache/usergrid::usergrid.username');
+
+ $password = $this->app['config']->get('apache/usergrid::usergrid.password');
+
+
+ if($this->app['config']->get('apache/usergrid::usergrid.auth_type') == 'organization') {
+
+ $url = $base_url.'/management/token';
+
+ } elseif($this->app['config']->get('apache/usergrid::usergrid.auth_type') == 'application')
+ {
+ $url = $base_url.'/'.$org_name.'/'.$app_name.'/token';
+ }
+
+ $oauth2Client = new Client($url);
+
+ if($grant_type == 'client_credentials') {
+ $config = [
+ 'client_id' => $client_id,
+ 'client_secret' => $client_secret,
+
+ ];
+ $grantType = new ClientCredentials($oauth2Client, $config);
+ $refreshTokenGrantType = new RefreshToken($oauth2Client,$config);
+ $this->oauth2Plugin = new Oauth2Plugin($grantType, $refreshTokenGrantType);
+
+ } elseif($grant_type == 'password') {
+ $config = [
+ 'username' => $username,
+ 'password' => $password,
+ 'client_id' => $client_id,
+ 'client_secret' => $client_secret
+ ];
+ $grantType = new PasswordCredentials($oauth2Client, $config);
+ $refreshTokenGrantType = new RefreshToken($oauth2Client, $config);
+ $this->oauth2Plugin = new Oauth2Plugin($grantType,$refreshTokenGrantType);
+ }
+
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/1f8a4920/sdks/php5/apache-usergrid/src/Laravel/Facades/Usergrid.php
----------------------------------------------------------------------
diff --git a/sdks/php5/apache-usergrid/src/Laravel/Facades/Usergrid.php b/sdks/php5/apache-usergrid/src/Laravel/Facades/Usergrid.php
new file mode 100644
index 0000000..1ea29f2
--- /dev/null
+++ b/sdks/php5/apache-usergrid/src/Laravel/Facades/Usergrid.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * Copyright 2010-2014 baas-platform.com, Pty Ltd. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+
+namespace Apache\Usergrid\Laravel\Facades;
+
+
+use Illuminate\Support\Facades\Facade;
+
+/**
+ * Class Usergrid
+ *
+ * @package Apache/Usergrid
+ * @version 1.0.0
+ * @author Jason Kristian <ja...@gmail.com>
+ * @license Apache License, Version 2.0
+ * @copyright (c) 2008-2014, Baas Platform Pty. Ltd
+ * @link http://baas-platform.com
+ */
+class Usergrid extends Facade
+{
+ /**
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return "usergrid";
+ }
+
+}
\ No newline at end of file