1. Go to this page and download the library: Download danilovl/parameter-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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;
}
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)
);
}
}
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');
}
}
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';
}
}