Skip to content

Commit

Permalink
Configurable namespace root in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jun 17, 2024
1 parent d2b8276 commit 8b5e244
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .config/ci/php/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
parameters:
level: 9 # https://phpstan.org/user-guide/rule-levels
paths:
- ../../../generated/Types
- ../../../generated/Runtime
- ../../../generated/src

# ignoreErrors:
# - identifier: enum.duplicateValue # most likely a schema problem
4 changes: 3 additions & 1 deletion config/foundation_sdk.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
kind_registry_version: 'v10.4.x'
grafana_version: 'v10.4.x'
go_package_root: 'github.com/grafana/cog/generated'
php_namespace_root: 'Grafana\Foundation'
java_package_path: 'com.grafana.foundation'

inputs:
Expand Down Expand Up @@ -83,7 +84,8 @@ output:
package_root: '%go_package_root%'
- jsonschema: {}
- openapi: {}
- php: {}
- php:
namespace_root: '%php_namespace_root%'
- python:
path_prefix: grafana_foundation_sdk
- typescript: {}
Expand Down
3 changes: 3 additions & 0 deletions internal/codegen/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func (outputLanguage *OutputLanguage) interpolateParameters(interpolator Paramet
if outputLanguage.Go != nil {
outputLanguage.Go.InterpolateParameters(interpolator)
}
if outputLanguage.PHP != nil {
outputLanguage.PHP.InterpolateParameters(interpolator)
}
if outputLanguage.Python != nil {
outputLanguage.Python.InterpolateParameters(interpolator)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/jennies/php/add_typehints_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
var _ compiler.Pass = (*AddTypehintsComments)(nil)

type AddTypehintsComments struct {
config Config
hinter *typehints
}

func (pass *AddTypehintsComments) Process(schemas []*ast.Schema) ([]*ast.Schema, error) {
pass.hinter = &typehints{}
pass.hinter = &typehints{
config: pass.config,
}

visitor := &compiler.Visitor{
OnStructField: pass.processStructField,
Expand Down
20 changes: 17 additions & 3 deletions internal/jennies/php/jennies.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ const LanguageRef = "php"

type Config struct {
debug bool

NamespaceRoot string `yaml:"namespace_root"`
}

func (config *Config) InterpolateParameters(interpolator func(input string) string) {
config.NamespaceRoot = interpolator(config.NamespaceRoot)
}

func (config Config) fullNamespace(typeName string) string {
return config.NamespaceRoot + "\\" + typeName
}

func (config Config) fullNamespaceRef(typeName string) string {
return "\\" + config.fullNamespace(typeName)
}

func (config Config) MergeWithGlobal(global languages.Config) Config {
Expand Down Expand Up @@ -42,8 +56,8 @@ func (language *Language) Jennies(globalConfig languages.Config) *codejen.JennyL
return LanguageRef
})
jenny.AppendOneToMany(
VariantsPlugins{},
common.If[languages.Context](globalConfig.Types, RawTypes{Config: config}),
VariantsPlugins{config: config},
common.If[languages.Context](globalConfig.Types, RawTypes{config: config}),
)
jenny.AddPostprocessors(common.GeneratedCommentHeader(globalConfig))

Expand All @@ -63,7 +77,7 @@ func (language *Language) CompilerPasses() compiler.Passes {
&compiler.AnonymousStructsToNamed{},
&compiler.DisjunctionInferMapping{},
&compiler.UndiscriminatedDisjunctionToAny{},
&AddTypehintsComments{},
&AddTypehintsComments{config: language.config},
}
}

Expand Down
11 changes: 6 additions & 5 deletions internal/jennies/php/rawtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type RawTypes struct {
Config Config
config Config

typeFormatter *typeFormatter
}
Expand Down Expand Up @@ -59,12 +59,12 @@ func (jenny RawTypes) generateSchema(context languages.Context, schema *ast.Sche
func (jenny RawTypes) formatObject(context languages.Context, schema *ast.Schema, def ast.Object) (codejen.File, error) {
var buffer strings.Builder

jenny.typeFormatter = defaultTypeFormatter(jenny.Config, context)
jenny.typeFormatter = defaultTypeFormatter(jenny.config, context)

defName := formatObjectName(def.Name)

comments := def.Comments
if jenny.Config.debug {
if jenny.config.debug {
passesTrail := tools.Map(def.PassesTrail, func(trail string) string {
return fmt.Sprintf("Modified by compiler pass '%s'", trail)
})
Expand Down Expand Up @@ -95,7 +95,7 @@ func (jenny RawTypes) formatObject(context languages.Context, schema *ast.Schema
case ast.KindStruct:
variant := ""
if def.Type.ImplementsVariant() {
variant = " implements \\Runtime\\Variants\\" + formatObjectName(def.Type.ImplementedVariant())
variant = " implements " + jenny.config.fullNamespaceRef("Runtime\\Variants\\"+formatObjectName(def.Type.ImplementedVariant()))
}

buffer.WriteString(fmt.Sprintf("class %s%s\n%s", defName, variant, jenny.typeFormatter.formatType(def.Type)))
Expand All @@ -106,12 +106,13 @@ func (jenny RawTypes) formatObject(context languages.Context, schema *ast.Schema
buffer.WriteString("\n")

filename := filepath.Join(
"src",
"Types",
formatPackageName(schema.Package),
fmt.Sprintf("%s.php", defName),
)

output := fmt.Sprintf("<?php\n\nnamespace Types\\%s;\n\n", formatPackageName(schema.Package))
output := fmt.Sprintf("<?php\n\nnamespace %s\\%s;\n\n", jenny.config.fullNamespace("Types"), formatPackageName(schema.Package))
output += buffer.String()

return *codejen.NewFile(filename, []byte(output), jenny), nil
Expand Down
6 changes: 4 additions & 2 deletions internal/jennies/php/rawtypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func TestRawTypes_Generate(t *testing.T) {
},
}

config := Config{}
config := Config{
NamespaceRoot: "Grafana\\Foundation",
}
jenny := RawTypes{
Config: config,
config: config,
}
compilerPasses := New(config).CompilerPasses()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Runtime\Variants;
namespace {{ .NamespaceRoot }}\Runtime\Variants;

interface Dataquery {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Runtime\Variants;
namespace {{ .NamespaceRoot }}\Runtime\Variants;

interface Panelcfg {
}
5 changes: 3 additions & 2 deletions internal/jennies/php/typehints.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

type typehints struct {
config Config
}

func (generator *typehints) requiresHint(def ast.Type) bool {
Expand Down Expand Up @@ -101,11 +102,11 @@ func (generator *typehints) refHint(def ast.Type) string {
referredPkg := formatPackageName(def.AsRef().ReferredPkg)
typeName := formatObjectName(def.AsRef().ReferredType)

return "\\Types\\" + referredPkg + "\\" + typeName
return generator.config.fullNamespaceRef("Types\\" + referredPkg + "\\" + typeName)
}

func (generator *typehints) composableSlotHint(def ast.Type) string {
return "\\Runtime\\Variants\\" + formatObjectName(string(def.ComposableSlot.Variant))
return generator.config.fullNamespaceRef("Runtime\\Variants\\" + formatObjectName(string(def.ComposableSlot.Variant)))
}

func (generator *typehints) disjunctionHint(def ast.Type) string {
Expand Down
8 changes: 5 additions & 3 deletions internal/jennies/php/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (formatter *typeFormatter) doFormatType(def ast.Type, resolveBuilders bool)
}

func (formatter *typeFormatter) variantInterface(variant string) string {
return "\\Runtime\\Variants\\" + formatObjectName(variant)
return formatter.config.fullNamespaceRef("Runtime\\Variants\\" + formatObjectName(variant))
}

func (formatter *typeFormatter) formatStructBody(def ast.StructType) string {
Expand Down Expand Up @@ -198,13 +198,15 @@ func (formatter *typeFormatter) formatRef(def ast.Type, resolveBuilders bool) st
typeName := formatObjectName(def.AsRef().ReferredType)

if referredPkg != "" {
typeName = "\\Types\\" + referredPkg + "\\" + typeName
typeName = "Types\\" + referredPkg + "\\" + typeName
}

if resolveBuilders {
typeName = "\\Builders\\" + referredPkg + "\\" + typeName
typeName = "Builders\\" + referredPkg + "\\" + typeName
}

typeName = formatter.config.fullNamespaceRef(typeName)

if resolveBuilders && formatter.context.ResolveToBuilder(def) {
cogAlias := "cog"

Expand Down
13 changes: 9 additions & 4 deletions internal/jennies/php/variantsplugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type VariantsPlugins struct {
config Config
}

func (jenny VariantsPlugins) JennyName() string {
Expand All @@ -30,19 +31,23 @@ func (jenny VariantsPlugins) Generate(_ languages.Context) (codejen.Files, error
}

func (jenny VariantsPlugins) dataqueryVariant() (codejen.File, error) {
rendered, err := renderTemplate("runtime/dataquery_variant.tmpl", map[string]any{})
rendered, err := renderTemplate("runtime/dataquery_variant.tmpl", map[string]any{
"NamespaceRoot": jenny.config.NamespaceRoot,
})
if err != nil {
return codejen.File{}, err
}

return *codejen.NewFile("Runtime/Variants/Dataquery.php", []byte(rendered), jenny), nil
return *codejen.NewFile("src/Runtime/Variants/Dataquery.php", []byte(rendered), jenny), nil
}

func (jenny VariantsPlugins) panelcfgInterface() (codejen.File, error) {
rendered, err := renderTemplate("runtime/panelcfg_variant.tmpl", map[string]any{})
rendered, err := renderTemplate("runtime/panelcfg_variant.tmpl", map[string]any{
"NamespaceRoot": jenny.config.NamespaceRoot,
})
if err != nil {
return codejen.File{}, err
}

return *codejen.NewFile("Runtime/Variants/Panelcfg.php", []byte(rendered), jenny), nil
return *codejen.NewFile("src/Runtime/Variants/Panelcfg.php", []byte(rendered), jenny), nil
}
Loading

0 comments on commit 8b5e244

Please sign in to comment.