anzusystems/common-bundle

Anzu 常用服务、实用工具和助手

安装数: 5,221

依赖关系: 2

建议者: 0

安全性: 0

星标: 0

关注者: 6

分支: 0

公开问题: 0

类型:symfony-bundle

8.8.0 2024-08-22 12:32 UTC

README

为 Anzusystems 的项目提供通用功能。

安装

在容器内执行以下命令以下载最新版本的包

$ composer require anzusystems/common-bundle --no-scripts

步骤 3:使用 Bundle

将您的 src\Kernel.php 修改为扩展 AnzuSystems\CommonBundle\Kernel\AnzuKernel

AnzuKernel 在构造函数中需要一些额外的参数

public function __construct(
    private string $appSystem, // Specific application/system name, eg. "core".
    private string $appVersion, // Specific application/system version, eg. "1.0.0".
    private bool $appReadOnlyMode, // Boolean if application/system should run in read only mode.
    string $environment,
    bool $debug,
) {
    parent::__construct(
        environment: $environment,
        debug: $debug
    );
}

这意味着,您必须在所有入口点初始化 AnzuKernel

  • public/index.php 应该看起来像这样
<?php

declare(strict_types=1);

use App\Kernel;
use AnzuSystems\CommonBundle\Kernel\AnzuKernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return static function (array $context): Kernel {
    return new Kernel(
        appNamespace: $context['APP_NAMESPACE'],
        appSystem: $context['APP_SYSTEM'],
        appVersion: $context['APP_VERSION'],
        appReadOnlyMode: (bool) $context['APP_READ_ONLY_MODE'],
        environment: $context['APP_ENV'],
        debug: (bool) $context['APP_DEBUG'],
    );
};
  • bin/console 应该看起来像这样
#!/usr/bin/env php
<?php

declare(strict_types=1);

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;

if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
    throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return static function (array $context): Application {
    $kernel = new Kernel(
        appNamespace: $context['APP_NAMESPACE'],
        appSystem: $context['APP_SYSTEN'],
        appVersion: $context['APP_VERSION'],
        appReadOnlyMode: (bool) $context['APP_READ_ONLY_MODE'],
        environment: $context['APP_ENV'],
        debug: (bool) $context['APP_DEBUG'],
    );

    return new Application($kernel);
};

配置

您必须在 config/packages/anzu_common.yaml 中定义配置。以下是一个完整的配置列表

