1. Go to this page and download the library: Download componenta/interceptor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$executor = new InterceptingExecutor($base, $auth, $logger, $cache);
// Single invocation, one pass through the chain:
//
// auth.before → logger.before → cache.before → callable
// → cache.after → logger.after → auth.after
//
// Each interceptor's intercept() is called exactly once. Work placed before
// $handler->handle() runs on the way in; work after it runs on unwind.
use Componenta\Interceptor\Attribute\Intercept;
final class UserController
{
#[Intercept(CacheInterceptor::class, ['ttl' => 300])]
#[Intercept(AuthInterceptor::class)]
public function show(int $id): User { /* ... */ }
}
#[Respond(200, 'application/json')] // outermost — wraps the final string in a PSR-7 response
#[Serialize(context: [...])] // innermost — receives the raw return value first
public function show(int $id): User { /* ... */ }
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final readonly class WrapJson implements InterceptorInterface
{
public function intercept($ctx, $handler): mixed
{
return json_encode($handler->handle($ctx));
}
}
use Componenta\Interceptor\InterceptorInterface;
use Componenta\Interceptor\Scope;
use Componenta\Scope\ScopedInterface;
use Componenta\Scope\Scopes;
final class RespondInterceptor implements InterceptorInterface, ScopedInterface
{
public Scopes $scopes {
get => Scopes::of(Scope::HTTP);
}
// ...
}
use Componenta\Interceptor\CallableContext;
use Componenta\Interceptor\Scope;
$context = $context->withAttribute(CallableContext::SCOPE_ATTRIBUTE, Scope::HTTP);
new InterceptingExecutor(
$container->get(CallableExecutorInterface::class),
new ParameterResolvingInterceptor($parametersResolver), // outermost — runs first
$container->get(AttributeInterceptor::class),
);
new \Componenta\Interceptor\ConfigProvider();
use Componenta\Interceptor\AttributeInterceptor;
use Componenta\Interceptor\ConfigKey;
return [
ConfigKey::HTTP_INTERCEPTORS => [
AttributeInterceptor::class,
],
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.