Skip to content

feat(php): Add all config classes generation #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/generation.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ module.exports = {
'!clients/algoliasearch-client-php/lib/RequestOptions/**',
'!clients/algoliasearch-client-php/lib/RetryStrategy/**',
'!clients/algoliasearch-client-php/lib/Support/**',
'!clients/algoliasearch-client-php/lib/Configuration/**',
'clients/algoliasearch-client-php/lib/Configuration/Configuration.php',
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,29 @@ public String getName() {
return "algolia-php";
}

public String getClientName(String client) {
return Utils.createClientName(client, "php");
}

@Override
public void processOpts() {
// generator specific options
String client = (String) additionalProperties.get("client");
setApiNameSuffix(Utils.API_SUFFIX);
setParameterNamingConvention("camelCase");
additionalProperties.put("modelPackage", "Model\\" + Utils.createClientName(client, "php"));
additionalProperties.put("modelPackage", "Model\\" + getClientName(client));
additionalProperties.put("invokerPackage", "Algolia\\AlgoliaSearch");
additionalProperties.put("clientName", getClientName(client));

super.processOpts();

// Remove base template as we want to change its path
supportingFiles.removeIf(file -> file.getTemplateFile().equals("Configuration.mustache"));

supportingFiles.add(new SupportingFile("Configuration.mustache", "lib/Configuration", "Configuration.php"));
supportingFiles.add(new SupportingFile("ConfigWithRegion.mustache", "lib/Configuration", "ConfigWithRegion.php"));

supportingFiles.add(new SupportingFile("client_config.mustache", "lib/Configuration", getClientName(client) + "Config.php"));

setDefaultGeneratorOptions(client);
try {
Expand All @@ -52,7 +60,7 @@ public void setDefaultGeneratorOptions(String client) {
additionalProperties.put("useCache", true);
}
additionalProperties.put("isSearchClient", client.equals("search"));
additionalProperties.put("configClassname", Utils.createClientName(client, "php") + "Config");
additionalProperties.put("configClassname", getClientName(client) + "Config");
}

public String getComposerPackageName() {
Expand Down
39 changes: 39 additions & 0 deletions templates/php/ConfigWithRegion.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace {{invokerPackage}}\Configuration;

use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;

abstract class ConfigWithRegion extends Configuration
{
public static function create(
$appId,
$apiKey,
$region = null,
$allowedRegions = null
) {
if (
$region !== null &&
$allowedRegions !== null &&
!in_array($region, $allowedRegions, true)
) {
throw new AlgoliaException(
'`region` must be one of the following: ' .
implode(', ', $allowedRegions)
);
}

$config = [
'appId' => $appId,
'apiKey' => $apiKey,
'region' => $region,
];

return new static($config);
}

public function getRegion()
{
return $this->config['region'];
}
}
48 changes: 48 additions & 0 deletions templates/php/client_config.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace {{invokerPackage}}\Configuration;

class {{configClassname}} extends {{#hasRegionalHost}}ConfigWithRegion{{/hasRegionalHost}}{{^hasRegionalHost}}Configuration{{/hasRegionalHost}}
{
protected $clientName = '{{clientName}}';
{{#isSearchClient}}private $defaultWaitTaskTimeBeforeRetry = 5000; // 5 sec in milliseconds
private $defaultMaxRetries = 50;

public static function create($appId, $apiKey)
{
$config = [
'appId' => $appId,
'apiKey' => $apiKey,
];

return new static($config);
}

public function getDefaultConfiguration()
{
return [
'appId' => '',
'apiKey' => '',
'hosts' => null,
'readTimeout' => $this->defaultReadTimeout,
'writeTimeout' => $this->defaultWriteTimeout,
'connectTimeout' => $this->defaultConnectTimeout,
'waitTaskTimeBeforeRetry' => $this->defaultWaitTaskTimeBeforeRetry,
'defaultMaxRetries' => $this->defaultMaxRetries,
'defaultHeaders' => [],
'defaultForwardToReplicas' => null,
'batchSize' => 1000,
];
}

public function getWaitTaskTimeBeforeRetry()
{
return $this->config['waitTaskTimeBeforeRetry'];
}

public function getDefaultMaxRetries()
{
return $this->config['defaultMaxRetries'];
}
{{/isSearchClient}}
}