Skip to content

Commit 07ee0db

Browse files
committed
initial commit of a DrupalOrg API SDK.
0 parents  commit 07ee0db

14 files changed

+1116
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
index.php
2+
vendor/*
3+
composer.lock
4+
.idea
5+
*.iml
6+
.htaccess

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "eclipsegc/drupal-org-api",
3+
"type": "library",
4+
"description": "Drupal.org API SDK for PHP.",
5+
"keywords": ["drupal", "drupal.org"],
6+
"homepage": "https://github.com/EclipseGc/drupal-org-api",
7+
"license": "GPL-2.0+",
8+
"authors": [
9+
{
10+
"name": "Kris Vanderwater"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0",
15+
"guzzlehttp/guzzle": "5.1.*"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"EclipseGc\\DrupalOrg\\Api\\": "src/"
20+
}
21+
}
22+
}
23+

src/DrupalClient.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains DrupalClient.php.
5+
*/
6+
7+
namespace EclipseGc\DrupalOrg\Api;
8+
9+
10+
use EclipseGc\DrupalOrg\Api\Resources\GetResource;
11+
use GuzzleHttp\Client;
12+
13+
class DrupalClient {
14+
15+
use GetResource;
16+
17+
/**
18+
* @return static
19+
*/
20+
public static function create() {
21+
$factory = static::createFactory();
22+
return new static($factory);
23+
}
24+
25+
/**
26+
* @return \EclipseGc\DrupalOrg\Api\FactoryInterface
27+
*/
28+
public static function createFactory() {
29+
$guzzle = new Client([
30+
'base_url' => 'https://www.drupal.org/api-d7/',
31+
'defaults' => ['headers' => ['Accept' => 'application/json']]
32+
]);
33+
return new Factory($guzzle);
34+
}
35+
36+
/**
37+
* @param FactoryInterface $factory
38+
*/
39+
public function __construct(FactoryInterface $factory) {
40+
$this->factory = $factory;
41+
}
42+
43+
/**
44+
* @param $id
45+
* @return \EclipseGc\DrupalOrg\Api\Resources\User
46+
*/
47+
public function getUser($id) {
48+
return $this->getResource('user', $id);
49+
}
50+
51+
/**
52+
* @param $id
53+
* @return \EclipseGc\DrupalOrg\Api\Resources\Node\NodeInterface;
54+
*/
55+
public function getNode($id) {
56+
return $this->getResource('node', $id);
57+
}
58+
59+
}

src/Factory.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains Factory.php.
5+
*/
6+
7+
namespace EclipseGc\DrupalOrg\Api;
8+
9+
use GuzzleHttp\Client;
10+
11+
class Factory implements FactoryInterface {
12+
13+
protected $objectTypes = array(
14+
'user' => '\EclipseGc\DrupalOrg\Api\Resources\User',
15+
'node' => '\EclipseGc\DrupalOrg\Api\Resources\Node',
16+
);
17+
18+
function __construct(Client $client) {
19+
$this->client($client);
20+
}
21+
22+
public function getObjectTypeClass($type, array $data) {
23+
if (isset($this->objectTypes[$type])) {
24+
$class = $this->objectTypes[$type];
25+
return $class::getClass($data);
26+
}
27+
throw new \Exception(sprintf('No object type %s', $type));
28+
}
29+
30+
public function createObjectType($type, array $data = array()) {
31+
$objectClass = $this->getObjectTypeClass($type, $data);
32+
$reflector = new \ReflectionClass($objectClass);
33+
if (!isset($data['factory'])) {
34+
$data['factory'] = $this;
35+
}
36+
$arguments = [];
37+
foreach ($reflector->getMethod('__construct')->getParameters() as $param) {
38+
$param_name = $param->getName();
39+
if (array_key_exists($param_name, $data)) {
40+
$arguments[] = $data[$param_name];
41+
}
42+
}
43+
return $reflector->newInstanceArgs($arguments);
44+
}
45+
46+
/**
47+
* @param $entity_type
48+
* @param $id
49+
* @return \GuzzleHttp\Message\ResponseInterface
50+
* @throws \Exception
51+
*/
52+
public function request($entity_type, $id) {
53+
$request = $this->client()->get(['{entity_type}/{id}', ['entity_type' => $entity_type, 'id' => $id]]);
54+
if ($request->getStatusCode() != 200) {
55+
throw new \Exception(sprintf('Status code was not OK. %d returned instead.', $request->getStatusCode()));
56+
}
57+
return $request;
58+
}
59+
60+
/**
61+
* Statically stores and returns a guzzle client.
62+
*
63+
* The Guzzle client is quite large and we really don't want to deal with it
64+
* during debugging, so we store it statically in a method to hide it away.
65+
*
66+
* @param \GuzzleHttp\Client $new_client
67+
*
68+
* @return \GuzzleHttp\Client
69+
*/
70+
protected function client(Client $new_client = NULL) {
71+
static $client;
72+
if (!is_null($new_client)) {
73+
$client = $new_client;
74+
}
75+
return $client;
76+
}
77+
78+
}

src/FactoryInterface.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains FactoryInterface.php.
5+
*/
6+
namespace EclipseGc\DrupalOrg\Api;
7+
8+
interface FactoryInterface {
9+
10+
/**
11+
* Determines the name of a class to instantiate for this type/data combo.
12+
*
13+
* @param $type
14+
* The object type for which to return a class name.
15+
* @param array $data
16+
* The data that will be passed into the class. This can also be used to
17+
* change which class is instantiated.
18+
* @return string
19+
* The class name to instantiate.
20+
*/
21+
public function getObjectTypeClass($type, array $data);
22+
23+
/**
24+
* Makes a request against the appropriate end point.
25+
*
26+
* @param $entity_type
27+
* The type of entity.
28+
* @param $id
29+
* The id of the entity.
30+
* @return \GuzzleHttp\Message\ResponseInterface
31+
* @throws \Exception
32+
*/
33+
public function request($entity_type, $id);
34+
35+
/**
36+
* Results in a full object for the provided data.
37+
*
38+
* @param $type
39+
* The entity type for which to return an object from the request results.
40+
* @param array $data
41+
* The returned request data. This can be used to change which class is
42+
* instantiated.
43+
* @return \EclipseGc\DrupalOrg\Api\Resources\ResourceInterface
44+
*/
45+
public function createObjectType($type, array $data = array());
46+
}

src/Resources/GetResource.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains GetResource.php.
5+
*/
6+
7+
namespace EclipseGc\DrupalOrg\Api\Resources;
8+
9+
trait GetResource {
10+
11+
/**
12+
* @var \EclipseGc\DrupalOrg\Api\FactoryInterface
13+
*/
14+
protected $factory;
15+
16+
protected function getResource($resource_type, $id) {
17+
$response = $this->factory->request($resource_type, $id);
18+
return $this->factory->createObjectType($resource_type, $response->json());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)