reliv/pipe-rat-2

只需几行Expressive配置即可创建REST API。这是一个PSR7兼容的PHP库,其核心使用Zend Expressive Middleware。

0.9.0 2019-12-05 15:58 UTC

This package is auto-updated.

Last update: 2024-09-06 01:59:42 UTC


README

只需几行Expressive配置即可创建REST API。这是一个PSR7兼容的PHP库,其核心使用Zend\Expressive Middleware配置。

  • 允许使用Doctrine实体的配置来创建API端点
  • 允许配置常见的API问题
    • data-extractor - 从数据对象和对象数组获取数组
    • data-hydrator - 将客户端数据获取到数据对象和数组中
    • data-validate - 验证客户端数据
    • repository - 常见仓库方法
    • request-attributes - 常见查询参数('where'、'fields'、'limit'、'order'、'skip'等...)
    • request-attributes-validate - 验证参数
    • request-format - 解析请求数据的数据格式
    • response-format - 响应数据格式(JSON等...)
    • response-headers - 响应的常见头部(缓存等...)

配置示例

'routes' => [
    /* Put your path here */
    'my.thing.find' => [
        /* Use standard route names for client simplicity */
        'name' => 'my.thing.find',
        
        /* Use standard route paths for client simplicity */
        'path' => 'my/thing',
        
        /* Wire each API independently */
        'middleware' => [
            RequestFormat::configKey()
            => RequestFormat::class,

            RequestAcl::configKey()
            => RequestAcl::class,

            RequestAttributes::configKey()
            => RequestAttributes::class,

            RequestAttributesValidate::configKey()
            => RequestAttributesValidate::class,

            /** <response-mutators> */
            ResponseHeaders::configKey()
            => ResponseHeaders::class,

            ResponseFormat::configKey()
            => ResponseFormat::class,

            ResponseDataExtractor::configKey()
            => ResponseDataExtractor::class,
            /** </response-mutators> */

            RepositoryFind::configKey()
            => RepositoryFind::class,
        ],
        
        /* Use route to find options at runtime */
        'options' => [
            RequestFormat::configKey() => [
                RequestFormat::OPTION_SERVICE_NAME
                => WithParsedBodyJson::class,

                RequestFormat::OPTION_SERVICE_OPTIONS => [],
            ],

            RequestAcl::configKey() => [
                RequestAcl::OPTION_SERVICE_NAME
                => IsAllowedNotConfigured::class,

                RequestAcl::OPTION_SERVICE_OPTIONS => [
                    IsAllowedNotConfigured::OPTION_MESSAGE
                    => IsAllowedNotConfigured::DEFAULT_MESSAGE
                        . ' for pipe-rat-2 resource: "{pipe-rat-2-config.resource-name}"'
                        . ' in file: "{pipe-rat-2-config.source-config-file}"',
                ],
            ],

            RequestAttributes::configKey() => [
                RequestAttributes::OPTION_SERVICE_NAMES => [
                    WithRequestAttributeFields::class
                    => WithRequestAttributeUrlEncodedFields::class,

                    WithRequestAttributeAllowedFieldConfig::class
                    => WithRequestAttributeAllowedFieldConfigFromOptions::class,

                    WithRequestAttributeExtractorFieldConfig::class
                    => WithRequestAttributeExtractorFieldConfigByRequestFields::class,

                    WithRequestAttributeWhere::class
                    => WithRequestAttributeUrlEncodedWhere::class,

                    WithRequestAttributeWhereMutator::class
                    => WithRequestAttributeWhereMutatorNoop::class,

                    WithRequestAttributeOrder::class
                    => WithRequestAttributeUrlEncodedOrder::class,

                    WithRequestAttributeSkip::class
                    => WithRequestAttributeUrlEncodedSkip::class,

                    WithRequestAttributeLimit::class
                    => WithRequestAttributeUrlEncodedLimit::class,
                ],

                RequestAttributes::OPTION_SERVICE_NAMES_OPTIONS => [
                    WithRequestAttributeAllowedFieldConfig::class => [
                        WithRequestAttributeAllowedFieldConfigFromOptions::OPTION_ALLOWED_FIELDS
                        /* @todo Over-ride with YOUR FieldsConfig */
                        => [
                            FieldConfig::KEY_TYPE => FieldConfig::COLLECTION,
                            FieldConfig::KEY_PROPERTIES => [],
                            FieldConfig::KEY_INCLUDE => true,
                        ],
                    ]
                ],
            ],
            
            RequestAttributesValidate::configKey() => [
                RequestAttributesValidate::OPTION_SERVICE_NAME
                => WithRequestValidAttributesAsserts::class,
            ],

            /** <response-mutators> */
            ResponseHeaders::configKey() => [
                ResponseHeaders::OPTION_SERVICE_NAME
                => WithResponseHeadersAdded::class,

                ResponseHeaders::OPTION_SERVICE_OPTIONS => [
                    WithResponseHeadersAdded::OPTION_HEADERS => []
                ],
            ],

            ResponseFormat::configKey() => [
                ResponseFormat::OPTION_SERVICE_NAME
                => WithFormattedResponseJson::class,

                ResponseFormat::OPTION_SERVICE_OPTIONS => [],
            ],

            ResponseDataExtractor::configKey() => [
                ResponseDataExtractor::OPTION_SERVICE_NAME => ExtractByType::class,
            ],
            /** </response-mutators> */

            RepositoryFind::configKey() => [
                RepositoryFind::OPTION_SERVICE_NAME
                => FindNotConfigured::class,

                RepositoryFind::OPTION_SERVICE_OPTIONS => [
                    FindNotConfigured::OPTION_MESSAGE
                    => FindNotConfigured::DEFAULT_MESSAGE
                        . ' for pipe-rat-2 resource: "{pipe-rat-2-config.resource-name}"'
                        . ' in file: "{pipe-rat-2-config.source-config-file}"',
                ],
            ],
        ],
        
        /* Use to define allowed methods */
        'allowed_methods' => ['GET'],
    ],
],