This symfony bundle provides abstractions for many common cases., (*1)
Installation
Open a command console, enter your project directory and execute:, (*2)
$ composer require openium/symfony-toolkit
For Symfony 7 use the v4, (*3)
For Symfony 6 use the v3, (*4)
For Symfony < 6 use the v2, (*5)
Usage
AbstractController
Add 2 protected methods for controllers :, (*6)
- getContentFromRequest: get json body from request
- getMultipartContent: get json body from multipart request
- extractObjectFromString: get json from string (used by getContentFromRequest and getMultipartContent)
- getFilterParameters: get filter query parameters from request
Filters
Add a class containing filters from the query parameters., (*7)
To get filters, use getFilterParameters in AbstractController., (*8)
You can also use FilterRepositoryUtils->applyFilters() to define the sort, limit and offset in queries., (*9)
Notes on filters :
- if the page parameter is passed but not the limit parameter, the limit is set to 10
- if order-by parameter is passed but not order parameter, order is set to ASC, (*10)
PaginatedResult
The PaginatedResult allow you to have a formatted result for endpoints who used filters., (*11)
ServerService
This service provide a way to get the actual server url., (*12)
Add ServerServiceInterface with dependencies injection and use the method getBasePath()
from it., (*13)
function myFunc(ServerServiceInterface $serverService): mixed
{
// ...
$basePath = $serverService->getBasePath();
// ...
}
FileUploaderService
This service help you to manage an entity with a uploaded file reference.
Caution, this service allow only one upload property., (*14)
First, implements your entity with WithUploadInterface., (*15)
Next, you can use the WithUploadTrait, which contains certain methods and properties required by the interface., (*16)
Then inject into your entity event listener the FileUploaderServiceInterface service., (*17)
Finally, use the service like that :, (*18)
-
prepareUploadPath in prePersist and preUpdate to set entity properties before persist in database
$fileUploaderService->prepareUploadPath($entity);
-
upload postPersist and postUpdate to move upload to right directory
$fileUploaderService->upload($entity);
-
removeUpload postPersist and postRemove to delete upload file
$fileUploaderService->removeUpload($entity);
AtHelper
Allow you to execute some commands with Unix AT command., (*19)
// $cmd command to execute
// $timestamp when the command will be executed
// $path path where the at creation command will be executed
// &$result result of at
$output = $atHelper->createAtCommandFromPath($cmd, $timestamp, $path, $result);
// get at job number
$jobNumber = $atHelper->extractJobNumberFromAtOutput($output);
- to remove existing AT job, save the jobNumber from extractJobNumberFromAtOutput() method and use it with
removeAtCommand() method.
$removeSuccess = $atHelper->removeAtCommand($jobNumber);
DoctrineExceptionHandlerService
Transform doctrine exceptions into HttpException., (*20)
In most cases, the exception will be a BadRequestHttpException., (*21)
But if the database error refers to a conflict, the method will throw a ConflictHttpException., (*22)
To use it, you need to inject DoctrineExceptionHandlerServiceInterface service., (*23)
try {
$this->em->persist($y);
$this->em->flush();
} catch (\Exception $e) {
$this->doctrineExceptionHandlerService->toHttpException($e);
}
Work fine with doctrine exceptions but not with other/custom exceptions, (*24)
Transform exceptions to json Response.
Exception's response is now generic, which means you can give your own code, text and message to the response.
, (*25)
In the case which you want to add secific code, firstly override the service of the bundle.
You have to add your own service in config/services.yaml., (*26)
For example:, (*27)
openium_symfony_toolkit.exception_format:
class: App\Service\ExceptionFormatService
arguments:
- '@kernel'
public: true
```
Then, you need to create an ExceptionFormatService in your project and extends the one in the bundle.
2 methods and one property can be override :
- `genericExceptionResponse` which will be defining each part of the exception: `$code, $text, $message`.
- `addKeyToErrorArray` which will add keys in final json object
- `$jsonKeys` to override final json keys
#### Example
```php
<?php
namespace App\Service;
use Openium\SymfonyToolKitBundle\Service\ExceptionFormatService as BaseExceptionFormatService;
use Openium\SymfonyToolKitBundle\Service\ExceptionFormatServiceInterface;
class ExceptionFormatService extends BaseExceptionFormatService implements ExceptionFormatServiceInterface {
protected array $jsonKeys = [
'code' => 'statusCode',
'text' => 'statusText',
'message' => 'message',
];
public function genericExceptionResponse(Exception $exception): array
{
// You define conditions and exceptions[ExceptionFormatExtendService.php](Tests%2FService%2FExceptionFormatExtendService.php) you want here
if ($exception instanceof MyException) {
$code = 123;
$text = 'This is my custom exception text';
$message = $text;
return [$code, $text, $message];
}
// Or use the default method in the toolkit
return parent::genericExceptionResponse($exception);
}
public function addKeyToErrorArray(array $error, Exception $exception): array
{
if ($exception instanceof MyException) {
$error['MyNewKey'] = 'value';
}
return $error;
}
}
The exception you formatted is going to be used in the method formatExceptionResponse
.
This way you can handle a custom exception., (*28)
$response = $this->exceptionFormat->formatExceptionResponse($exception);
PathExceptionListener
The listener catch kernel exceptions and transform them into HttpException thanks to ExceptionFormatService., (*29)
It is enabled by default and have this configuration :, (*30)
parameters:
openium_symfony_toolkit.kernel_exception_listener_enable: true
openium_symfony_toolkit.kernel_exception_listener_path: '/api'
openium_symfony_toolkit.kernel_exception_listener_class: 'Openium\SymfonyToolKitBundle\EventListener\PathExceptionListener'
it use the ExceptionFormatService to format automatically the kernel exceptions
only for the routes defined in exception_listener_path parameter, (*31)
MemoryUtils
Use to display memory usage or juste bytes into human-readable string., (*32)
$str = MemoryUtils::convert(1024);
// $str = '1 kb';
$phpMemory = MemoryUtils::getMemoryUsage();
// apply convert() to actual php memory usage
Use to extract types data from array with specific key, (*33)
$myString = ContentExtractorUtils::getString($content, $key);
With option to allow null value, set a default value and set if value is required., (*34)
List of methods :, (*35)
- getString
- getBool
- getInt
- getFloat
- getDateTimeInterface
- getArray
All methods throws 400 HTTP error with correct message if the value is missing or is not with the right type (depends of
parameters), (*36)
Behind all these methods are control methods., (*37)
List of check methods :, (*38)
- checkKeyExists
- checkKeyIsString
- checkKeyIsBoolean
- checkKeyIsInt
- checkKeyIsFloat
- checkKeyIsArray
Methods checkKeyIs{type} use checkKeyExists()., (*39)
All the methods in this class are static., (*40)
DateStringUtils
Provide a static method to get date from string :, (*41)
public static function getDateTimeFromString(
string $dateString,
?string $format = null,
?DateTimeZone $timeZone = null
): DateTime | false
If no format has been supplied, the method attempts to determine the correct date format., (*42)
Two formats can be detected:, (*43)
- ATOM
'Y-m-d\TH:i:sP'
- ISO8601
'Y-m-d\TH:i:sO'
If no format is detected, the method falls back to the 'Y-m-d'
format and return false if the string can't be parse as
DateTime., (*44)