Download the PHP package shlinkio/shlink-common without Composer
On this page you can find all versions of the php package shlinkio/shlink-common. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shlinkio/shlink-common
More information about shlinkio/shlink-common
Files in shlinkio/shlink-common
Package shlink-common
Short Description Common tools used by Shlink
License MIT
Homepage https://shlink.io
Informations about the package shlink-common
Shlink Common
This library provides some utils and conventions for web apps. It's main purpose is to be used on Shlink project, but any PHP project can take advantage.
Most of the elements it provides require a PSR-11 container, and it's easy to integrate on mezzio applications thanks to the ConfigProvider
it includes.
Install
Install this library using composer:
composer require shlinkio/shlink-common
This library is also a mezzio module which provides its own
ConfigProvider
. Add it to your configuration to get everything automatically set up.
Cache
This library provides both PSR-6 and PSR-16 cache adapters, via symfony/cache.
They can be fetched via Psr\Cache\CacheItemPoolInterface
and Psr\SimpleCache\CacheInterface
.
The concrete implementation they return is different depending on your configuration:
- An
ArrayAdapter
instance when thedebug
config is set to true or when the APCu extension is not installed and thecache.redis
config is not defined. - An
ApcuAdapter
instance when nocache.redis
is defined and the APCu extension is installed. - A
RedisAdapter
instance when thecache.redis
config is defined.
The last two adapters will use the namespace defined in cache.namespace
config entry.
The three of them will allow setting a default lifetime for those entries which do not explicitly define one, picking it up from cache.default_lifetime
.
Redis support
You can allow caching to be done on a redis instance, redis cluster or redis sentinels, by defining some options under cache.redis
config.
servers
: A list of redis servers. If one is provided, it will be treated as a single instance, and otherwise, a cluster will be assumed.sentinel_service
: Lets you enable sentinel mode. When provided, the servers will be treated as sentinel instances.
Note The entries in
servers
support credentials in the form oftcp://password@my-server:6379
ortcp://username:password@my-server:6379
.
Redis publishing helper
Also, in order to support publishing in redis pub/sub, a RedisPublishingHelper
service is provided, which will use the configuration above in order to connect to the redis instance/cluster.
Middlewares
This module provides a set of useful middlewares, all registered as services in the container:
-
CloseDbConnectionMiddleware
:Should be an early middleware in the pipeline. It makes use of the EntityManager that ensure the database connection is closed at the end of the request.
It should be used when serving an app with a non-blocking IO server (like RoadRunner or FrankenPHP), which persist services between requests.
-
IpAddress
(from akrabat/ip-address-middleware package):Improves detection of the remote IP address.
The set of headers which are inspected in order to search for the address can be customized using this configuration:
-
RequestIdMiddleware
: Sets arequest-id
attribute to current request, by reading theX-Request-Id
header or falling back to an auto-generated UUID v4.It also implements monolog's
ProcessorInterface
to set the request ID asextra.request-id
.
Doctrine integration
Some doctrine-related services are provided, that can be customized via configuration:
EntityManager
The EntityManager service can be fetched using names em
or Doctrine\ORM\EntityManager
.
In any case, it will come decorated so that it is reopened automatically after having been closed.
The EntityManager can be customized using this configuration:
Connections
As well as the EntityManager, there are two Connection objects that can be fetched.
Doctrine\DBAL\Connection
: Returns the connection used by the EntityManager, as is.Shlinkio\Shlink\Common\Doctrine\NoDbNameConnection
: Returns a connection which is the same used by the EntityManager but without setting the database name. Useful to perform operations like creating the database (which would otherwise fail since the database does not exist yet).
EntityRepository factory
In order to allow multiple repositories per entity, and also to avoid the $this->em->getRepository(MyEntity::class)
pattern and instead "promote" injecting repositories, this library provides a EntityRepositoryFactory
helper class that can be used like this.
Logger
A few logger-related helpers are provided by this library.
LoggerFactory
The LoggerFactory
class is capable of creating Monolog\Logger
instances wrapping either stream handlers or rotating file handlers, which should be defined under the logger
config entry.
Every logger can have these config options:
type
: Any value from theLoggerType
enum, which will make different handlers to be injected in the logger instance.level
: Any value from monolog'sLevel
enum, which determines the minimum level of the generated logs. Defaults toLevel::Info
if not provided.line_format
: The format of the line logs to generate.add_new_line
: Whether to add an extra empty line on every log. Defaults totrue
.processors
: An optional list of extra processors to inject in the generated logger. The values in the array must be service names.destination
: Where to send logs. It defaults tophp:stdout
for stream logs, anddata/log/shlink_log.log
for file logs.
Other logger utils
This module provides some other logger-related utilities:
ExceptionWithNewLineProcessor
: A monolog processor which captures the{e}
pattern inside log messages, and prepends a new line before it, assuming you are going to replace that with an exception trace.LoggerAwareDelegatorFactory
: A ServiceManager delegator factory that checks if the service returned by previous factory is aPsr\Log\LoggerAwareInterface
instance. If it is, it sets thePsr\Log\LoggerInterface
service on it (if it was registered).ErrorLogger
: A callable which expects aPsr\Log\LoggerInterface
to be injected and uses it to log aThrowable
when invoked. It will log 5xx errors with error level and 4xx errors with debug level.ErrorHandlerListenerAttachingDelegator
: A ServiceManager delegator factory that registers all the services configured undererror_handler.listeners
as listeners for a stratigilityErrorHandler
or aProblemDetailsMiddleware
.BackwardsCompatibleMonologProcessor
: It lets you wrap monolog 2 processors withcallable(array): array
signature to make them compatible with monolog 3 and its newcallable(LogRecord): LogRecord
signature.AccessLogMiddleware
: A PSR-15 middleware which logs requests. It expects a PSR-3 logger service to be registered underAccessLogMiddleware::LOGGER_SERVICE_NAME
.
HTTP Client
A guzzle HTTP client comes preregistered, under the GuzzleHttp\Client
service name, and aliased by httpClient
.
It can be customized by adding request and response middlewares using a configuration like this:
Middlewares can be registered as static callbacks with a signature like the one from the example or as service names which resolve to a service with that same signature.
Mercure
A helper to publish updates on a mercure hub comes preregistered. You need to provide a configuration like this one:
After that, you can get the publisher from the container, and invoke it to publish updates for specific topics:
Find more info about the symfony/mercure component here: https://symfony.com/blog/symfony-gets-real-time-push-capabilities
RabbitMQ
A helper to publish updates on RabbitMQ comes preregistered. You need to provide a configuration like this one:
After that, you can get the helper from the container, and invoke it to publish updates for specific queues:
Utils
PagerfantaUtilsTrait
: A trait providing methods to get useful info fromPagerfanta\Pagerfanta
objects. It requires that you installpagerfanta/core
.Paginator
: An object extendingPagerfanta
, that makes it behave as laminas' Paginator object on regards to be able to set-1
as the max results and get all the results in that case. It requires that you installpagerfanta/core
.DateRange
: An immutable value object wrapping twoChronos
date objects that can be used to represent a time period between two dates.IpAddress
: An immutable value object representing an IP address that can be copied into an anonymized instance which removes the last octet.NamespacedStore
: Asymfony/lock
store that can wrap another store instance but making sure keys are prefixed with a namespace and namespace separator.
All versions of shlink-common with dependencies
ext-fileinfo Version *
akrabat/ip-address-middleware Version ^2.3
cakephp/chronos Version ^3.1
doctrine/orm Version ^3.2
endroid/qr-code Version ^6.0 || ^5.0
fig/http-message-util Version ^1.1
guzzlehttp/guzzle Version ^7.9
laminas/laminas-config Version ^3.9
laminas/laminas-diactoros Version ^3.3
laminas/laminas-inputfilter Version ^2.30
laminas/laminas-servicemanager Version ^3.22
lcobucci/jwt Version ^5.3
monolog/monolog Version ^3.7
php-amqplib/php-amqplib Version ^3.7
predis/predis Version ^2.2
psr/http-server-middleware Version ^1.0
ramsey/uuid Version ^4.7
shlinkio/shlink-config Version ^3.3
shlinkio/shlink-json Version ^1.2
symfony/cache Version ^7.1
symfony/lock Version ^7.1
symfony/mercure Version ^0.6
symfony/string Version ^7.1
symfony/translation-contracts Version ^3.5
symfony/var-exporter Version ^7.1