Skip to content

Commit

Permalink
chore(php): get file locations for types, errors, services, request w…
Browse files Browse the repository at this point in the history
…rappers (#4654)
  • Loading branch information
dsinghvi authored Sep 16, 2024
1 parent 2afa278 commit 0c1465c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
28 changes: 19 additions & 9 deletions generators/php/codegen/src/context/AbstractPhpGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import {
TypeId,
TypeDeclaration,
Subpackage,
SubpackageId
SubpackageId,
FernFilepath
} from "@fern-fern/ir-sdk/api";
import { BasePhpCustomConfigSchema } from "../custom-config/BasePhpCustomConfigSchema";
import { PhpProject } from "../project";
import { camelCase, upperFirst } from "lodash-es";
import { PhpTypeMapper } from "./PhpTypeMapper";
import { RelativeFilePath } from "@fern-api/fs-utils";

export interface FileLocation {
namespace: string;
directory: RelativeFilePath;
}

export abstract class AbstractPhpGeneratorContext<
CustomConfig extends BasePhpCustomConfigSchema
Expand Down Expand Up @@ -44,14 +51,6 @@ export abstract class AbstractPhpGeneratorContext<
return subpackage;
}

public getNamespaceForTypeId(typeId: TypeId): string {
const typeDeclaration = this.getTypeDeclarationOrThrow(typeId);
return [
this.getRootNamespace(),
...typeDeclaration.name.fernFilepath.packagePath.map((path) => path.pascalCase.safeName)
].join("\\");
}

public getClassName(name: Name): string {
return name.pascalCase.safeName;
}
Expand Down Expand Up @@ -118,4 +117,15 @@ export abstract class AbstractPhpGeneratorContext<
public abstract getCoreAsIsFiles(): string[];

public abstract getCoreTestAsIsFiles(): string[];

public abstract getLocationForTypeId(typeId: TypeId): FileLocation;

