fanatov37 / actioneasekit
ActionEaseKit is a Symfony library designed for rapidly creating actions and providing convenient services for developers
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- doctrine/annotations: ^2.0
- doctrine/orm: ^3.0
- psr/log: ^3.0
- symfony/console: 6.4.*
- symfony/framework-bundle: 6.4.*
- symfony/options-resolver: 6.4.*
- symfony/process: 6.4.*
- symfony/security-bundle: ^6.4
- symfony/yaml: 6.4.*
Requires (Dev)
- ext-curl: *
- ext-json: *
- ext-pdo: *
- doctrine/doctrine-bundle: ^2.6
- doctrine/doctrine-migrations-bundle: 3.4.x-dev
- jms/serializer-bundle: ^5.0@dev
- lexik/jwt-authentication-bundle: 3.1.0
- symfony/asset: ^6.4
- symfony/dotenv: 6.4.*
- symfony/flex: ^1.0
- symfony/maker-bundle: ^1.0@dev
- symfony/monolog-bundle: ^3.7
- symfony/property-access: ^6.4
- symfony/property-info: ^6.4
- symfony/proxy-manager-bridge: ^6.4
- symfony/runtime: 6.4.*
- symfony/serializer: ^6.4
- symfony/validator: ^6.4
Conflicts
- symfony/symfony: <6.4
Replaces
README
ActionEaseKit is a Symfony library designed to help developers rapidly create actions and provide convenient services. It simplifies the development process by streamlining action creation and enhancing access to Symfony's functionality.
Key Features
- Fast action creation: Simplify the process of creating actions in Symfony with predefined service architecture.
- Service integration: Provides additional services and abstract classes to standardize actions, enhancing code reusability and maintainability.
- JSON EntityData support: Offers tools to easily work with JSON data within database columns, making it convenient to manage complex data structures.
Prerequisites
- Symfony version: 6.0 or later
- PHP version: 8.1 or later
Ensure you have these installed before proceeding with the library setup.
Installation
To start using ActionEaseKit, install it via Composer:
composer require fanatov37/actioneasekit
Usage
Example of Action
Define your controller that extends the base AbstractRequestController
provided by the library:
use ActionEaseKit\Base\Controller\AbstractRequestController; class RequestController extends AbstractRequestController { public function __construct( RequestActionService $requestActionService // add more action services... ) { parent::__construct($requestActionService); } }
Adding Route
You need to define a route for the action. Here's an example of how to add a route in your Symfony application:
api_request: path: /api/request controller: App\Controller\RequestController::indexAction
or in Controller
#[Route('/api/request', name: 'api_request')] class RequestController extends AbstractRequestController {}
Example of Action Service
Create a service that extends the AbstractActionService
. You can define various actions within this service. Use role attributes if needed for specific actions.
use ActionEaseKit\Base\Service\AbstractActionService; use ActionEaseKit\Attributes\RequiresRole; class RequestService extends AbstractActionService { public function firstAction(array $args): array { return $args; } #[RequiresRole(['ROLE_ADMIN', 'ROLE_USER'])] public function secondAction(int $id, string $name): array { return [ 'id' => $id, 'name' => $name, 'email' => $this->getUser()->getUserIdentifier(), 'roles' => $this->getUser()->getRoles() ]; } }
POST Request Example
All POST requests will be made to the /api/request
endpoint, as specified in the route configuration.
When making a request, you need to specify:
- service: The name of the service that you want to call (e.g.,
RequestService
). - action: The specific action within the service that you want to execute (e.g.,
firstAction
orsecondAction
). - arguments: The arguments required for the action, passed as an associative array.
Here’s how you can send a POST request to trigger your actions:
First Action:
{ "service": "RequestService", "action": "firstAction", "arguments": [{"id": 1, "name": "myName"}] }
Second Action:
{ "service": "RequestService", "action": "secondAction", "arguments": {"id": 1, "name": "myName"} }
Curl Example
curl -X POST http://localhost/api/request \
-H "Content-Type: application/json" \
-d '{
"service": "RequestService",
"action": "firstAction",
"arguments": [{"id": 1, "name": "myName"}]
}'
Example of Usage: IndicatorEntity (Quick Data Access from JSON Columns)
This section demonstrates how to quickly access JSON data stored in columns using the IndicatorEntity
. Let's assume we have an entity User
that includes two JSON columns: data
and activity_log
.
Example Entity: User
In this example, the User
entity extends IndicatorEntity
to leverage quick access to JSON fields, like user profiles or activity logs, using simple getter methods.
Trait DataPropertyTrait has default data json column
#[ORM\Table(name: 'users')] class User extends IndicatorEntity implements PasswordAuthenticatedUserInterface, UserInterface { use DataPropertyTrait; public const ACTIVITY_LOG_PROPERTY_NAME = 'activityLog'; // define your json fields protected const INDICATOR_DATA = [ 'profile' => [ "first_name", "last_name", "bio", "date_of_birth", "location", "interests" ], 'settings' => [ 'notifications' => ['email', 'sms'], 'privacy' => ['profile_visible', 'last_seen_visible'] ] ]; protected const INDICATOR_ACTIVITYLOG = [ "activity", "timestamp", "ip_address", "location", "device", "browser", ]; #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private int $id; // define more json fields #[ORM\Column(name: "activity_log", type: "json", nullable: true, options: ["jsonb" => true])] protected ?array $activityLog = null; }
JSON Data Structure
- data Column: Contains user profile and settings.
- activity_log Column: Tracks user activities, such as logins or password changes.
Example of data
Column
{ "profile": { "first_name": "Jane", "last_name": "Smith", "bio": "Passionate about technology and design.", "date_of_birth": "1992-05-30", "location": "San Francisco, USA", "interests": [ "design", "photography", "traveling" ] }, "settings": { "notifications": { "email": true, "sms": true }, "privacy": { "profile_visible": false, "last_seen_visible": true } } }
Example of activity_log
Column
{ "device": "MacBook Pro", "browser": "Chrome", "activity": "password_change", "location": "Tirana, Albania", "timestamp": "2024-10-22T16:45:00Z", "ip_address": "192.168.1.15" }
Quick Access to JSON Data
Here’s an example of how to quickly access data from JSON columns using the getIndicator()
method.
#[RequiresRole(['ROLE_ADMIN'])] public function secondAction(int $id, string $name) : array { /** @var User $user */ $user = $this->getUser(); //get from data columns by default. from trait DataPropertyTrait $location = $user->getIndicator('profile:location'); $notifications = $user->getIndicator('settings:notifications'); $notificationsSMS = $user->getIndicator('settings:notifications:sms'); // change property for get data from activity_log columns $user->setCurrentPropertyName(User::ACTIVITY_LOG_PROPERTY_NAME); $activityLogDevice = $user->getIndicator('device'); return [ 'id' => $id, 'name' => $name, 'email' => $this->getUser()->getUserIdentifier(), 'roles' => $this->getUser()->getRoles(), 'location' => $location, 'notification' => $notifications, 'notification_sms' => $notificationsSMS, 'device' => $activityLogDevice ]; }
Explanation of the Example:
Accessing Data from data
Column:
- By default, the
getIndicator()
method fetches data from thedata
column.profile:location
fetches the user's location (e.g., "San Francisco, USA").settings:notifications:sms
checks whether SMS notifications are enabled.
Switching to activity_log
Column:
- To access the
activity_log
column, thesetCurrentPropertyName()
method is used to point toactivityLog
. - The
getIndicator()
method then fetches data such as the device used during the latest activity.
This method greatly simplifies data retrieval from complex JSON columns, allowing quick and flexible access to nested JSON structures.