PHPackages                             openium/symfony-toolkit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. openium/symfony-toolkit

ActiveSymfony-bundle[API Development](/categories/api)

openium/symfony-toolkit
=======================

Openium Web Toolkit for Symfony Project

4.4.1(5mo ago)29.0k↓33.3%MITPHPPHP ^8.2

Since May 11Pushed 5mo ago4 watchersCompare

[ Source](https://github.com/openium/symfony-toolkit)[ Packagist](https://packagist.org/packages/openium/symfony-toolkit)[ Docs](https://www.openium.fr)[ RSS](/packages/openium-symfony-toolkit/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (32)Versions (80)Used By (0)

Symfony toolkit
===============

[](#symfony-toolkit)

This symfony bundle provides abstractions for many common cases.

Installation
------------

[](#installation)

Open a command console, enter your project directory and execute:

```
$ composer require openium/symfony-toolkit
```

> For Symfony 7 use the v4

> For Symfony 6 use the v3

> For Symfony &lt; 6 use the v2

Usage
-----

[](#usage)

### AbstractController

[](#abstractcontroller)

Add 2 protected methods for controllers :

- 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

[](#filters)

Add a class containing filters from the query parameters.

To get filters, use getFilterParameters in AbstractController.

You can also use FilterRepositoryUtils-&gt;applyFilters() to define the sort, limit and offset in queries.

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

### PaginatedResult

[](#paginatedresult)

The PaginatedResult allow you to have a formatted result for endpoints who used filters.

### ServerService

[](#serverservice)

This service provide a way to get the actual server url.

Add ServerServiceInterface with dependencies injection and use the method `getBasePath()` from it.

```
    function myFunc(ServerServiceInterface $serverService): mixed
    {
        // ...
        $basePath = $serverService->getBasePath();
        // ...
    }
```

---

### FileUploaderService

[](#fileuploaderservice)

This service help you to manage an entity with a uploaded **file reference. Caution, this service allow only one upload property**.

First, implements your entity with WithUploadInterface.

Next, you can use the WithUploadTrait, which contains certain methods and properties required by the interface.

Then inject into your entity event listener the FileUploaderServiceInterface service.

Finally, use the service like that :

- *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

[](#athelper)

Allow you to execute some commands with Unix AT command.

- To create a new AT job :

```

    // $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

[](#doctrineexceptionhandlerservice)

Transform doctrine exceptions into HttpException.

In most cases, the exception will be a BadRequestHttpException.

But if the database error refers to a conflict, the method will throw a ConflictHttpException.

To use it, you need to inject DoctrineExceptionHandlerServiceInterface service.

```
        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

---

### ExceptionFormatService

[](#exceptionformatservice)

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.

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.

For example:

```
    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

[](#example)

```
