Skip to content

Commit

Permalink
feat(php): Add support for global headers (#5985)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Feb 13, 2025
1 parent 4f46dd8 commit f2227ad
Show file tree
Hide file tree
Showing 82 changed files with 597 additions and 384 deletions.
18 changes: 9 additions & 9 deletions generators/php/base/src/context/PhpTypeMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export class PhpTypeMapper {
}
}

public convertLiteral({ literal }: { literal: Literal }): php.Type {
switch (literal.type) {
case "boolean":
return php.Type.bool();
case "string":
return php.Type.string();
}
}

public convertToClassReference(declaredTypeName: { typeId: TypeId; name: Name }): php.ClassReference {
return new php.ClassReference({
name: this.context.getClassName(declaredTypeName.name),
Expand Down Expand Up @@ -114,15 +123,6 @@ export class PhpTypeMapper {
});
}

private convertLiteral({ literal }: { literal: Literal }): php.Type {
switch (literal.type) {
case "boolean":
return php.Type.bool();
case "string":
return php.Type.string();
}
}

private convertNamed({ named, preserveEnums }: { named: DeclaredTypeName; preserveEnums: boolean }): php.Type {
const classReference = this.convertToClassReference(named);
const typeDeclaration = this.context.getTypeDeclarationOrThrow(named.typeId);
Expand Down
53 changes: 45 additions & 8 deletions generators/php/sdk/src/root-client/RootClientGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ interface ConstructorParameter {
interface LiteralParameter {
name: string;
value: Literal;
docs?: string;
header?: HeaderInfo;
environmentVariable?: string;
}

interface HeaderInfo {
Expand Down Expand Up @@ -128,6 +131,15 @@ export class RootClientGenerator extends FileGenerator<PhpFile, SdkCustomConfigS
})
);
}
for (const param of constructorParameters.literal) {
parameters.push(
php.parameter({
name: param.name,
type: this.getLiteralRootClientParameterType({ literal: param.value }),
docs: param.docs
})
);
}

parameters.push(
php.parameter({
Expand Down Expand Up @@ -157,10 +169,12 @@ export class RootClientGenerator extends FileGenerator<PhpFile, SdkCustomConfigS
}

for (const param of constructorParameters.literal) {
headerEntries.push({
key: php.codeblock(`'${param.name}'`),
value: php.codeblock(this.context.getLiteralAsString(param.value))
});
if (param.header != null) {
headerEntries.push({
key: php.codeblock(`'${param.header.name}'`),
value: php.codeblock(this.context.getLiteralAsString(param.value))
});
}
}

const platformHeaders = this.context.ir.sdkConfig.platformHeaders;
Expand Down Expand Up @@ -222,6 +236,16 @@ export class RootClientGenerator extends FileGenerator<PhpFile, SdkCustomConfigS
writer.endControlFlow();
}
}
for (const param of constructorParameters.literal) {
if (param.header != null) {
writer.controlFlow("if", php.codeblock(`$${param.name} != null`));
writer.write(`$defaultHeaders['${param.header.name}'] = `);
writer.writeNodeStatement(
this.getHeaderValue({ prefix: param.header.prefix, parameterName: param.name })
);
writer.endControlFlow();
}
}
writer.writeLine();

writer.writeNodeStatement(
Expand Down Expand Up @@ -329,9 +353,11 @@ export class RootClientGenerator extends FileGenerator<PhpFile, SdkCustomConfigS
const literal = this.context.maybeLiteral(param.typeReference);
if (literal != null) {
literalParameters.push({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
name: param.header!.name,
value: literal
name: param.name,
value: literal,
docs: param.docs,
header: param.header,
environmentVariable: param.environmentVariable
});
continue;
}
Expand Down Expand Up @@ -437,7 +463,7 @@ export class RootClientGenerator extends FileGenerator<PhpFile, SdkCustomConfigS

private getParameterForHeader(header: HttpHeader): ConstructorParameter {
return {
name: `$${header.name.name.camelCase.safeName}`,
name: this.context.getParameterName(header.name.name),
header: {
name: header.name.wireValue
},
Expand Down Expand Up @@ -473,6 +499,17 @@ export class RootClientGenerator extends FileGenerator<PhpFile, SdkCustomConfigS
: typeReference;
}

private getLiteralRootClientParameterType({ literal }: { literal: Literal }): php.Type {
switch (literal.type) {
case "string":
return php.Type.optional(php.Type.string());
case "boolean":
return php.Type.optional(php.Type.bool());
default:
assertNever(literal);
}
}

private getAuthParameterDocs({ docs, name }: { docs: string | undefined; name: string }): string {
return docs ?? `The ${name} to use for authentication.`;
}
Expand Down
7 changes: 7 additions & 0 deletions generators/php/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
- version: 0.13.2
changelogEntry:
- type: fix
summary: >-
Fix a bug where literal global headers could not be overridden in the root client constructor.
irVersion: 55

- version: 0.13.1
changelogEntry:
- type: fix
Expand Down
7 changes: 6 additions & 1 deletion seed/php-sdk/auth-environment-variables/src/SeedClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f2227ad

Please sign in to comment.