Skip to content

Commit 3d80010

Browse files
committed
init commit Sylius API PHP Client
0 parents  commit 3d80010

File tree

140 files changed

+8366
-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.

140 files changed

+8366
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bin
2+
vendor
3+
composer.lock
4+
docker-compose.yml
5+
tests/etc/parameters.yml
6+
phpspec.yml
7+
phpunit.xml
8+
.php_cs.cache

.php_cs.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRules(array(
5+
'@PSR2' => true,
6+
'linebreak_after_opening_tag' => true,
7+
'ordered_imports' => true,
8+
))
9+
->setFinder(
10+
PhpCsFixer\Finder::create()
11+
->name('*.php')
12+
->in(__DIR__ . '/src')
13+
);

.travis.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
8+
sudo: false
9+
10+
cache:
11+
directories:
12+
- $HOME/.composer/cache/files
13+
14+
before_install:
15+
- phpenv config-rm xdebug.ini
16+
- phpenv config-add travis.php.ini
17+
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
18+
- composer self-update --no-interaction
19+
20+
install:
21+
- composer require php-http/guzzle6-adapter
22+
- composer install --prefer-dist --no-interaction
23+
24+
script:
25+
- bin/php-cs-fixer fix --diff --dry-run --config=.php_cs.php -vvv
26+
- bin/phpspec run --no-interaction
27+
- php -d error_reporting="E_ALL" bin/phpunit -c phpunit.xml.dist
28+
29+
notifications:
30+
email: false

LICENCE.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The Open Software License version 3.0
2+
3+
Copyright (c) 2013-2017, Akeneo SAS. - Original Work
4+
Copyright (c) 2013-2020 - Diglin GmbH - Derivative Work
5+
6+
Full license is at: http://opensource.org/licenses/OSL-3.0

