Skip to content

Commit

Permalink
feat(php): Add PHP clients init + PHP search client (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
damcou authored Feb 4, 2022
1 parent 739121d commit b6800a9
Show file tree
Hide file tree
Showing 278 changed files with 99,947 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ runs:
echo "::set-output name=JAVA_CLIENT_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-java-2 | wc -l)"
echo "::set-output name=JAVA_TEMPLATE_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- templates/java | wc -l)"
echo "::set-output name=PHP_CLIENT_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- clients/algoliasearch-client-php | wc -l)"
echo "::set-output name=PHP_TEMPLATE_CHANGED::$(git diff --shortstat origin/${{ github.base_ref }}..HEAD -- templates/php | wc -l)"
- name: Compute specs matrix
id: spec-matrix
shell: bash
Expand Down Expand Up @@ -91,6 +94,23 @@ runs:
echo "::set-output name=MATRIX::$matrix"
echo "::set-output name=RUN_CLIENT::$run"
- name: Compute the PHP client build matrix
id: php-matrix
shell: bash
run: |
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 }}
matrix=$(./scripts/ci/create-client-matrix.sh php $base_changed ${{ github.base_ref }})
if [[ $matrix == '{"client":["no-run"]}' ]]; then
run="false"
else
run="true"
fi
echo "::set-output name=MATRIX::$matrix"
echo "::set-output name=RUN_CLIENT::$run"
outputs:
RUN_SPECS:
description: Whether to run specs or not
Expand All @@ -116,6 +136,14 @@ outputs:
description: Generate the matrix for the Java client
value: ${{ steps.java-matrix.outputs.MATRIX }}

RUN_PHP:
description: Whether to run php client or not
value: ${{ steps.php-matrix.outputs.RUN_CLIENT }}

PHP_MATRIX:
description: Generate the matrix for the PHP client
value: ${{ steps.php-matrix.outputs.MATRIX }}

RUN_CTS:
description: Determine if the `cts` job should run
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 }}
35 changes: 35 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
RUN_JAVA: ${{ steps.setup.outputs.RUN_JAVA }}
JAVA_MATRIX: ${{ steps.setup.outputs.JAVA_MATRIX }}

RUN_PHP: ${{ steps.setup.outputs.RUN_PHP }}
PHP_MATRIX: ${{ steps.setup.outputs.PHP_MATRIX }}

RUN_CTS: ${{ steps.setup.outputs.RUN_CTS }}

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

client_php:
runs-on: ubuntu-20.04
needs:
- setup
- specs
if: ${{ always() && needs.setup.outputs.RUN_PHP == 'true' }}
strategy:
matrix: ${{ fromJSON(needs.setup.outputs.PHP_MATRIX) }}
steps:
- uses: actions/checkout@v2

- name: Restore cache
uses: ./.github/actions/cache
with:
job: client
language: php
spec: ${{ matrix.client.name }}

- name: Generate ${{ matrix.client.name }} client
if: steps.cache.outputs.cache-hit != 'true'
run: yarn generate php ${{ matrix.client.name }}

- name: Check diff with pushed client
if: steps.cache.outputs.cache-hit != 'true'
run: exit $(git status --porcelain ${{ matrix.client.folder }} | wc -l)

- name: Build ${{ matrix.client.name }} client
if: steps.cache.outputs.cache-hit != 'true'
run: yarn build:clients php ${{ matrix.client.name }}

cts:
runs-on: ubuntu-20.04
needs:
- client_javascript
- client_java
- client_php

if: ${{ always() && needs.setup.outputs.RUN_CTS == 'true' }}
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ENV JAVA_HOME=/usr/lib/jvm/default-jvm
# Java formatter
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

# PHP dependencies
RUN apk add -U composer php8 php8-tokenizer

WORKDIR /app

CMD ["bash"]
13 changes: 13 additions & 0 deletions clients/algoliasearch-client-php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ref: https://github.com/github/gitignore/blob/master/Composer.gitignore

composer.phar
/vendor/

# php-cs-fixer cache
.php_cs.cache

# PHPUnit cache
.phpunit.result.cache

.openapi-generator/
composer.lock
16 changes: 16 additions & 0 deletions clients/algoliasearch-client-php/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

