Skip to content

Commit e4ede83

Browse files
committed
Initial Commit
0 parents  commit e4ede83

File tree

946 files changed

+101298
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

946 files changed

+101298
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 ShopifyExtras.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Shopify API Wrapper
2+
By [ShopifyExtras.com](http://www.shopifyextras.com) - 24/7 Shopify Support - Bugs Resolved Same Day
3+
4+
### Installing via Composer
5+
The recommended way to install the wrapper is through
6+
[Composer](http://getcomposer.org).
7+
8+
```bash
9+
# Install Composer
10+
curl -sS https://getcomposer.org/installer | php
11+
```
12+
13+
Next, run the Composer command to install the latest stable version of the wrapper:
14+
15+
```bash
16+
composer require shopifyextras/shopify-api-wrapper
17+
```
18+
After installing, you need to require Composer's autoloader:
19+
20+
```php
21+
require 'vendor/autoload.php';
22+
```
23+
24+
### Getting Started
25+
26+
First you will need to initialise the client like this:
27+
28+
```
29+
$client = Shopify::settings(array(
30+
"shopUrl" => $shopUrl,
31+
"X-Shopify-Access-Token" => $accessToken
32+
));
33+
```
34+
35+
Then you can begin making requests:
36+
```
37+
// Get a list of all products.
38+
$client->getProducts();
39+
40+
// Get a list of all orders.
41+
$client->getOrders();
42+
43+
// Get a specific product.
44+
$client->getProduct(array("id", $product_id));
45+
```
46+
47+
### Bugs & Issues
48+
If you spot any bugs, please report it using the issue tracker. If you would like to contribute to the project please feel free to make your amends and submit a pull request.
49+
50+
### Professional Services
51+
Unfortunately we are unable to provide free technical support for the wrapper. If you require this kind of help then please contact us by emailing [[email protected]](mailto:[email protected]).

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "ShopifyExtras/Shopify-PHP-API-Wrapper",
3+
"description": "Shopify PHP (Guzzle) API Wrapper",
4+
"type": "library",
5+
"license": "MIT",
6+
"homepage": "http://www.shopifyextras.com",
7+
"authors": [
8+
{
9+
"name": "Andrew Cargill",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require-dev": {
14+
"phpunit/phpunit": "~4.2"
15+
},
16+
"require": {
17+
"php": ">=5.4",
18+
"guzzlehttp/guzzle": "~5.0.3",
19+
"guzzlehttp/guzzle-services": "~0.4"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Shopify\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Shopify\\Tests\\": "tests/"
29+
}
30+
},
31+
"minimum-stability": "stable"
32+
}

src/Client.php

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
<?php namespace Shopify;
2+
3+
use Monolog\Logger;
4+
use Monolog\Handler\StreamHandler;
5+
6+
use GuzzleHttp\Client as BaseClient;
7+
use GuzzleHttp\Command\Guzzle\GuzzleClient;
8+
9+
class Client
10+
{
11+
12+
/**
13+
* Guzzle service description
14+
*
15+
* @var \Shopify\Description
16+
*/
17+
private static $description;
18+
19+
20+
/**
21+
* Guzzle base client
22+
*
23+
* @var \GuzzleHttp\Client
24+
*/
25+
private $baseClient;
26+
27+
28+
/**
29+
* Adapter for Guzzle base client
30+
*
31+
* @var \GuzzleHttp\Adapter\AdapterInterface
32+
*/
33+
private $baseClientAdapter;
34+
35+
36+
/**
37+
* Api client services
38+
*
39+
* @var \GuzzleHttp\Command\Guzzle\GuzzleClient
40+
*/
41+
private $serviceClient;
42+
43+
44+
/**
45+
* Shopify client config settings
46+
*
47+
* @var array
48+
*/
49+
private $settings;
50+
51+
52+
/**
53+
* Request header items
54+
*
55+
* @var array
56+
*/
57+
private $globalParams = [
58+
"shopUrl" => [
59+
"type" => "string",
60+
"location" => "uri",
61+
"required" => true,
62+
],
63+
"account_token" => [
64+
"type" => "string",
65+
"location" => "header",
66+
"required" => false,
67+
"sentAs" => "X-Shopify-Access-Token",
68+
],
69+
"Content-Type" => [
70+
"type" => "string",
71+
"location" => "header",
72+
"required" => false,
73+
"sentAs" => "application/json"
74+
],
75+
"X-Shopify-Access-Token" => [
76+
"type" => "string",
77+
"location" => "header",
78+
"description" => "The Auth Token."
79+
]
80+
];
81+
82+
/**
83+
* Create a new GuzzleClient Service, ability to use the client
84+
* without setting properties on instantiation.
85+
*
86+
* @param array $attributes
87+
* @return void
88+
*/
89+
public function __construct(array $settings = array())
90+
{
91+
$this->settings = $settings;
92+
}
93+
94+
95+
/**
96+
* Merge additional settings with existing and save. Overrides
97+
* existing settings as well.
98+
*
99+
* @param array $settings
100+
* @return static
101+
*/
102+
public function settings(array $settings)
103+
{
104+
$this->settings = array_merge($this->settings, $settings);
105+
if ($this->serviceClient) $this->buildClient();
106+
return $this;
107+
}
108+
109+
110+
/**
111+
* Load resource configuration file and return array.
112+
*
113+
* @param string $name
114+
* @return array
115+
*/
116+
private function loadResource($name)
117+
{
118+
return require __DIR__.'/resources/'.$name.'.php';
119+
}
120+
121+
122+
/**
123+
* Build new service client from descriptions.
124+
*
125+
* @return void
126+
*/
127+
private function buildClient()
128+
{
129+
$client = $this->getBaseClient();
130+
131+
if (!static::$description) {
132+
$this->reloadDescription();
133+
}
134+
135+
$this->serviceClient = new GuzzleClient(
136+
$client,
137+
static::$description,
138+
['emitter' => $this->baseClient->getEmitter()]
139+
);
140+
}
141+
142+
/**
143+
* Retrieve Guzzle base client.
144+
*
145+
* @return \GuzzleHttp\Client
146+
*/
147+
private function getBaseClient()
148+
{
149+
return $this->baseClient ?: $this->baseClient = $this->loadBaseClient();
150+
}
151+
152+
153+
/**
154+
* Set adapter and create Guzzle base client.
155+
*
156+
* @return \GuzzleHttp\Client
157+
*/
158+
private function loadBaseClient(array $settings = [])
159+
{
160+
if ($this->baseClientAdapter)
161+
$settings['adapter'] = $this->baseClientAdapter;
162+
163+
return $this->baseClient = new BaseClient($settings);
164+
}
165+
166+
167+
/**
168+
* Description works tricky as a static
169+
* property, reload as a needed.
170+
*
171+
* @return void
172+
*/
173+
private function reloadDescription()
174+
{
175+
static::$description = new Description($this->loadConfig());
176+
}
177+
178+
179+
/**
180+
* Load configuration file and parse resources.
181+
*
182+
* @return array
183+
*/
184+
private function loadConfig()
185+
{
186+
$description = $this->loadResource('service-config');
187+
188+
// initial description building, use api info and build base url
189+
$description = $description + [
190+
'baseUrl' => 'https://'.$this->settings['shopUrl'],
191+
'operations' => [],
192+
'models' => []
193+
];
194+
195+
// process each of the service description resources defined
196+
foreach ($description['services'] as $serviceName) {
197+
198+
$service = $this->loadResource($serviceName);
199+
$description = $this->loadServiceDescription($service, $description);
200+
201+
}
202+
203+
// dead weight now, clean it up
204+
unset($description['services']);
205+
return $description;
206+
}
207+
208+
209+
/**
210+
* Load service description from resource, add global
211+
* parameters to operations. Operations and models
212+
* added to full description.
213+
*
214+
* @param array $service
215+
* @param array $description
216+
* @return array
217+
*/
218+
private function loadServiceDescription(array $service, array $description)
219+
{
220+
foreach ($service as $section => $set) {
221+
if ($section == 'operations') {
222+
// add global parameters to the operation parameters
223+
foreach ($set as &$op)
224+
$op['parameters'] = isset($op['parameters'])
225+
? $op['parameters'] + $this->globalParams
226+
: $this->globalParams;
227+
}
228+
$description[$section] = $description[$section] + $set;
229+
}
230+
return $description;
231+
}
232+
233+
234+
public function __call($method, $parameters)
235+
{
236+
if (!$this->serviceClient) $this->buildClient();
237+
238+
// gather parameters to pass to service definitions
239+
$settings = $this->settings;
240+
241+
// merge client settings/parameters and method parameters
242+
$parameters[0] = isset($parameters[0])
243+
? $parameters[0] + $settings
244+
: $settings;
245+
246+
$response = call_user_func_array([$this->serviceClient, $method], $parameters);
247+
248+
return $response;
249+
250+
}
251+
252+
}

src/Description.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Shopify;
2+
3+
use GuzzleHttp\Command\Guzzle\Description as GuzzleDescription;
4+
5+
class Description extends GuzzleDescription
6+
{
7+
/**
8+
*
9+
* @param string $url
10+
* @return void
11+
*/
12+
public function setBaseUrl($url)
13+
{
14+
$this->baseUrl = $url;
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace Shopify\Exception;
2+
3+
class UnauthorizedException extends \OutOfBoundsException {}

0 commit comments

Comments
 (0)