Skip to content

Commit a209af1

Browse files
Merge pull request #10 from phansys/composer
[Upgrade] Move to NSs, autoloading with PSR-4 and dependency management
2 parents fb25f1e + 4258aac commit a209af1

8 files changed

+212
-140
lines changed

README.textile

+17-14
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ h2. Overview
44

55
The SoftLayer API PHP client classes provide a simple method for connecting to and making calls from the SoftLayer API and provides support for many of the SoftLayer API's features. Method calls and client management are handled by the PHP SOAP and XML-RPC extensions.
66

7-
Making API calls using the SoftLayer_SoapClient or SoftLayer_XmlrpcClient classes is done in the following steps:
7+
Making API calls using the \SoftLayer\SoapClient or \SoftLayer\XmlRpcClient classes is done in the following steps:
88

9-
# Instantiate a new SoftLayer_SoapClient or SoftLayer_XmlrpcClient object using the SoftLayer_SoapClient::getClient() or SoftLayer_XmlrpcClient::getClient() methods. Provide the name of the service that you wish to query, an optional id number of the object that you wish to instantiate, your SoftLayer API username, your SoftLayer API key, and an optional API endpoint base URL. The client classes default to connect over the public Internet. Enter SoftLayer_SoapClient::API_PRIVATE_ENDPOINT or SoftLayer_XmlrpcClient::API_PRIVATE_ENDPOINT to connect to the API over SoftLayer's private network. The system making API calls must be connected to SoftLayer's private network (eg. purchased from SoftLayer or connected via VPN) in order to use the private network API endpoints.
9+
# Instantiate a new \SoftLayer\SoapClient or \SoftLayer\XmlRpcClient object using the \SoftLayer\SoapClient::getClient() or \SoftLayer\XmlRpcClient::getClient() methods. Provide the name of the service that you wish to query, an optional id number of the object that you wish to instantiate, your SoftLayer API username, your SoftLayer API key, and an optional API endpoint base URL. The client classes default to connect over the public Internet. Enter \SoftLayer\SoapClient::API_PRIVATE_ENDPOINT or \SoftLayer\XmlRpcClient::API_PRIVATE_ENDPOINT to connect to the API over SoftLayer's private network. The system making API calls must be connected to SoftLayer's private network (eg. purchased from SoftLayer or connected via VPN) in order to use the private network API endpoints.
1010
# Define and add optional headers to the client, such as object masks and result limits.
1111
# Call the API method you wish to call as if it were local to your client object. This class throws exceptions if it's unable to execute a query, so it's best to place API method calls in try / catch statements for proper error handling.
1212

@@ -16,36 +16,39 @@ The most up to date version of this library can be found on the SoftLayer github
1616

1717
h2. System Requirements
1818

19-
The SoftLayer_SoapClient class requires at least PHP 5.2.3 and the PHP SOAP enxtension installed. The SoftLayer_Xmlrpc class requires PHP at least PHP 5 and the PHP XML-RPC extension installed.
19+
The \SoftLayer\SoapClient class requires at least PHP 5.3.0 and the PHP SOAP enxtension installed. The \SoftLayer\XmlRpcClient class requires PHP at least PHP 5 and the PHP XML-RPC extension installed.
2020

2121
A valid API username and key are required to call the SoftLayer API. A connection to the SoftLayer private network is required to connect to SoftLayer's private network API endpopints.
2222

2323
h2. Installation
2424

25-
Download and copy the SoftLayer API client to a directory local to your PHP project or a path within your PHP installation's include_path.
25+
Install the SoftLayer API client using "Composer":https://getcomposer.org/.
26+
<pre><code>
27+
composer require softlayer/softlayer-api-php-client:~1.0@dev
28+
</code></pre>
2629

2730
h2. Usage
2831

29-
These examples use the SoftLayer_SoapClient class. If you wish to use the XML-RPC API then replace mentions of SoapClient.class.php with XmlrpcClient.class.php and SoftLayer_SoapClient with SoftLayer_XmlrpcClient.
32+
These examples use the \SoftLayer\SoapClient class. If you wish to use the XML-RPC API then replace mentions of SoapClient.class.php with XmlrpcClient.class.php and \SoftLayer\SoapClient with \SoftLayer\XmlRpcClient.
3033

3134
Here's a simple usage example that retrieves account information by calling the "getObject()":http://sldn.softlayer.com/reference/services/SoftLayer_Account/getObject method in the "SoftLayer_Account":http://sldn.softlayer.com/reference/services/SoftLayer_Account service:
3235

3336
<pre><code>
3437
<?php
3538

