Skip to content

Commit b6800a9

Browse files
authored
feat(php): Add PHP clients init + PHP search client (#78)
1 parent 739121d commit b6800a9

File tree

278 files changed

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

278 files changed

+99947
-0
lines changed

.github/actions/setup/action.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ runs:
4040
echo "::set-output name=JAVA_CLIENT_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-java-2 | wc -l)"
4141
echo "::set-output name=JAVA_TEMPLATE_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- templates/java | wc -l)"
4242
43+
echo "::set-output name=PHP_CLIENT_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-php | wc -l)"
44+
echo "::set-output name=PHP_TEMPLATE_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- templates/php | wc -l)"
45+
4346
- name: Compute specs matrix
4447
id: spec-matrix
4548
shell: bash
@@ -91,6 +94,23 @@ runs:
9194
echo "::set-output name=MATRIX::$matrix"
9295
echo "::set-output name=RUN_CLIENT::$run"
9396
97+
- name: Compute the PHP client build matrix
98+
id: php-matrix
99+
shell: bash
100+
run: |
101+
base_changed=${{ github.ref == 'refs/heads/main' || steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.PHP_TEMPLATE_CHANGED > 0 }}
102+
103+
matrix=$(./scripts/ci/create-client-matrix.sh php $base_changed ${{ github.base_ref }})
104+
105+
if [[ $matrix == '{"client":["no-run"]}' ]]; then
106+
run="false"
107+
else
108+
run="true"
109+
fi
110+
111+
echo "::set-output name=MATRIX::$matrix"
112+
echo "::set-output name=RUN_CLIENT::$run"
113+
94114
outputs:
95115
RUN_SPECS:
96116
description: Whether to run specs or not
@@ -116,6 +136,14 @@ outputs:
116136
description: Generate the matrix for the Java client
117137
value: ${{ steps.java-matrix.outputs.MATRIX }}
118138

139+
RUN_PHP:
140+
description: Whether to run php client or not
141+
value: ${{ steps.php-matrix.outputs.RUN_CLIENT }}
142+
143+
PHP_MATRIX:
144+
description: Generate the matrix for the PHP client
145+
value: ${{ steps.php-matrix.outputs.MATRIX }}
146+
119147
RUN_CTS:
120148
description: Determine if the `cts` job should run
121149
value: ${{ github.ref_name == 'main' || steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.SPECS_CHANGED > 0 || steps.diff.outputs.TESTS_CHANGED > 0 || steps.diff.outputs.JS_CLIENT_CHANGED > 0 || steps.diff.outputs.JS_TEMPLATE_CHANGED > 0 || steps.diff.outputs.JAVA_CLIENT_CHANGED > 0 || steps.diff.outputs.JAVA_TEMPLATE_CHANGED > 0 }}

.github/workflows/check.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ jobs:
3636
RUN_JAVA: ${{ steps.setup.outputs.RUN_JAVA }}
3737
JAVA_MATRIX: ${{ steps.setup.outputs.JAVA_MATRIX }}
3838

39+
RUN_PHP: ${{ steps.setup.outputs.RUN_PHP }}
40+
PHP_MATRIX: ${{ steps.setup.outputs.PHP_MATRIX }}
41+
3942
RUN_CTS: ${{ steps.setup.outputs.RUN_CTS }}
4043

4144
specs:
@@ -134,11 +137,43 @@ jobs:
134137
if: steps.cache.outputs.cache-hit != 'true'
135138
run: yarn build:clients java ${{ matrix.client.name }}
136139

140+
client_php:
141+
runs-on: ubuntu-20.04
142+
needs:
143+
- setup
144+
- specs
145+
if: ${{ always() && needs.setup.outputs.RUN_PHP == 'true' }}
146+
strategy:
147+
matrix: ${{ fromJSON(needs.setup.outputs.PHP_MATRIX) }}
148+
steps:
149+
- uses: actions/checkout@v2
150+
151+
- name: Restore cache
152+
uses: ./.github/actions/cache
153+
with:
154+
job: client
155+
language: php
156+
spec: ${{ matrix.client.name }}
157+
158+
- name: Generate ${{ matrix.client.name }} client
159+
if: steps.cache.outputs.cache-hit != 'true'
160+
run: yarn generate php ${{ matrix.client.name }}
161+
162+
- name: Check diff with pushed client
163+
if: steps.cache.outputs.cache-hit != 'true'
164+
run: exit $(git status --porcelain ${{ matrix.client.folder }} | wc -l)
165+
166+
- name: Build ${{ matrix.client.name }} client
167+
if: steps.cache.outputs.cache-hit != 'true'
168+
run: yarn build:clients php ${{ matrix.client.name }}
169+
137170
cts:
138171
runs-on: ubuntu-20.04
139172
needs:
140173
- client_javascript
141174
- client_java
175+
- client_php
176+
142177
if: ${{ always() && needs.setup.outputs.RUN_CTS == 'true' }}
143178
steps:
144179
- uses: actions/checkout@v2

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ ENV JAVA_HOME=/usr/lib/jvm/default-jvm
1010
# Java formatter
1111
ADD https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar /tmp/java-formatter.jar
1212

13+
# PHP dependencies
14+
RUN apk add -U composer php8 php8-tokenizer
15+
1316
WORKDIR /app
1417

1518
CMD ["bash"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ref: https://github.com/github/gitignore/blob/master/Composer.gitignore
2+
3+
composer.phar
4+
/vendor/
5+
6+
# php-cs-fixer cache
7+
.php_cs.cache
8+
9+
# PHPUnit cache
10+
.phpunit.result.cache
11+
12+
.openapi-generator/
13+
composer.lock
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
docs/**
8+
test/**
9+
10+
.travis.yml
11+
phpunit.xml.dist
12+
README.md
13+
git_push.sh
14+
.php_cs
15+
16+
lib/HeaderSelector.php
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
$config = new PhpCsFixer\Config();
4+
5+
return $config
6+
->setUsingCache(true)
7+
->setRules([
8+
'array_syntax' => [ 'syntax' => 'short' ],
9+
'blank_line_after_namespace' => false,
10+
'blank_line_after_opening_tag' => true,
11+
'blank_line_before_statement' => true,
12+
'braces' => false,
13+
'cast_spaces' => true,
14+
'combine_consecutive_unsets' => true,
15+
'echo_tag_syntax' => true,
16+
'general_phpdoc_tag_rename' => true,
17+
'mb_str_functions' => true,
18+
'no_blank_lines_after_class_opening' => true,
19+
'no_empty_phpdoc' => true,
20+
'no_empty_statement' => true,
21+
'no_extra_blank_lines' => true,
22+
'no_multiline_whitespace_around_double_arrow' => true,
23+
'no_short_bool_cast' => true,
24+
'no_trailing_whitespace' => true,
25+
'no_trailing_whitespace_in_comment' => true,
26+
'no_unneeded_control_parentheses' => true,
27+
'no_unreachable_default_argument_value' => true,
28+
'no_unused_imports' => true,
29+
'no_useless_else' => true,
30+
'no_useless_return' => true,
31+
'no_whitespace_before_comma_in_array' => true,
32+
'no_whitespace_in_blank_line' => true,
33+
'normalize_index_brace' => true,
34+
'not_operator_with_space' => false,
35+
'object_operator_without_whitespace' => true,
36+
'ordered_imports' => true,
37+
'phpdoc_annotation_without_dot' => true,
38+
'phpdoc_inline_tag_normalizer' => true,
39+
'phpdoc_order' => true,
40+
'phpdoc_scalar' => true,
41+
'phpdoc_separation' => true,
42+
'phpdoc_single_line_var_spacing' => true,
43+
'phpdoc_tag_type' => true,
44+
'protected_to_private' => true,
45+
'@PSR2' => true,
46+
'short_scalar_cast' => true,
47+
'single_blank_line_at_eof' => false,
48+
'single_blank_line_before_namespace' => true,
49+
'single_quote' => true,
50+
'space_after_semicolon' => true,
51+
'standardize_not_equals' => true,
52+
'strict_comparison' => true,
53+
'strict_param' => true,
54+
'ternary_operator_spaces' => true,
55+
'trailing_comma_in_multiline' => true,
56+
'trim_array_spaces' => true,
57+
])
58+
->setFinder(
59+
PhpCsFixer\Finder::create()
60+
->exclude('test')
61+
->exclude('tests')
62+
->in(__DIR__)
63+
);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "algolia/algoliasearch-client-php",
3+
"description": "API powering the features of Algolia.",
4+
"keywords": [
5+
"openapitools",
6+
"openapi-generator",
7+
"openapi",
8+
"php",
9+
"sdk",
10+
"rest",
11+
"api"
12+
],
13+
"homepage": "https://openapi-generator.tech",
14+
"license": "unlicense",
15+
"authors": [
16+
{
17+
"name": "OpenAPI-Generator contributors",
18+
"homepage": "https://openapi-generator.tech"
19+
}
20+
],
21+
"require": {
22+
"php": "^7.3 || ^8.0",
23+
"ext-curl": "*",
24+
"ext-json": "*",
25+
"ext-mbstring": "*",
26+
"guzzlehttp/guzzle": "^7.3",
27+
"guzzlehttp/psr7": "^2.0",
28+
"psr/http-message": "^1.0",
29+
"psr/log": "^1.0 || ^2.0 || ^3.0",
30+
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
31+
},
32+
"require-dev": {
33+
"friendsofphp/php-cs-fixer": "^3.5.0"
34+
},
35+
"autoload": {
36+
"psr-4": { "Algolia\\AlgoliaSearch\\" : "lib/" },
37+
"files": [
38+
"lib/Http/Psr7/functions.php"
39+
]
40+
},
41+
"autoload-dev": {
42+
"psr-4": { "Algolia\\AlgoliaSearch\\Test\\" : "test/" }
43+
}
44+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch;
4+
5+
use Algolia\AlgoliaSearch\Cache\NullCacheDriver;
6+
use Algolia\AlgoliaSearch\Http\HttpClientInterface;
7+
use Algolia\AlgoliaSearch\Log\DebugLogger;
8+
use Psr\Log\LoggerInterface;
9+
use Psr\SimpleCache\CacheInterface;
10+
11+
final class Algolia
12+
{
13+
const VERSION = '1.0.0';
14+
15+
/**
16+
* Holds an instance of the simple cache repository (PSR-16).
17+
*
18+
* @var \Psr\SimpleCache\CacheInterface|null
19+
*/
20+
private static $cache;
21+
22+
/**
23+
* Holds an instance of the logger (PSR-3).
24+
*
25+
* @var \Psr\Log\LoggerInterface|null
26+
*/
27+
private static $logger;
28+
29+
/**
30+
* @var \Algolia\AlgoliaSearch\Http\HttpClientInterface
31+
*/
32+
private static $httpClient;
33+
34+
public static function isCacheEnabled()
35+
{
36+
if (null === self::$cache) {
37+
return false;
38+
}
39+
40+
return !self::getCache() instanceof NullCacheDriver;
41+
}
42+
43+
/**
44+
* Gets the cache instance.
45+
*
46+
* @return \Psr\SimpleCache\CacheInterface
47+
*/
48+
public static function getCache()
49+
{
50+
if (null === self::$cache) {
51+
self::setCache(new NullCacheDriver());
52+
}
53+
54+
return self::$cache;
55+
}
56+
57+
/**
58+
* Sets the cache instance.
59+
*/
60+
public static function setCache(CacheInterface $cache)
61+
{
62+
self::$cache = $cache;
63+
}
64+
65+
/**
66+
* Gets the logger instance.
67+
*
68+
* @return \Psr\Log\LoggerInterface
69+
*/
70+
public static function getLogger()
71+
{
72+
if (null === self::$logger) {
73+
self::setLogger(new DebugLogger());
74+
}
75+
76+
return self::$logger;
77+
}
78+
79+
/**
80+
* Sets the logger instance.
81+
*/
82+
public static function setLogger(LoggerInterface $logger)
83+
{
84+
self::$logger = $logger;
85+
}
86+
87+
public static function getHttpClient()
88+
{
89+
if (null === self::$httpClient) {
90+
self::setHttpClient(new \Algolia\AlgoliaSearch\Http\GuzzleHttpClient());
91+
}
92+
93+
return self::$httpClient;
94+
}
95+
96+
public static function setHttpClient(HttpClientInterface $httpClient)
97+
{
98+
self::$httpClient = $httpClient;
99+
}
100+
101+
public static function resetHttpClient()
102+
{
103+
self::$httpClient = null;
104+
}
105+
}

0 commit comments

Comments
 (0)