Installation Add the package via Composer (if available in a private repo or fork):
composer require alterphp/components
Note: Since the repo is archived, ensure you have a fork or local copy.
First Use Case: Dependency Injection The package leverages Symfony’s DI container. Start by bootstrapping it in a Laravel app:
// In a service provider (e.g., AppServiceProvider.php)
use Symfony\Component\DependencyInjection\ContainerBuilder;
public function register()
{
$container = new ContainerBuilder();
// Register services/components here (see Implementation Patterns)
$this->app->singleton('symfony.container', function () use ($container) {
return $container;
});
}
Key Entry Points
Component Base Class: Extend Alterphp\Component\Component for reusable logic.EventDispatcher: Use Symfony’s event system via Alterphp\Component\EventDispatcher.config/alterphp.php (if provided) or define your own.namespace App\Components;
use Alterphp\Component\Component;
class UserValidator extends Component
{
public function validate(array $data): bool
{
// Validation logic
return true;
}
}
$container->register('user.validator', UserValidator::class)
->addArgument(new Reference('some.dependency'));
$dispatcher = $container->get('event_dispatcher');
$dispatcher->dispatch('user.created', new UserCreatedEvent($user));
$dispatcher->addListener('user.created', function ($event) {
// Handle event
});
$this->app->bind('some.service', function ($app) {
return $app->make('symfony.container')->get('some.service');
});
public function store(Request $request)
{
$validator = $this->app->make('user.validator');
if (!$validator->validate($request->all())) {
// Handle error
}
}
$config = $container->getParameter('app.config');
$container->setParameter('app.config', [
'max_retries' => 3,
]);
Archived Package Risks
symfony/event-dispatcher).DI Container Conflicts
alterphp.user.validator).php artisan config:clear
PHP 5.3+ Constraints
ContainerBuilder and the container is bound in Laravel:
$this->app->has('symfony.container'); // Should return true
Custom Components
Extend Alterphp\Component\Component to add shared logic (e.g., logging, caching).
Event Subscribers Create a subscriber class:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LoggingSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return ['user.created' => 'logEvent'];
}
public function logEvent($event) { /* ... */ }
}
Register it with the dispatcher:
$dispatcher->addSubscriber(new LoggingSubscriber());
Configuration Overrides
Override Symfony parameters in Laravel’s config/services.php:
'alterphp' => [
'parameters' => [
'app.timeout' => env('APP_TIMEOUT', 30),
],
],
How can I help you explore Laravel packages today?