anzu_common:
    settings:
        # Service id of your application Redis
        app_redis: TestRedis
        # Boolean flag for enabling/disabling proxy cache headers.
        app_cache_proxy_enabled: true
        # Set FCQN to your User entity class
        user_entity_class: App\Entity\User
        # Namespace of your application entity classes
        app_entity_namespace: App\Entity
        # Namespace of your application value objects.
        app_value_object_namespace: App\Model\ValueObject
        # FCQN of your commands which won't be locked against concurrency. Defaults to command listed bellow.
        unlocked_commands:
            - Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand
            - Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand
            - Symfony\Component\Messenger\Command\ConsumeMessagesCommand
    health_check:
        enabled: true
        # Table name against which is performed health check.
        mysql_table_name: _doctrine_migration_versions
        mongo_collections: [anzu_mongo_app_log_collection, anzu_mongo_audit_log_collection]
        # Modules used for health check. Defaults to modules listed bellow.
        # Here you can only define some of these modules.
        # For none use modules: []
        modules:
            - AnzuSystems\CommonBundle\HealthCheck\Module\OpCacheModule
            - AnzuSystems\CommonBundle\HealthCheck\Module\ForwardIpModule
            - AnzuSystems\CommonBundle\HealthCheck\Module\MysqlModule
            - AnzuSystems\CommonBundle\HealthCheck\Module\MongoModule
            - AnzuSystems\CommonBundle\HealthCheck\Module\RedisModule
            - AnzuSystems\CommonBundle\HealthCheck\Module\DataMountModule
    errors:
        enabled: true
        # Default empty. You can define regexes on which is error handling enabled.
        only_uri_match:
          - ^/api/
        # Default exception handler service id. You can change to your own service id, it's required.
        default_exception_handler: AnzuSystems\CommonBundle\Exception\Handler\DefaultExceptionHandler
        # Exception Handlers used in `AnzuSystems\CommonBundle\Event\Listener\ExceptionListener`.
        # Here you can only define some of these handlers.
        # For none use exception_handlers: []
        exception_handlers:
            - AnzuSystems\CommonBundle\Exception\Handler\NotFoundExceptionHandler
            - AnzuSystems\CommonBundle\Exception\Handler\ValidationExceptionHandler
            - AnzuSystems\CommonBundle\Exception\Handler\AppReadOnlyModeExceptionHandler
            - AnzuSystems\CommonBundle\Exception\Handler\AccessDeniedExceptionHandler
    logs:
        enabled: true
        # Logs are sent through Symfony Messenger.
        messenger_transport:
            # Name of your messenger transport.
            name: 'core_log'
            # Messenger transport DSN
            dsn: '%env(MESSENGER_TRANSPORT_DSN)%?topic[name]=core_log'
        # Application log section
        app:
            # Mongo connection definition
            mongo:
                uri: '%env(ANZU_MONGODB_APP_LOG_URI)%'
                username: '%env(ANZU_MONGODB_APP_LOG_USERNAME)%'
                password: '%env(ANZU_MONGODB_APP_LOG_PASSWORD)%'
                database: '%env(ANZU_MONGODB_APP_LOG_DB)%'
                ssl: '%env(bool:ANZU_MONGODB_APP_LOG_SSL)%'
                collection: appLogs
        # Audit log section
        audit:
            # Mongo connection definition
            mongo:
                uri: '%env(ANZU_MONGODB_AUDIT_LOG_URI)%'
                username: '%env(ANZU_MONGODB_AUDIT_LOG_USERNAME)%'
                password: '%env(ANZU_MONGODB_AUDIT_LOG_PASSWORD)%'
                database: '%env(ANZU_MONGODB_AUDIT_LOG_DB)%'
                ssl: '%env(bool:ANZU_MONGODB_AUDIT_LOG_SSL)%'
                collection: auditLogs
            logged_methods: ['POST', 'PUT', 'PATCH', 'DELETE']
    permissions:
        # List of roles that can be assigned to user.
        roles: [ROLE_USER, ROLE_ADMIN]
        # Default list of grants that can be assigned to all permissions.
        default_grants: [2, 0]
        config:
            # List of arbitrary subjects of permission.
            app_article:
                # List of subject's actions with default grants:
                create:
                update:
                # Action with non-default grants for permission app_article_delete:
                delete:
                    grants: [0, 1, 2]
            app_post:
                create:
        translation:
            subjects:
                # Translation of all unique subjects defined in config above.
                app_article:
                    # Feel free to add more languages.
                    en: Article
                    sk: Článok
                app_post:
                  en: Post
                  sk: Príspevok
            actions:
                # Translation of all unique actions in all subjects defined in config above.
                create:
                  en: Create
                  sk: Vytvor
                update:
                  en: Update
                  sk: Uprav
                delete:
                  en: Delete
                  sk: Zmaž
            roles:
                # Translation of all roles provided in configuration above.
                ROLE_USER:
                    en: User
                    sk: Užívateľ
                ROLE_ADMIN:
                    en: Admin
                    sk: Administrátor

设置权限及其管理

请参阅 权限管理

文档

除了 AnzuSystems 自家的 serializer-bundlecontracts 之外,common-bundle 还提供许多功能,您可以在以下类别中了解它们

故障排除

一些包,如 Google SDK,需要定义全局环境变量。允许在 composer.json 中使用 putenv

"extra": {
    "runtime": {
        "use_putenv": true
    }
}