jtc-solutions / core
Core bundle for every application in JTC that follows standard processes.
Installs: 33
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.3
- doctrine/orm: ^3.3
- jtc-solutions/helpers: ^0.2.0
- nelmio/api-doc-bundle: ^5.0
- ramsey/uuid: ^4.7
- symfony/framework-bundle: 7.2.*
- symfony/http-foundation: ^7.2
- symfony/property-info: ^7.2
- symfony/serializer: ^7.2
- symfony/translation: ^7.2
- symfony/validator: ^7.2
- symfony/yaml: ^7.2
Requires (Dev)
- grifart/phpstan-oneline: ^0.5.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
- slevomat/coding-standard: ^8.16
- symplify/easy-coding-standard: ^12.1
This package is not auto-updated.
Last update: 2025-04-24 18:38:29 UTC
README
This bundle provides a foundational set of services, base classes, and conventions used as a "skeleton" for most PHP Symfony projects developed at JTC Solutions. It aims to streamline development by offering reusable components for common tasks like CRUD operations, exception handling, and API documentation.
Installation
-
Require the bundle using Composer:
composer require jtc-solutions/core-bundle
-
Enable the Bundle: Add the bundle to your
config/bundles.php
file:<?php return [ // ... other bundles JtcSolutions\Core\JtcSolutionsCoreBundle::class => ['all' => true], ];
Usage
-
Entities:
- Your application entities should implement
JtcSolutions\Core\Entity\IEntity
. - Use
Ramsey\Uuid\UuidInterface
for primary identifiers.
- Your application entities should implement
-
Repositories:
- Implement
JtcSolutions\Core\Repository\IEntityRepository
. - Extend
JtcSolutions\Core\Repository\BaseRepository
.
- Implement
-
Services:
- Implement
JtcSolutions\Core\Service\IEntityService
for entities requiring CRUD operations. - Extend
JtcSolutions\Core\Service\BaseEntityService
to leverage built-in logic for handling creation, updates, and deletion via abstract methods (mapDataAndCallCreate
,mapDataAndCallUpdate
,delete
). - Inject the corresponding repository into your service.
- Implement
-
Controllers:
- Extend
JtcSolutions\Core\Controller\BaseController
for AbstractController from Symfony, with the possibility of future improvements. - For controllers handling standard entity CRUD, extend
JtcSolutions\Core\Controller\BaseEntityCRUDController
. Inject the correspondingIEntityService
. This base controller provides protected methods (handleCreate
,handleUpdate
,handleDelete
) that validate request DTOs and call the service.
- Extend
-
Request Data Transfer Objects (DTOs):
- For request bodies used in create/update operations handled by
BaseEntityCRUDController
/BaseEntityService
, implementJtcSolutions\Core\Dto\IEntityRequestBody
. - Use Symfony's Validator component constraints on your DTO properties.
- For request bodies used in create/update operations handled by
-
Exception Handling & Translations:
- The
ExceptionListener
is enabled by default and will catch exceptions. - It automatically converts
TranslatableException
types and standard HTTP exceptions intoErrorRequestJsonResponse
. - Provide translations for the exception keys in the configured translation domain (default:
exceptions
). Keys follow the patterncore.<type>.title
,core.<type>.message
for standard HTTP errors andcustom.<translation_code>.title
,custom.<translation_code>.message
forTranslatableException
types. Example translation file (translations/exceptions.en.yaml
):core.not_found.title: 'Resource Not Found' core.not_found.message: 'The requested resource could not be found.' custom.entity_not_found.title: 'Entity Not Found' custom.entity_not_found.message: 'The specific item you were looking for does not exist.' custom.entity_already_exists.title: 'Conflict' custom.entity_already_exists.message: 'An item with the provided details already exists.' # ... add other translations
- The
-
Parameter Resolvers:
- Entity Resolver: If enabled (default), controller actions can directly type-hint arguments with an
IEntity
implementation (e.g.,public function show(Product $product)
). If a route parameter{product}
exists, the resolver will fetch theProduct
using its repository'sfind()
method. - UUID Resolver: If enabled (default), controller actions can type-hint arguments with
UuidInterface
(e.g.,public function list(UuidInterface $categoryId)
). The resolver will attempt to create aUuid
object from a query parameter with the same name (?categoryId=...
).
- Entity Resolver: If enabled (default), controller actions can directly type-hint arguments with an
Configuration
Configure the bundle under the jtc_solutions_core
key in your config/packages/jtc_solutions_core.yaml
(or any other config file).
# config/packages/jtc_solutions_core.yaml jtc_solutions_core: # Parameter Resolver Configuration param_resolvers: uuid_resolver: # Enable/disable the UuidQueryParamResolver (default: true) enable: true entity_resolver: # Enable/disable the EntityParamResolver (default: true) enable: true # Listener Configuration listeners: exception_listener: # Enable/disable the ExceptionListener (default: true) enable: true # Set the translation domain for exception messages (default: 'exceptions') translation_domain: 'exceptions' # OpenAPI / NelmioApiDocBundle Integration open_api: property_describers: uuid_interface_property_describer: # Enable/disable the describer for UuidInterface (default: true) enable: true