logcomex / php-utils
PHP Utils for Lumen and Laravel projects
Installs: 180 104
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 5
Forks: 1
Open Issues: 2
Requires
- php: ^7.4 || ^8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.2
Requires (Dev)
- laravel/lumen-framework: ^8.0 || ^9.0
- phpunit/phpunit: ^9.0
- dev-master
- 2.x-dev
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.x-dev
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.9
- 0.11.8
- 0.11.7
- 0.11.6
- 0.11.5
- 0.11.4
- 0.11.3
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.0
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- 0.0.2
- 0.0.1
- dev-FixCI/master
- dev-fix/treat_context_data
- dev-feature/LOG-55667.0.x
- dev-feature/LOG-55667
- dev-feature/LOG-46511
- dev-feature/LOG-36885
- dev-feature/LOG-36885-1
- dev-feature/LOG-36885-0
- dev-feature/LOG-36420_1.x
- dev-feature/LOG-36420
- dev-feature/LOG-35230
- dev-feature/LOG-35230-1.x
- dev-feature/LOG-31713_1.X
- dev-feature/LOG-31713
- dev-feature/LOG-28734_1.x
- dev-feature/LOG-28734
- dev-feature/LOG-25082
- dev-feature/LOG-25082-1x
- dev-feature/LOG-10124
- dev-feature/LOG-9610
- dev-feature/LOG-8941
- dev-dependabot/composer/phpunit/phpunit-tw-9.0or-tw-10.0
- dev-dependabot/composer/laravel/lumen-framework-tw-9.0or-tw-10.0
- dev-dependabot/add-v2-config-file
- dev-codeowner-1.x
- dev-codeowner-0.x
- dev-changelog/2.x
- dev-add_config_base_url_logs
- dev-merging-0.x-in-2.x
- dev-fix/exception-handler-signature
- dev-migration/stack_lumenphp7_to_php8
- dev-feature/testing-abandonned-composer-info
- dev-solve_logs_error_url
- dev-feature/LOG-64202
- dev-feature/LOG-64202-0.x
- dev-feature/LOG-64202-1.x
- dev-feature/LOG-60402-1.x
- dev-feature/LOG-60402-0.x
- dev-feature/LOG-60402
- dev-feature/LOG-55667.1.x
- dev-feature/log-32405_1
- dev-feature/log-32405_0
- dev-feature/0.x/ExceptionHandler/trace
- dev-feature/1.x/ExceptionHandler/trace
- dev-http-helper/0.x
- dev-fix/LOG-25072
- dev-fix/LOG-25072-1.x
- dev-feature/logger-1.x
- dev-feature/logger
- dev-feature/custom-http-status
- dev-feature/custom-http-status-0x
- dev-feature/exceptions/BaseException
- dev-feature/log-17379-2
- dev-feature/php-8-lumen-8-compatibility
- dev-fix/log-12710
- dev-fix/log-12003
- dev-fix/log-11568
- dev-feature/log-10499
- dev-feature/functionalities/ValuesExporterToJsonFunctionality
- dev-feature/middlewares/TraceMiddleware
- dev-feature/exceptions/UnavailableServiceException
- dev-hotfix/AccreditedApiKeysMiddleware/prevent-array-flip-in-null-values
- dev-hotfix/HttpHelper/mock-handler
- dev-feature/middlewares/AuthenticateMiddleware
- dev-hotfix/properties-attacher-functionality
- dev-hotfix/composer
- dev-feature/contracts/builderContract
- dev-hotfix/exception-handler/exceptionExporterToStatic
- dev-hotfix/change-the-error-log-patttern
- dev-feature/enumHelper
- dev-hotfix/AccreditedApiKeys
- dev-hotfix/adjust-tests
This package is auto-updated.
Last update: 2025-01-13 17:05:30 UTC
README
PHP Utilities for Laravel/Lumen
Installation
cd /path/to/your/project
composer require logcomex/php-utils
Utilities Packages
- Contracts
- Exceptions
- Functionalities
- Handlers
- Helpers
- Loggers
- Logs
- Middlewares
- Providers
- Singletons
Contracts
Have all the contracts (interfaces) used by the php-utils classes and other that you can use in your project.
Exceptions
Have all the exceptions used by the php-utils classes. And others that you can use in your project to segregate your errors types .
ApiException
You can use for all exceptions in 400 range http code
ApiException(string $token, string $message, int $httpCode = Response::HTTP_BAD_REQUEST, Exception $previous = null)
BadImplementationException
This exception means that a situation has been overlooked or incorrectly done by the developer.
BadImplementationException(string $token, string $message, int $httpCode = Response::HTTP_INTERNAL_SERVER_ERROR, Exception $previous = null)
SecurityException
This exception serves to point out some security problem in your application.
SecurityException(string $token, string $message, int $httpCode = Response::HTTP_FORBIDDEN, Exception $previous = null)
UnavailableServiceException
This exception serves to point out that your or other application is unavailable.
UnavailableServiceException(string $token, string $message, int $httpCode = Response::HTTP_FORBIDDEN, Exception $previous = null)
Functionalities
They're a pack of traits that can be useful in your code
PropertiesExporterFunctionality
You can use this functionality to export an array with you class properties
public static function properties(): array
PropertiesAttacherFunctionality
You can use this functionality to attach in your class properties the values passed in the parameter.
Note: To uses this functionality, you need use the PropertiesExporterFunctionality in the class.
public function attachValues(array $values): void
ValuesExporterToArrayFunctionality
You can use this functionality to easily get all the properties of class in an array.
Note: To uses this functionality, you need to do two things:
- The class must implement Illuminate\Contracts\Support\Arrayable.
- The class must use PropertiesExporterFunctionality.
public function toArray()
ValuesExporterToJsonFunctionality
You can use this functionality to easily get all the properties of class in a Json.
Note: To uses this functionality, you need to do two things:
- The class must implement Illuminate\Contracts\Support\Jsonable.
- The class must use PropertiesExporterFunctionality.
public function toJson()
Helpers
They're a pack of Helpers classes and traits.
EnumHelper
It's a trait that provide some utilities to your Enumerators classes.
use Logcomex\PhpUtils\Helpers\EnumHelper; class ProductEnum { user EnumHelper; public const EXAMPLE = 'example'; public const EXAMPLE2 = 'example2'; } $allProducts = ProductEnum::all();
Loggers
The idea of this package is provide all the Loggers classes.
LogcomexLogger
Using this class you can easily provide a log template, that is very important to integrate with Datadog.
// bootstrap/app.php $app->register(Logcomex\PhpUtils\Providers\LogcomexLoggerProvider::class); $app->withFacades(true, [ Logcomex\PhpUtils\Facades\Logger::class => 'Logger', ]); Logger::info('TOKEN-001', ['reason' => 'test',]);
Middlewares
They're a pack of Middleware classes.
AuthenticateMiddleware
It is a class that provides authentication verification. You'll need a AuthProvider configured in your application to use this Middleware.
// bootstrap/app.php $app->register(Your\Provider\AuthServiceProvider::class); // Using in global mode $app->middleware([ Logcomex\PhpUtils\Middlewares\AuthenticateMiddleware::class, ]); // Or, by specific route $app->routeMiddleware([ 'auth' => Logcomex\PhpUtils\Middlewares\AuthenticateMiddleware::class, ]);
RequestLogMiddleware
It is a class that provides a log for each request in your api. You can choose what you gonna print in the log, such as: request-header, request-server, request-payload, response-header, response-content, response-time, and trace-id.
The .env configuration:
// config/requestLog.php return [ 'enable-request-header' => env('REQUEST_LOGGER_ENABLE_REQUEST_HEADER', true), 'enable-request-server' => env('REQUEST_LOGGER_ENABLE_REQUEST_SERVER', true), 'enable-request-payload' => env('REQUEST_LOGGER_ENABLE_REQUEST_PAYLOAD', true), 'enable-response-header' => env('REQUEST_LOGGER_ENABLE_RESPONSE_HEADER', true), 'enable-response-content' => env('REQUEST_LOGGER_ENABLE_RESPONSE_CONTENT', true), 'enable-response-time' => env('REQUEST_LOGGER_ENABLE_RESPONSE_TIME', true), 'allowed-data-request-server' => explode(';', env('REQUEST_LOGGER_ALLOWED_DATA_REQUEST_SERVER', '')), ]; // bootstrap/app.php $app->configure('requestLog'); // Using in global mode $app->middleware([ Logcomex\PhpUtils\Middlewares\TracerMiddleware::class, // If you gonna use tracer, it must be above the requestlog Logcomex\PhpUtils\Middlewares\RequestLogMiddleware::class, // And after trace, you need the request log ]);
ResponseTimeLogMiddleware
It is a class that registers the response time of each request in your api. You can choose what request will be measured through calling the middleware by route.
First of all, you have to define the framework start time globally before requiring anything in your bootstrap:
// bootstrap/app.php if (!defined('GLOBAL_FRAMEWORK_START')) { define('GLOBAL_FRAMEWORK_START', microtime(true)); }
Configuration:
Your app config has to contain the key 'api-name', which will be used by this middleware for identifying what API the response time belongs to.
// bootstrap/app.php $app->configure('app');
Usage:
It is IMPORTANT that you call this middleware as the first one, so the response time calc can be the most accurate as possible.
// Using in global mode $app->middleware([ Logcomex\PhpUtils\Middlewares\ResponseTimeLogMiddleware::class, ]); // Using in specific routes $app->routeMiddleware([ 'response-time-log' => Logcomex\PhpUtils\Middlewares\ResponseTimeLogMiddleware::class, ]); Route::group( [ 'prefix' => 'example', 'middleware' => ['response-time-log'], ], function () { Route::get('responseTimeLog', 'ExampleClassName@exampleMethodName'); });
TracerMiddleware
It is a class that provides tracer functionality for your api. So your log can use this value and the HttpHelper. You must create a tracer config file in config folder. We recommend uses this middleware as global, and the first one in middlewares chain.
// config/tracer.php return [ 'headersToPropagate' => explode(';', env('TRACER_HEADERS_TO_PROPAGATE')), ]; // bootstrap/app.php $app->configure('tracer'); // Using in global mode $app->middleware([ Logcomex\PhpUtils\Middlewares\TracerMiddleware::class, // the other middlewares ]); // Or, by specific route $app->routeMiddleware([ 'tracer' => Logcomex\PhpUtils\Middlewares\TracerMiddleware::class, ]);
AccreditedApiKeysMiddleware
It is a class that provides a first level of security for your api. The best analogy is that middleware is your "API guest list".
You need to register a configuration file called accreditedApiKeys, with all the api-keys that can request your api.
Therefore, if the request does not contain the x-infra-key header or a allowed value, the API denies the request with the security exception.
It is recommended to use as a global middleware, and if you need to avoid this middleware for some routes, just insert into the public route group.
// config/accreditedApiKeys.php return [ 'api-1' => env('API_1_X_API_KEY'), 'api-2' => env('API_2_X_API_KEY'), 'api-3' => env('API_3_X_API_KEY'), ]; // bootstrap/app.php $app->configure('accreditedApiKeys'); // Using in global mode $app->middleware([ Logcomex\PhpUtils\Middlewares\AccreditedApiKeysMiddleware::class, ]); // routes/api.php $router->group(['prefix' => 'public',], function () use ($router) { $router->get('test', 'Controller@test');// this route does not need x-infra-key validation });
Providers
The idea of this package is provide some providers. For a better understanding: Lumen Providers
LogcomexLoggerProvider
You have to use this provider when you're using the Logger Facade.
// bootstrap/app.php $app->register(Logcomex\MicroservicesCore\Providers\LogcomexLoggerProvider::class); ## Singletons > They're a pack of Singleton classes. #### TracerSingleton > It is a class that provides the tracer value. ## Unit Tests Coverage Master <br> [![codecov](https://codecov.io/gh/comexio/php-utils/branch/master/graph/badge.svg)](https://codecov.io/gh/comexio/php-utils) ## TODO - [ ] HttpHelper Doc - [ ] TokenHelper Doc - [ ] Handlers Package Doc - [ ] ExceptionHandler Doc - [ ] Logs Package Doc - [ ] RequestLog Doc - [ ] Middlewares Package Doc - [ ] AllowedHostsMiddleware Doc - [ ] CorsMiddleware Doc ## Contributing - Open an issue first to discuss potential changes/additions. - Open a pull request, you need two approvals and tests need to pass Travis CI.