docs/**
test/**

.travis.yml
phpunit.xml.dist
README.md
git_push.sh
.php_cs

lib/HeaderSelector.php
63 changes: 63 additions & 0 deletions clients/algoliasearch-client-php/.php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

$config = new PhpCsFixer\Config();

return $config
->setUsingCache(true)
->setRules([
'array_syntax' => [ 'syntax' => 'short' ],
'blank_line_after_namespace' => false,
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => true,
'braces' => false,
'cast_spaces' => true,
'combine_consecutive_unsets' => true,
'echo_tag_syntax' => true,
'general_phpdoc_tag_rename' => true,
'mb_str_functions' => true,
'no_blank_lines_after_class_opening' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_short_bool_cast' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unreachable_default_argument_value' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'not_operator_with_space' => false,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_inline_tag_normalizer' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_tag_type' => true,
'protected_to_private' => true,
'@PSR2' => true,
'short_scalar_cast' => true,
'single_blank_line_at_eof' => false,
'single_blank_line_before_namespace' => true,
'single_quote' => true,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'strict_comparison' => true,
'strict_param' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline' => true,
'trim_array_spaces' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('test')
->exclude('tests')
->in(__DIR__)
);
44 changes: 44 additions & 0 deletions clients/algoliasearch-client-php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "algolia/algoliasearch-client-php",
"description": "API powering the features of Algolia.",
"keywords": [
"openapitools",
"openapi-generator",
"openapi",
"php",
"sdk",
"rest",
"api"
],
"homepage": "https://openapi-generator.tech",
"license": "unlicense",
"authors": [
{
"name": "OpenAPI-Generator contributors",
"homepage": "https://openapi-generator.tech"
}
],
"require": {
"php": "^7.3 || ^8.0",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^2.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5.0"
},
"autoload": {
"psr-4": { "Algolia\\AlgoliaSearch\\" : "lib/" },
"files": [
"lib/Http/Psr7/functions.php"
]
},
"autoload-dev": {
"psr-4": { "Algolia\\AlgoliaSearch\\Test\\" : "test/" }
}
}
105 changes: 105 additions & 0 deletions clients/algoliasearch-client-php/lib/Algolia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Algolia\AlgoliaSearch;

use Algolia\AlgoliaSearch\Cache\NullCacheDriver;
use Algolia\AlgoliaSearch\Http\HttpClientInterface;
use Algolia\AlgoliaSearch\Log\DebugLogger;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;

final class Algolia
{
const VERSION = '1.0.0';

/**
* Holds an instance of the simple cache repository (PSR-16).
*
* @var \Psr\SimpleCache\CacheInterface|null
*/
private static $cache;

/**
* Holds an instance of the logger (PSR-3).
*
* @var \Psr\Log\LoggerInterface|null
*/
private static $logger;

/**
* @var \Algolia\AlgoliaSearch\Http\HttpClientInterface
*/
private static $httpClient;

public static function isCacheEnabled()
{
if (null === self::$cache) {
return false;
}

return !self::getCache() instanceof NullCacheDriver;
}

/**
* Gets the cache instance.
*
* @return \Psr\SimpleCache\CacheInterface
*/
public static function getCache()
{
if (null === self::$cache) {
self::setCache(new NullCacheDriver());
}

return self::$cache;
}

/**
* Sets the cache instance.
*/
public static function setCache(CacheInterface $cache)
{
self::$cache = $cache;
}

/**
* Gets the logger instance.
*
* @return \Psr\Log\LoggerInterface
*/
public static function getLogger()
{
if (null === self::$logger) {
self::setLogger(new DebugLogger());
}

return self::$logger;
}

/**
* Sets the logger instance.
*/
public static function setLogger(LoggerInterface $logger)
{
self::$logger = $logger;
}

public static function getHttpClient()
{
if (null === self::$httpClient) {
self::setHttpClient(new \Algolia\AlgoliaSearch\Http\GuzzleHttpClient());
}

return self::$httpClient;
}

public static function setHttpClient(HttpClientInterface $httpClient)
{
self::$httpClient = $httpClient;
}

public static function resetHttpClient()
{
self::$httpClient = null;
}
}
Loading

0 comments on commit b6800a9

Please sign in to comment.