protected getFileLocation(filepath: FernFilepath, suffix?: string): FileLocation {
let parts = [this.getRootNamespace(), ...filepath.packagePath.map((path) => path.pascalCase.safeName)];
parts = suffix != null ? [...parts, suffix] : parts;
return {
namespace: parts.join("\\"),
directory: RelativeFilePath.of(parts.join("/"))
};
}
}
2 changes: 1 addition & 1 deletion generators/php/codegen/src/context/PhpTypeMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class PhpTypeMapper {
public convertToClassReference(declaredTypeName: { typeId: TypeId; name: Name }): ClassReference {
return new php.ClassReference({
name: this.context.getClassName(declaredTypeName.name),
namespace: this.context.getNamespaceForTypeId(declaredTypeName.typeId)
namespace: this.context.getLocationForTypeId(declaredTypeName.typeId).namespace
});
}

Expand Down
4 changes: 1 addition & 3 deletions generators/php/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { fromPairs } from "lodash-es";

export * from "./AsIs";
export { AbstractPhpGeneratorContext } from "./context/AbstractPhpGeneratorContext";
export { AbstractPhpGeneratorContext, type FileLocation } from "./context/AbstractPhpGeneratorContext";
export { AbstractPhpGeneratorCli } from "./cli/AbstractPhpGeneratorCli";
export { BasePhpCustomConfigSchema } from "./custom-config/BasePhpCustomConfigSchema";
export { PhpFile } from "./project/PhpFile";
Expand Down
8 changes: 7 additions & 1 deletion generators/php/model/src/ModelGeneratorContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { AbstractPhpGeneratorContext, AsIsFiles } from "@fern-api/php-codegen";
import { RelativeFilePath } from "@fern-api/fs-utils";
import { AbstractPhpGeneratorContext, AsIsFiles, FileLocation } from "@fern-api/php-codegen";
import { ModelCustomConfigSchema } from "./ModelCustomConfig";

export class ModelGeneratorContext extends AbstractPhpGeneratorContext<ModelCustomConfigSchema> {
public getRawAsIsFiles(): string[] {
return [AsIsFiles.GitIgnore, AsIsFiles.PhpStanNeon, AsIsFiles.PhpUnitXml];
}

public getLocationForTypeId(typeId: string): FileLocation {
const typeDeclaration = this.getTypeDeclarationOrThrow(typeId);
return this.getFileLocation(typeDeclaration.name.fernFilepath);
}

public getCoreAsIsFiles(): string[] {
return [];
}
Expand Down
1 change: 1 addition & 0 deletions generators/php/model/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TYPES_DIRECTORY = "Types";
35 changes: 33 additions & 2 deletions generators/php/sdk/src/SdkGeneratorContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpService, ServiceId } from "@fern-fern/ir-sdk/api";
import { AbstractPhpGeneratorContext } from "@fern-api/php-codegen";
import { DeclaredErrorName, HttpService, ServiceId } from "@fern-fern/ir-sdk/api";
import { AbstractPhpGeneratorContext, FileLocation } from "@fern-api/php-codegen";
import { GeneratorNotificationService } from "@fern-api/generator-commons";
import { FernGeneratorExec } from "@fern-fern/generator-exec-sdk";
import { IntermediateRepresentation } from "@fern-fern/ir-sdk/api";
Expand All @@ -8,6 +8,9 @@ import { AsIsFiles, php } from "@fern-api/php-codegen";
import { camelCase, upperFirst } from "lodash-es";
import { RawClient } from "./core/RawClient";
import { GuzzleClient } from "./external/GuzzleClient";
import { RelativeFilePath } from "@fern-api/fs-utils";
import { ErrorId, ErrorDeclaration } from "@fern-fern/ir-sdk/api";
import { TYPES_DIRECTORY, ERRORS_DIRECTORY } from "./constants";

export class SdkGeneratorContext extends AbstractPhpGeneratorContext<SdkCustomConfigSchema> {
public guzzleClient: GuzzleClient;
Expand All @@ -31,6 +34,14 @@ export class SdkGeneratorContext extends AbstractPhpGeneratorContext<SdkCustomCo
return service;
}

public getErrorDeclarationOrThrow(errorId: ErrorId): ErrorDeclaration {
const error = this.ir.errors[errorId];
if (error == null) {
throw new Error(`Error with id ${errorId} not found`);
}
return error;
}

public getRootClientClassName(): string {
if (this.customConfig["client-class-name"] != null) {
return this.customConfig["client-class-name"];
Expand Down Expand Up @@ -61,4 +72,24 @@ export class SdkGeneratorContext extends AbstractPhpGeneratorContext<SdkCustomCo
public getCoreTestAsIsFiles(): string[] {
return [AsIsFiles.RawClientTest];
}

public getLocationForTypeId(typeId: string): FileLocation {
const typeDeclaration = this.getTypeDeclarationOrThrow(typeId);
return this.getFileLocation(typeDeclaration.name.fernFilepath, TYPES_DIRECTORY);
}

public getLocationForHttpService(serviceId: string): FileLocation {
const httpService = this.getHttpServiceOrThrow(serviceId);
return this.getFileLocation(httpService.name.fernFilepath);
}

public getLocationForRequestWrapper(serviceId: string): FileLocation {
const httpService = this.getHttpServiceOrThrow(serviceId);
return this.getFileLocation(httpService.name.fernFilepath);
}

public getLocationForError(errorId: ErrorId): FileLocation {
const errorDeclaration = this.getErrorDeclarationOrThrow(errorId);
return this.getFileLocation(errorDeclaration.name.fernFilepath, ERRORS_DIRECTORY);
}
}
2 changes: 2 additions & 0 deletions generators/php/sdk/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const TYPES_DIRECTORY = "Types";
export const ERRORS_DIRECTORY = "Errors";

0 comments on commit 0c1465c

Please sign in to comment.