danilovl / parameter-bundle
Symfony 扩展包,方便从配置中获取参数。
v4.4.0
2024-05-26 09:33 UTC
Requires
- php: ^8.3.0
- symfony/framework-bundle: ^7.0
- symfony/twig-bundle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.2
This package is auto-updated.
Last update: 2024-09-26 10:24:39 UTC
README
ParameterBundle
关于
Symfony 扩展包,方便从配置中获取参数。
需求
- PHP 8.3 或更高版本
- Symfony 7.0 或更高版本
- TwigBundle 7.0 或更高版本
1. 安装
使用 Composer 安装 danilovl/parameter-bundle
包
composer require danilovl/parameter-bundle
如果你的应用没有自动添加,请将 ParameterBundle
添加到应用捆绑中
<?php // config/bundles.php return [ // ... Danilovl\ParameterBundle\ParameterBundle::class => ['all' => true] ];
你可以更改自己的 delimiter
。
danilovl_parameter: delimiter: '.'
2. 可用方法
<?php declare(strict_types=1); namespace Danilovl\ParameterBundle\Interfaces; interface ParameterServiceInterface { public function get( string $key, string $delimiter = null, bool $ignoreNotFound = false ): array|bool|string|int|float|UnitEnum|null; public function getString(string $key, string $delimiter = null): string; public function getStringOrNull(string $key, string $delimiter = null): ?string; public function getInt(string $key, string $delimiter = null): int; public function getIntOrNull(string $key, string $delimiter = null): ?int; public function getFloat(string $key, string $delimiter = null): float; public function getFloatOrNull(string $key, string $delimiter = null): ?float; public function getBoolean(string $key, string $delimiter = null): bool; public function getBooleanOrNull(string $key, string $delimiter = null): ?bool; public function getArray(string $key, string $delimiter = null): array; public function getArrayOrNull(string $key, string $delimiter = null): ?array; public function getUnitEnum(string $key, string $delimiter = null): UnitEnum; public function getUnitEnumOrNull(string $key, string $delimiter = null): ?UnitEnum; public function has(string $key, string $delimiter = null): bool; }
3. 使用方法
项目参数。
# config/services.yaml parameters: locale: 'en' debug: false price: 200.00 volume: 0.00 project_namespace: 'App' pagination: default: page: 1 limit: 25 google: api_key: 'AzT6Ga0A46K3pUAdQKLwr-zT6Ga0A46K3pUAdQKLwr' analytics_code: 'UA-X000000'
3.1 服务
在控制器中获取参数。
<?php declare(strict_types=1); namespace App\Controller; use Danilovl\ParameterBundle\Interfaces\ParameterServiceInterface; use Knp\Component\Pager\Pagination\PaginationInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; class BaseController extends AbstractController { protected function createPagination( Request $request, $query, int $page = null, int $limit = null, array $options = null ): PaginationInterface { $page = $page ?? $this->get(ParameterServiceInterface::class) ->getInt('pagination::default::page', '::'); $limit = $limit ?? $this->get(ParameterServiceInterface::class) ->getInt('pagination.default.limit'); $pagination = $this->get('knp_paginator'); if ($options !== null) { $pagination->setDefaultPaginatorOptions($options); } return $pagination->paginate( $query, $request->query->getInt('page', $page), $request->query->getInt('limit', $limit) ); } }
通过依赖注入获取参数。
<?php declare(strict_types=1); namespace App\Service; use Knp\Component\Pager\Pagination\PaginationInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Danilovl\ParameterBundle\Interfaces\ParameterServiceInterface; class UserService { public function __construct(private ParameterServiceInterface $parameterService) { } public function getUserRoles(): array { return $this->parameterService->getArray('user.roles'); } }
如果参数不存在,忽略 ParameterNotFoundException
。方法 get
返回 null
。
<?php declare(strict_types=1); namespace App\Service; use Knp\Component\Pager\Pagination\PaginationInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Danilovl\ParameterBundle\Interfaces\ParameterServiceInterface; class WidgetService { public function __construct(private ParameterServiceInterface $parameterService) { } public function getWidgetName(): string { return $this->parameterService->get(key: 'widget.name', ignoreNotFound: true) ?? 'default widget name'; } }
3.2 Twig 扩展
Twig 函数。
parameter_get parameter_get_string parameter_get_string_or_null parameter_get_int parameter_get_int_or_null parameter_get_float parameter_get_float_or_null parameter_get_boolean parameter_get_boolean_or_null parameter_get_array parameter_get_array_or_null parameter_get_unit_enum parameter_get_unit_enum_or_null parameter_has
在模板中检查 debug
参数。
{# templates/first.html.twig #} {% if parameter_has('debug') == true %} {#some code#} {% endif %} {% if parameter_get_string('locale') == 'en' %} {#some code#} {% endif %}
获取 google api
参数。
{# templates/first.html.twig #} {{ parameter_get('google.api_key') }} {{ parameter_get_string('google.api_key') }} {{ parameter_get_string('google.analytics_code') }} {{ parameter_get_int('pagination.default.page') }} {{ parameter_get_int('pagination.default.limit') }}
许可证
ParameterBundle 是开源软件,根据 MIT 许可证 许可。