36-
require_once dirname(__FILE__) . '/SoftLayer/SoapClient.class.php';
39+
require_once __DIR__.'/vendor/autoload.php';
3740

3841
$apiUsername = 'set me';
3942
$apiKey = 'set me too';
4043

4144
// Initialize an API client for the SoftLayer_Account service.
42-
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
45+
$client = \SoftLayer\SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
4346

4447
// Retrieve our account record
4548
try {
4649
$account = $client->getObject();
4750
print_r($account);
48-
} catch (Exception $e) {
51+
} catch (\Exception $e) {
4952
die('Unable to retrieve account information: ' . $e->getMessage());
5053
}
5154
</code></pre>
@@ -55,16 +58,16 @@ For a more complex example we'll retrieve a support ticket with id 123456 along
5558
<pre><code>
5659
<?php
5760

58-
require_once dirname(__FILE__) . '/SoftLayer/SoapClient.class.php';
61+
require_once __DIR__.'/vendor/autoload.php';
5962

6063
$apiUsername = 'set me';
6164
$apiKey = 'set me too';
6265

6366
// Initialize an API client for ticket 123456
64-
$client = SoftLayer_SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey);
67+
$client = \SoftLayer\SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey);
6568

6669
// Create an object mask and assign it to our API client.
67-
$objectMask = new SoftLayer_ObjectMask();
70+
$objectMask = new \SoftLayer\ObjectMask();
6871
$objectMask->updates;
6972
$objectMask->assignedUser;
7073
$objectMask->attachedHardware->datacenter;
@@ -73,18 +76,18 @@ $client->setObjectMask($objectMask);
7376
// Retrieve the ticket record
7477
try {
7578
$ticket = $client->getObject();
76-
} catch (Exception $e) {
79+
} catch (\Exception $e) {
7780
die('Unable to retrieve ticket record: ' . $e->getMessage());
7881
}
7982

8083
// Update the ticket
81-
$update = new stdClass();
84+
$update = new \stdClass();
8285
$update->entry = 'Hello!';
8386

8487
try {
8588
$update = $client->addUpdate($update);
8689
echo "Updated ticket 123456. The new update's id is " . $update[0]->id . '.');
87-
} catch (Exception $e) {
90+
} catch (\Exception $e) {
8891
die('Unable to update ticket: ' . $e->getMessage());
8992
}
9093
</code></pre>

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "softlayer/softlayer-api-php-client",
3+
"description": "SoftLayer API PHP client",
4+
"keywords": ["softlayer"],
5+
"homepage": "http://sldn.softlayer.com/article/PHP",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "SoftLayer Development Team",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.3",
15+
"ext-xmlrpc": "*",
16+
"ext-soap": "*"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"SoftLayer\\": "src/"
21+
}
22+
},
23+
"extra": {
24+
"branch-alias": {
25+
"dev-master": "1.0-dev"
26+
}
27+
}
28+
}

composer.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.php

+40-14
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,42 @@
2727
* POSSIBILITY OF SUCH DAMAGE.
2828
*/
2929

30+
use SoftLayer\Common\ObjectMask;
31+
use SoftLayer\SoapClient;
32+
use SoftLayer\XmlRpcClient;
33+
3034
/**
31-
* Start by including the API client class. This example assumes that the
32-
* SoftLayer API classes are in the directory "SoftLayer" relative to this
33-
* script's path.
35+
* Start by including the autoload class.
3436
*
3537
* If you wish to use the XML-RPC API then replace mentions of
36-
* SoapClient.class.php with XmlrpcClient.class.php and SoftLayer_SoapClient
37-
* with SoftLayer_XmlrpcClient.
38+
* SoapClient with XmlRpcClient.
3839
*/
39-
require_once dirname(__FILE__) . '/SoftLayer/SoapClient.class.php';
40+
$files = [
41+
__DIR__.'/vendor/autoload.php',
42+
__DIR__.'/../../autoload.php',
43+
];
44+
45+
$autoload = false;
46+
foreach ($files as $file) {
47+
if (is_file($file)) {
48+
$autoload = include_once $file;
49+
50+
break;
51+
}
52+
}
53+
54+
if (!$autoload) {
55+
die(<<<MSG
56+
Unable to find autoload.php file, please use composer to load dependencies:
57+
58+
wget http://getcomposer.org/composer.phar
59+
php composer.phar install
60+
61+
Visit http://getcomposer.org/ for more information.
62+
63+
MSG
64+
);
65+
}
4066