README.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# PHP Sylius PHP API client
2+
3+
A simple PHP client to use the [Sylius PHP API](https://docs.sylius.com/en/latest/api/).
4+
5+
Matrix compatibility:
6+
7+
| Sylius version(s) | API PHP Client version |CI status |
8+
|--------------------|-------------------------|--------------------------------------------------------------------------------------------------------------------------|
9+
| v1.6 | v1.0 |[![Build Status](https://travis-ci.org/akeneo/api-php-client.svg?branch=1.0)](https://travis-ci.org/akeneo/api-php-client)|
10+
| - | master |[![Build Status](https://travis-ci.org/akeneo/api-php-client.svg?branch=master)](https://travis-ci.org/akeneo/api-php-client)|
11+
12+
Note that our PHP client is backward compatible.
13+
For example, if your PIM is currently a v2.3, you can still use a 1.0 version of the PHP client. The new endpoints available in v2.3 will not be available in the v1.0 of the PHP client.
14+
15+
## Requirements
16+
17+
* PHP >= 5.6
18+
* Composer
19+
20+
## Installation
21+
22+
We use HTTPPlug as the HTTP client abstraction layer.
23+
In this example, we will use [Guzzle](https://github.com/guzzle/guzzle) v6 as the HTTP client implementation.
24+
25+
`api-php-client` uses [Composer](http://getcomposer.org).
26+
The first step to use `api-php-client` is to download composer:
27+
28+
```bash
29+
$ curl -s http://getcomposer.org/installer | php
30+
```
31+
32+
Then, run the following command to require the library:
33+
```bash
34+
$ php composer.phar require akeneo/api-php-client php-http/guzzle6-adapter
35+
```
36+
37+
If you want to use another HTTP client implementation, you can check [here](https://packagist.org/providers/php-http/client-implementation) the full list of HTTP client implementations.
38+
39+
## Documentation
40+
41+
Full documentation is available on the [API website](https://api.akeneo.com/php-client/introduction.html).
42+
43+
## Getting started
44+
45+
### Initialise the client
46+
You first need to initialise the client with your credentials client id/secret and with your user/password.
47+
48+
If you don't have any client id, let's take a look at [this page](https://api.akeneo.com/documentation/security.html#authentication) to create it.
49+
50+
```php
51+
<?php
52+
53+
require_once __DIR__ . '/vendor/autoload.php';
54+
55+
$clientBuilder = new \Akeneo\Pim\ApiClient\SyliusClientBuilder('http://localhost/');
56+
$client = $clientBuilder->buildAuthenticatedByPassword('client_id', 'secret', 'admin', 'admin');
57+
```
58+
59+
You can authenticate to the client with your token/refresh token as well.
60+
```php
61+
$client = $clientBuilder->buildAuthenticatedByToken('client_id', 'secret', 'token', 'refresh_token');
62+
```
63+
64+
You can authenticate to the client with a custom header, in this case dummy client_id, secret, token and refresh token.
65+
```php
66+
$client = $clientBuilder->buildAuthenticatedByHeader(['X-AUTH-TOKEN' => 'qwertzuiopasdfghjkléxcvbnm']);
67+
```
68+
69+
Getting the token and refresh token is as simple as:
70+
```php
71+
$client->getToken();
72+
$client->getRefreshToken();
73+
```
74+
75+
### Get a product
76+
77+
```php
78+
$product = $client->getProductApi()->get('top');
79+
echo $product['identifier']; // display "top"
80+
```
81+
82+
### Get a list of products
83+
84+
#### By getting pages
85+
86+
```php
87+
$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
88+
$searchBuilder->addFilter('enabled', '=', true);
89+
$searchFilters = $searchBuilder->getFilters();
90+
91+
$firstPage = $client->getProductApi()->listPerPage(50, true, ['search' => $searchFilters]);
92+
93+
echo $page->getCount();
94+
95+
foreach ($page->getItems() as $product) {
96+
// do your stuff here
97+
echo $product['identifier'];
98+
}
99+
100+
$nextPage = $page->getNextPage();
101+
102+
$firstPage = $nextPage->getPreviousPage();
103+
```
104+
105+
#### By getting a cursor
106+
107+
```php
108+
$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
109+
$searchBuilder->addFilter('enabled', '=', true);
110+
$searchFilters = $searchBuilder->getFilters();
111+
112+
$products = $client->getProductApi()->all(50, ['search' => $searchFilters]);
113+
foreach ($products as $product) {
114+
// do your stuff here
115+
echo $product['identifier'];
116+
}
117+
```
118+
119+
### Create a product
120+
121+
```php
122+
$client->getProductApi()->create('top', ['enabled' => true]);
123+
```
124+
125+
### Upsert a product
126+
127+
```php
128+
$client->getProductApi()->upsert('top', ['family' => 'tshirt']);
129+
```
130+
131+
### Upsert a list of of products
132+
133+
```php
134+
$client->getProductApi()->upsertList([
135+
[
136+
'identifier' => 'top',
137+
'family' => 'tshirt',
138+
],
139+
[
140+
'identifier' => 'cap',
141+
'categories' => ['hat'],
142+
],
143+
]);
144+
```
145+
146+
## Testing
147+
148+
Do note that you have to delete the `composer.lock` because Doctrine dependencies are loaded.
149+
These dependencies are different in function of the PHP version running `composer install`.
150+
151+
```
152+
cp docker-compose.yml.dist docker-compose.yml
153+
rm -rf composer.lock vendor/
154+
docker-compose run client_56 composer install
155+
docker-compose run client_56 bin/phpunit -c phpunit.xml.dist
156+
docker-compose run client_56 bin/phpspec run
157+
docker-compose run client_56 bin/php-cs-fixer fix --diff --dry-run --config=.php_cs.php -vvv
158+
```
159+
160+
## Support
161+
162+
The support of this client is made in best effort by our Akeneo team.
163+
164+
If you find a bug or want to submit an improvement, don't hesitate to raise an issue on Github.
165+
Also, you can ask questions and discuss about the PHP client with the community in the [Slack User Group](https://akeneopim-ug.slack.com/messages/web-api/).

composer.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "diglin/sylius-api-php-client",
3+
"description": "Sylius client for the API",
4+
"license": "OSL-3.0",
5+
"minimum-stability": "stable",
6+
"authors": [
7+
{
8+
"name": "Akeneo (Original Work)",
9+
"homepage": "https://www.akeneo.com"
10+
},
11+
{
12+
"name": "Diglin (Derivative Work)",
13+
"homepage": "https://www.diglin.com"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"Diglin\\Sylius\\ApiClient\\": "src/"
19+
}
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Diglin\\Sylius\\ApiClient\\tests\\": "tests/"
24+
}
25+
},
26+
"require": {
27+
"php": ">=7.1",
28+
"psr/http-message": "^1.0",
29+
"php-http/httplug": "^2.0",
30+
"php-http/message": "^1.7",
31+
"php-http/discovery": "^1.6",
32+
"php-http/message-factory": "^v1.0",
33+
"php-http/multipart-stream-builder": "^1.0",
34+
"php-http/client-implementation": "^1.0"
35+
},
36+
"require-dev": {
37+
"friendsofphp/php-cs-fixer": "^2.14",
38+
"phpunit/phpunit": "^5.7",
39+
"phpspec/phpspec": "^5.0",
40+
"symfony/yaml": "^4.2",
41+
"donatj/mock-webserver": "^2.0",
42+
"php-http/guzzle6-adapter": "^2.0"
43+
},
44+
"config": {
45+
"bin-dir": "bin"
46+
},
47+
"suggest": {
48+
"php-http/guzzle6-adapter": "In order to use Guzzle v6 as the HTTP client"
49+
}
50+
}

phpspec.yml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Rename this file in phpspec.yml to customize the phpspec configuration
2+
suites:
3+
SyliusClient:
4+
namespace: Diglin\Sylius\ApiClient
5+
psr4_prefix: Diglin\Sylius\ApiClient

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="true"
11+
stopOnFailure="false"
12+
syntaxCheck="false">
13+
14+
<testsuite>
15+
<directory>tests/</directory>
16+
</testsuite>
17+
</phpunit>

0 commit comments

Comments
 (0)