use minijinja::Environment;
static TEMPLATES: &[(&str, &str)] = &[
(
"php_json_map_field_doc.jinja",
include_str!("templates/php_json_map_field_doc.jinja"),
),
(
"visitor_nodecontext_helper.jinja",
include_str!("templates/visitor_nodecontext_helper.jinja"),
),
(
"visitor_zval_to_visitresult.jinja",
include_str!("templates/visitor_zval_to_visitresult.jinja"),
),
(
"visitor_bridge_struct.jinja",
include_str!("templates/visitor_bridge_struct.jinja"),
),
(
"bridge_constructor.jinja",
include_str!("templates/bridge_constructor.jinja"),
),
(
"bridge_unregister_fn.jinja",
include_str!("templates/bridge_unregister_fn.jinja"),
),
("bridge_clear_fn.jinja", include_str!("templates/bridge_clear_fn.jinja")),
(
"bridge_registration_fn.jinja",
include_str!("templates/bridge_registration_fn.jinja"),
),
(
"sync_method_body.jinja",
include_str!("templates/sync_method_body.jinja"),
),
(
"async_method_body.jinja",
include_str!("templates/async_method_body.jinja"),
),
(
"bridge_sync_impl.jinja",
include_str!("templates/bridge_sync_impl.jinja"),
),
(
"bridge_registration_validation.jinja",
include_str!("templates/bridge_registration_validation.jinja"),
),
(
"bridge_registration_body.jinja",
include_str!("templates/bridge_registration_body.jinja"),
),
(
"php_named_let_binding.jinja",
include_str!("templates/php_named_let_binding.jinja"),
),
(
"php_named_enum_serde_let_binding.jinja",
include_str!("templates/php_named_enum_serde_let_binding.jinja"),
),
(
"php_vec_named_let_binding.jinja",
include_str!("templates/php_vec_named_let_binding.jinja"),
),
(
"php_sanitized_vec_let_binding.jinja",
include_str!("templates/php_sanitized_vec_let_binding.jinja"),
),
(
"php_vec_string_refs_let_binding.jinja",
include_str!("templates/php_vec_string_refs_let_binding.jinja"),
),
(
"php_vec_named_struct_let_binding.jinja",
include_str!("templates/php_vec_named_struct_let_binding.jinja"),
),
(
"php_struct_field_assignment.jinja",
include_str!("templates/php_struct_field_assignment.jinja"),
),
(
"php_impl_from_begin.jinja",
include_str!("templates/php_impl_from_begin.jinja"),
),
(
"php_impl_from_end.jinja",
include_str!("templates/php_impl_from_end.jinja"),
),
(
"php_lossy_binding_struct_end.jinja",
include_str!("templates/php_lossy_binding_struct_end.jinja"),
),
(
"php_lossy_binding_struct_begin.jinja",
include_str!("templates/php_lossy_binding_struct_begin.jinja"),
),
(
"php_default_update.jinja",
include_str!("templates/php_default_update.jinja"),
),
(
"php_duration_default_expr.jinja",
include_str!("templates/php_duration_default_expr.jinja"),
),
(
"php_phpdoc_block_start.jinja",
include_str!("templates/php_phpdoc_block_start.jinja"),
),
(
"php_phpdoc_block_end.jinja",
include_str!("templates/php_phpdoc_block_end.jinja"),
),
(
"php_phpdoc_empty_line.jinja",
include_str!("templates/php_phpdoc_empty_line.jinja"),
),
(
"php_phpdoc_text_line.jinja",
include_str!("templates/php_phpdoc_text_line.jinja"),
),
(
"php_phpdoc_lines.jinja",
include_str!("templates/php_phpdoc_lines.jinja"),
),
(
"php_phpdoc_param_line.jinja",
include_str!("templates/php_phpdoc_param_line.jinja"),
),
(
"php_phpdoc_return_line.jinja",
include_str!("templates/php_phpdoc_return_line.jinja"),
),
(
"php_phpdoc_throws_line.jinja",
include_str!("templates/php_phpdoc_throws_line.jinja"),
),
(
"php_class_declaration.jinja",
include_str!("templates/php_class_declaration.jinja"),
),
("php_class_end.jinja", include_str!("templates/php_class_end.jinja")),
(
"php_method_signature_start.jinja",
include_str!("templates/php_method_signature_start.jinja"),
),
(
"php_method_signature_end.jinja",
include_str!("templates/php_method_signature_end.jinja"),
),
("php_method_end.jinja", include_str!("templates/php_method_end.jinja")),
(
"php_constructor_method.jinja",
include_str!("templates/php_constructor_method.jinja"),
),
("php_file_header.jinja", include_str!("templates/php_file_header.jinja")),
(
"php_declare_strict_types.jinja",
include_str!("templates/php_declare_strict_types.jinja"),
),
("php_namespace.jinja", include_str!("templates/php_namespace.jinja")),
(
"php_namespace_block_begin.jinja",
include_str!("templates/php_namespace_block_begin.jinja"),
),
(
"php_namespace_block_end.jinja",
include_str!("templates/php_namespace_block_end.jinja"),
),
(
"php_class_registration.jinja",
include_str!("templates/php_class_registration.jinja"),
),
(
"php_class_declaration_final.jinja",
include_str!("templates/php_class_declaration_final.jinja"),
),
(
"php_method_call_void.jinja",
include_str!("templates/php_method_call_void.jinja"),
),
(
"php_method_call_return.jinja",
include_str!("templates/php_method_call_return.jinja"),
),
(
"php_exception_class.jinja",
include_str!("templates/php_exception_class.jinja"),
),
(
"php_opaque_class_stub.jinja",
include_str!("templates/php_opaque_class_stub.jinja"),
),
(
"php_record_class_begin.jinja",
include_str!("templates/php_record_class_begin.jinja"),
),
(
"php_property_type_hint.jinja",
include_str!("templates/php_property_type_hint.jinja"),
),
(
"php_property_declaration.jinja",
include_str!("templates/php_property_declaration.jinja"),
),
(
"php_constructor_doc_param.jinja",
include_str!("templates/php_constructor_doc_param.jinja"),
),
(
"php_constructor_doc_return.jinja",
include_str!("templates/php_constructor_doc_return.jinja"),
),
(
"php_enum_class_begin.jinja",
include_str!("templates/php_enum_class_begin.jinja"),
),
(
"php_tagged_enum_begin.jinja",
include_str!("templates/php_tagged_enum_begin.jinja"),
),
(
"php_enum_variant.jinja",
include_str!("templates/php_enum_variant.jinja"),
),
(
"php_facade_class_begin.jinja",
include_str!("templates/php_facade_class_begin.jinja"),
),
(
"php_facade_function_doc_param.jinja",
include_str!("templates/php_facade_function_doc_param.jinja"),
),
(
"php_facade_function_doc_return.jinja",
include_str!("templates/php_facade_function_doc_return.jinja"),
),
(
"php_flat_enum_struct_start.jinja",
include_str!("templates/php_flat_enum_struct_start.jinja"),
),
(
"php_flat_enum_tag_field.jinja",
include_str!("templates/php_flat_enum_tag_field.jinja"),
),
(
"php_flat_enum_from_json.jinja",
include_str!("templates/php_flat_enum_from_json.jinja"),
),
(
"php_flat_enum_option_field.jinja",
include_str!("templates/php_flat_enum_option_field.jinja"),
),
(
"php_flat_enum_struct_end.jinja",
include_str!("templates/php_flat_enum_struct_end.jinja"),
),
(
"php_flat_enum_impl_from_start.jinja",
include_str!("templates/php_flat_enum_impl_from_start.jinja"),
),
(
"php_flat_enum_variant_match_empty.jinja",
include_str!("templates/php_flat_enum_variant_match_empty.jinja"),
),
(
"php_flat_enum_impl_match_end.jinja",
include_str!("templates/php_flat_enum_impl_match_end.jinja"),
),
(
"php_flat_enum_impl_into_start.jinja",
include_str!("templates/php_flat_enum_impl_into_start.jinja"),
),
(
"php_flat_enum_variant_match_into_empty.jinja",
include_str!("templates/php_flat_enum_variant_match_into_empty.jinja"),
),
(
"php_flat_enum_default_fallback_match_arm.jinja",
include_str!("templates/php_flat_enum_default_fallback_match_arm.jinja"),
),
(
"php_flat_enum_variant_field.jinja",
include_str!("templates/php_flat_enum_variant_field.jinja"),
),
(
"php_flat_enum_variant_constructor.jinja",
include_str!("templates/php_flat_enum_variant_constructor.jinja"),
),
(
"php_flat_enum_tag_assignment.jinja",
include_str!("templates/php_flat_enum_tag_assignment.jinja"),
),
(
"php_flat_enum_fallback_variant_empty.jinja",
include_str!("templates/php_flat_enum_fallback_variant_empty.jinja"),
),
(
"php_flat_enum_fallback_variant_tuple_start.jinja",
include_str!("templates/php_flat_enum_fallback_variant_tuple_start.jinja"),
),
(
"php_flat_enum_fallback_variant_struct_start.jinja",
include_str!("templates/php_flat_enum_fallback_variant_struct_start.jinja"),
),
(
"php_flat_enum_fallback_variant_field.jinja",
include_str!("templates/php_flat_enum_fallback_variant_field.jinja"),
),
(
"php_flat_enum_tuple_exprs.jinja",
include_str!("templates/php_flat_enum_tuple_exprs.jinja"),
),
(
"php_class_stub_start.jinja",
include_str!("templates/php_class_stub_start.jinja"),
),
("php_doc_line.jinja", include_str!("templates/php_doc_line.jinja")),
(
"php_enum_constant_stub.jinja",
include_str!("templates/php_enum_constant_stub.jinja"),
),
(
"php_property_annotation.jinja",
include_str!("templates/php_property_annotation.jinja"),
),
(
"php_facade_class_declaration.jinja",
include_str!("templates/php_facade_class_declaration.jinja"),
),
(
"php_exception_class_declaration.jinja",
include_str!("templates/php_exception_class_declaration.jinja"),
),
(
"php_opaque_class_stub_declaration.jinja",
include_str!("templates/php_opaque_class_stub_declaration.jinja"),
),
(
"php_record_class_stub_declaration.jinja",
include_str!("templates/php_record_class_stub_declaration.jinja"),
),
(
"php_phpdoc_doc_line.jinja",
include_str!("templates/php_phpdoc_doc_line.jinja"),
),
(
"php_property_type_annotation.jinja",
include_str!("templates/php_property_type_annotation.jinja"),
),
(
"php_property_stub.jinja",
include_str!("templates/php_property_stub.jinja"),
),
(
"php_phpdoc_array_param.jinja",
include_str!("templates/php_phpdoc_array_param.jinja"),
),
(
"php_phpdoc_array_return.jinja",
include_str!("templates/php_phpdoc_array_return.jinja"),
),
("php_getter_stub.jinja", include_str!("templates/php_getter_stub.jinja")),
(
"php_method_call_statement.jinja",
include_str!("templates/php_method_call_statement.jinja"),
),
(
"php_api_class_declaration.jinja",
include_str!("templates/php_api_class_declaration.jinja"),
),
(
"php_phpdoc_static_param.jinja",
include_str!("templates/php_phpdoc_static_param.jinja"),
),
(
"php_phpdoc_static_return.jinja",
include_str!("templates/php_phpdoc_static_return.jinja"),
),
(
"php_static_method_stub.jinja",
include_str!("templates/php_static_method_stub.jinja"),
),
(
"php_let_binding_named.jinja",
include_str!("templates/php_let_binding_named.jinja"),
),
(
"php_let_binding_named_optional.jinja",
include_str!("templates/php_let_binding_named_optional.jinja"),
),
(
"php_let_binding_vec_named.jinja",
include_str!("templates/php_let_binding_vec_named.jinja"),
),
(
"php_let_binding_vec_named_optional.jinja",
include_str!("templates/php_let_binding_vec_named_optional.jinja"),
),
(
"php_let_binding_sanitized_vec_string.jinja",
include_str!("templates/php_let_binding_sanitized_vec_string.jinja"),
),
(
"php_let_binding_sanitized_vec_string_optional.jinja",
include_str!("templates/php_let_binding_sanitized_vec_string_optional.jinja"),
),
(
"php_json_let_binding.jinja",
include_str!("templates/php_json_let_binding.jinja"),
),
(
"php_json_let_binding_optional.jinja",
include_str!("templates/php_json_let_binding_optional.jinja"),
),
(
"php_let_binding_string_refs.jinja",
include_str!("templates/php_let_binding_string_refs.jinja"),
),
(
"php_serde_ref_named_optional_let_binding.jinja",
include_str!("templates/php_serde_ref_named_optional_let_binding.jinja"),
),
(
"php_serde_ref_named_let_binding.jinja",
include_str!("templates/php_serde_ref_named_let_binding.jinja"),
),
(
"php_result_unit_body.jinja",
include_str!("templates/php_result_unit_body.jinja"),
),
(
"php_result_unit_body_with_let_bindings.jinja",
include_str!("templates/php_result_unit_body_with_let_bindings.jinja"),
),
(
"php_result_wrapped_body.jinja",
include_str!("templates/php_result_wrapped_body.jinja"),
),
(
"php_result_wrapped_body_with_let_bindings.jinja",
include_str!("templates/php_result_wrapped_body_with_let_bindings.jinja"),
),
(
"php_result_debug_body_with_let_bindings.jinja",
include_str!("templates/php_result_debug_body_with_let_bindings.jinja"),
),
(
"php_async_result_body_with_let_bindings.jinja",
include_str!("templates/php_async_result_body_with_let_bindings.jinja"),
),
(
"php_async_body_with_let_bindings.jinja",
include_str!("templates/php_async_body_with_let_bindings.jinja"),
),
(
"php_debug_body_with_let_bindings.jinja",
include_str!("templates/php_debug_body_with_let_bindings.jinja"),
),
(
"php_wrapped_body_with_let_bindings.jinja",
include_str!("templates/php_wrapped_body_with_let_bindings.jinja"),
),
(
"php_ok_wrapped_body_with_let_bindings.jinja",
include_str!("templates/php_ok_wrapped_body_with_let_bindings.jinja"),
),
(
"php_stub_error_body.jinja",
include_str!("templates/php_stub_error_body.jinja"),
),
(
"php_stub_unsupported_return.jinja",
include_str!("templates/php_stub_unsupported_return.jinja"),
),
(
"php_enum_string_match_arm.jinja",
include_str!("templates/php_enum_string_match_arm.jinja"),
),
(
"php_enum_string_match_fallback_arm.jinja",
include_str!("templates/php_enum_string_match_fallback_arm.jinja"),
),
(
"php_enum_string_optional_match_expr.jinja",
include_str!("templates/php_enum_string_optional_match_expr.jinja"),
),
(
"php_enum_string_match_expr.jinja",
include_str!("templates/php_enum_string_match_expr.jinja"),
),
(
"php_enum_variant_unit_expr.jinja",
include_str!("templates/php_enum_variant_unit_expr.jinja"),
),
(
"php_enum_variant_tuple_expr.jinja",
include_str!("templates/php_enum_variant_tuple_expr.jinja"),
),
(
"php_enum_variant_struct_expr.jinja",
include_str!("templates/php_enum_variant_struct_expr.jinja"),
),
(
"php_enum_variant_default_field_expr.jinja",
include_str!("templates/php_enum_variant_default_field_expr.jinja"),
),
(
"php_method_definition_no_params.jinja",
include_str!("templates/php_method_definition_no_params.jinja"),
),
(
"php_method_definition_with_params.jinja",
include_str!("templates/php_method_definition_with_params.jinja"),
),
(
"php_optional_named_setter.jinja",
include_str!("templates/php_optional_named_setter.jinja"),
),
(
"php_static_method_definition_no_params.jinja",
include_str!("templates/php_static_method_definition_no_params.jinja"),
),
(
"php_static_method_definition_with_params.jinja",
include_str!("templates/php_static_method_definition_with_params.jinja"),
),
(
"php_async_static_method_definition_no_params.jinja",
include_str!("templates/php_async_static_method_definition_no_params.jinja"),
),
(
"php_async_static_method_definition_with_params.jinja",
include_str!("templates/php_async_static_method_definition_with_params.jinja"),
),
(
"php_async_instance_method_definition_no_params.jinja",
include_str!("templates/php_async_instance_method_definition_no_params.jinja"),
),
(
"php_async_instance_method_definition_with_params.jinja",
include_str!("templates/php_async_instance_method_definition_with_params.jinja"),
),
(
"php_visit_result_with_template.jinja",
include_str!("templates/php_visit_result_with_template.jinja"),
),
(
"php_trait_impl_start.jinja",
include_str!("templates/php_trait_impl_start.jinja"),
),
(
"php_visitor_method_signature.jinja",
include_str!("templates/php_visitor_method_signature.jinja"),
),
(
"php_visitor_method_php_call.jinja",
include_str!("templates/php_visitor_method_php_call.jinja"),
),
(
"php_visitor_method_result_match.jinja",
include_str!("templates/php_visitor_method_result_match.jinja"),
),
(
"php_bridge_function_definition.jinja",
include_str!("templates/php_bridge_function_definition.jinja"),
),
(
"php_visitor_arg_nodecontext.jinja",
include_str!("templates/php_visitor_arg_nodecontext.jinja"),
),
(
"php_visitor_arg_optional_string_ref.jinja",
include_str!("templates/php_visitor_arg_optional_string_ref.jinja"),
),
(
"php_visitor_arg_string_ref.jinja",
include_str!("templates/php_visitor_arg_string_ref.jinja"),
),
(
"php_visitor_arg_string_owned.jinja",
include_str!("templates/php_visitor_arg_string_owned.jinja"),
),
(
"php_visitor_arg_bool.jinja",
include_str!("templates/php_visitor_arg_bool.jinja"),
),
(
"php_visitor_arg_default.jinja",
include_str!("templates/php_visitor_arg_default.jinja"),
),
(
"php_visitor_template_var_let_binding.jinja",
include_str!("templates/php_visitor_template_var_let_binding.jinja"),
),
(
"php_visitor_interface_start.jinja",
include_str!("templates/php_visitor_interface_start.jinja"),
),
(
"php_visitor_interface_method.jinja",
include_str!("templates/php_visitor_interface_method.jinja"),
),
(
"php_interface_start.jinja",
include_str!("templates/php_interface_start.jinja"),
),
(
"php_interface_method.jinja",
include_str!("templates/php_interface_method.jinja"),
),
(
"php_service_class_start.jinja",
include_str!("templates/php_service_class_start.jinja"),
),
(
"php_service_constructor_start.jinja",
include_str!("templates/php_service_constructor_start.jinja"),
),
(
"php_service_method_start.jinja",
include_str!("templates/php_service_method_start.jinja"),
),
(
"php_service_property_assignment.jinja",
include_str!("templates/php_service_property_assignment.jinja"),
),
(
"php_service_native_call.jinja",
include_str!("templates/php_service_native_call.jinja"),
),
(
"php_service_native_return.jinja",
include_str!("templates/php_service_native_return.jinja"),
),
(
"php_service_registration_factory_body.jinja",
include_str!("templates/php_service_registration_factory_body.jinja"),
),
(
"php_service_registration_store.jinja",
include_str!("templates/php_service_registration_store.jinja"),
),
(
"php_service_rust_function_start.jinja",
include_str!("templates/php_service_rust_function_start.jinja"),
),
(
"php_service_rust_owner_init.jinja",
include_str!("templates/php_service_rust_owner_init.jinja"),
),
(
"php_service_rust_registration_match_start.jinja",
include_str!("templates/php_service_rust_registration_match_start.jinja"),
),
(
"php_service_rust_bridge_binding.jinja",
include_str!("templates/php_service_rust_bridge_binding.jinja"),
),
(
"php_service_rust_metadata_binding.jinja",
include_str!("templates/php_service_rust_metadata_binding.jinja"),
),
(
"php_service_rust_owner_registration_call.jinja",
include_str!("templates/php_service_rust_owner_registration_call.jinja"),
),
(
"php_service_variant_direct_body.jinja",
include_str!("templates/php_service_variant_direct_body.jinja"),
),
(
"php_service_variant_factory_body.jinja",
include_str!("templates/php_service_variant_factory_body.jinja"),
),
(
"php_service_variant_wrapper_factory_body.jinja",
include_str!("templates/php_service_variant_wrapper_factory_body.jinja"),
),
(
"php_prefixed_phpdoc_line.jinja",
include_str!("templates/php_prefixed_phpdoc_line.jinja"),
),
(
"php_inline_property_phpdoc.jinja",
include_str!("templates/php_inline_property_phpdoc.jinja"),
),
(
"php_trait_bridge_api_method.jinja",
include_str!("templates/php_trait_bridge_api_method.jinja"),
),
(
"php_streaming_adapter_method.jinja",
include_str!("templates/php_streaming_adapter_method.jinja"),
),
(
"php_stub_method_definition.jinja",
include_str!("templates/php_stub_method_definition.jinja"),
),
(
"php_indented_phpdoc_empty_line.jinja",
include_str!("templates/php_indented_phpdoc_empty_line.jinja"),
),
(
"php_indented_phpdoc_block_end.jinja",
include_str!("templates/php_indented_phpdoc_block_end.jinja"),
),
(
"php_ahash_cow_value_let_binding.jinja",
include_str!("templates/php_ahash_cow_value_let_binding.jinja"),
),
(
"php_optional_mut_unwrap_binding.jinja",
include_str!("templates/php_optional_mut_unwrap_binding.jinja"),
),
(
"php_service_handler_bridge.jinja",
include_str!("templates/php_service_handler_bridge.jinja"),
),
(
"php_final_class_stub_start.jinja",
include_str!("templates/php_final_class_stub_start.jinja"),
),
(
"serde_defaults_module.jinja",
include_str!("templates/serde_defaults_module.jinja"),
),
];
pub(crate) fn make_env() -> Environment<'static> {
let mut env = Environment::new();
env.set_trim_blocks(true);
env.set_lstrip_blocks(true);
env.set_keep_trailing_newline(true);
for (name, src) in TEMPLATES {
env.add_template(name, src).expect("built-in template is valid");
}
env
}
pub(crate) fn render(template_name: &str, ctx: minijinja::Value) -> String {
let rendered = make_env()
.get_template(template_name)
.unwrap_or_else(|_| panic!("template {template_name} not found"))
.render(ctx)
.unwrap_or_else(|e| panic!("template {template_name} failed to render: {e}"));
crate::core::keep_marker::strip_keep_markers(&rendered)
}
#[cfg(test)]
mod template_registration_tests {
use super::TEMPLATES;
use std::collections::HashSet;
use std::path::Path;
#[test]
fn every_template_file_is_registered() {
let templates_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/backends/php/templates"));
let registered_contents: HashSet<&str> = TEMPLATES.iter().map(|(_, content)| *content).collect();
let mut unregistered = Vec::new();
collect_unregistered(templates_dir, templates_dir, ®istered_contents, &mut unregistered);
unregistered.sort();
assert!(
unregistered.is_empty(),
"found .jinja file(s) in templates/ whose content is not registered in TEMPLATES: {unregistered:?}"
);
}
fn collect_unregistered(
root: &Path,
dir: &Path,
registered_contents: &HashSet<&str>,
unregistered: &mut Vec<String>,
) {
for entry in std::fs::read_dir(dir).expect("read templates directory") {
let entry = entry.expect("read templates directory entry");
let path = entry.path();
if path.is_dir() {
collect_unregistered(root, &path, registered_contents, unregistered);
continue;
}
if path.extension().and_then(|ext| ext.to_str()) != Some("jinja") {
continue;
}
let content = std::fs::read_to_string(&path).expect("read template file");
if !registered_contents.contains(content.as_str()) {
let relative = path
.strip_prefix(root)
.expect("template path under templates root")
.to_str()
.expect("template path is valid UTF-8")
.replace('\\', "/");
unregistered.push(relative);
}
}
}
}