4167
/**
4268
* It's possible to define your SoftLayer API username and key diredtly in the
@@ -48,7 +74,7 @@
4874

4975
/**
5076
* Usage:
51-
* SoftLayer_SoapClient::getClient([API Service], <object id>, [username], [API key]);
77+
* SoapClient::getClient([API Service], <object id>, [username], [API key]);
5278
*
5379
* API Service: The name of the API service you wish to connect to.
5480
* id: An optional id to initialize your API service with, if you're
@@ -57,7 +83,7 @@
5783
* username: Your SoftLayer API username.
5884
* API key: Your SoftLayer API key,
5985
*/
60-
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
86+
$client = SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
6187

6288
/**
6389
* Once your client object is created you can call API methods for that service
@@ -71,7 +97,7 @@
7197
*/
7298
try {
7399
print_r($client->getObject());
74-
} catch (Exception $e) {
100+
} catch (\Exception $e) {
75101
die($e->getMessage());
76102
}
77103

@@ -84,10 +110,10 @@
84110
*/
85111

86112
// Declare an API client to connect to the SoftLayer_Ticket API service.
87-
$client = SoftLayer_SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey);
113+
$client = SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey);
88114

89115
// Assign an object mask to our API client:
90-
$objectMask = new SoftLayer_ObjectMask();
116+
$objectMask = new ObjectMask();
91117
$objectMask->updates;
92118
$objectMask->assignedUser;
93119
$objectMask->attachedHardware->datacenter;
@@ -97,17 +123,17 @@
97123
try {
98124
$ticket = $client->getObject();
99125
print_r($ticket);
100-
} catch (Exception $e) {
126+
} catch (\Exception $e) {
101127
die('Unable to retrieve ticket record: ' . $e->getMessage());
102128
}
103129

104130
// Now update the ticket.
105-
$update = new stdClass();
131+
$update = new \stdClass();
106132
$update->entry = 'Hello!';
107133

108134
try {
109135
$update = $client->addUpdate($update);
110136
echo "Updated ticket 123456. The new update's id is " . $update[0]->id . '.';
111-
} catch (Exception $e) {
137+
} catch (\Exception $e) {
112138
die('Unable to update ticket: ' . $e->getMessage());
113139
}

SoftLayer/Common/ObjectMask.class.php src/Common/ObjectMask.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
* POSSIBILITY OF SUCH DAMAGE.
2828
*/
2929

30+
namespace SoftLayer\Common;
31+
3032
/**
3133
* A simple object mask implementation.
3234
*
33-
* Use this class instead of stdClass when defining object masks in SoftLayer
35+
* Use this class instead of \stdClass when defining object masks in SoftLayer
3436
* API calls. This one is a bit easier to use. For example, to declare a new
35-
* object mask using stdClass enter:
37+
* object mask using \stdClass enter:
3638
*
3739
* $objectMask = new StdClass();
3840
* $objectMask->datacenter = new StdClass();
@@ -41,16 +43,16 @@
4143
* $objectMask->softwareComponents = new StdClass();
4244
* $objectMask->softwareComponents->passwords = new StdClass();
4345
*
44-
* Building an object mask using SoftLayer_ObjectMask is a bit easier to
46+
* Building an object mask using ObjectMask is a bit easier to
4547
* type:
4648
*
47-
* $objectMask = new SoftLayer_ObjectMask();
49+
* $objectMask = new ObjectMask();
4850
* $objectMask->datacenter;
4951
* $objectMask->serverRoom;
5052
* $objectMask->provisionDate;
5153
* $objectMask->sofwareComponents->passwords;
5254
*
53-
* Use SoftLayer_SoapClient::setObjectMask() to set these object masks before
55+
* Use SoapClient::setObjectMask() to set these object masks before
5456
* making your SoftLayer API calls.
5557
*
5658
* For more on object mask usage in the SoftLayer API please see
@@ -65,10 +67,10 @@
6567
* @author SoftLayer Technologies, Inc. <[email protected]>
6668
* @copyright Copyright (c) 2009 - 2010, Softlayer Technologies, Inc
6769
* @license http://sldn.softlayer.com/article/License
68-
* @see SoftLayer_SoapClient::setObjectMask()
69-
* @see SoftLayer_XmlrpcClient::setObjectMask()
70+
* @see SoapClient::setObjectMask()
71+
* @see XmlRpcClient::setObjectMask()
7072
*/
71-
class SoftLayer_ObjectMask
73+
class ObjectMask
7274
{
7375
/**
7476
* Define an object mask value
@@ -77,7 +79,7 @@ class SoftLayer_ObjectMask
7779
*/
7880
public function __get($var)
7981
{
80-
$this->{$var} = new SoftLayer_ObjectMask();
82+
$this->{$var} = new self();
8183

8284
return $this->{$var};
8385
}

0 commit comments

Comments
 (0)