adtechpotok/symfony-aware
Symfony “aware” interfaces and traits for quickly injecting common services (EntityManager, Doctrine, cache, logger, kernel, request stack, etc.) into your classes. Works with explicit service calls or Symfony 3.3+ autowiring.
Installation Add the package via Composer:
composer require adtechpotok/symfony-aware
Laravel users: Since this is a Symfony package, use it via illuminate/support compatibility or wrap it in a Laravel service provider.
First Use Case Inject Symfony services (e.g., Doctrine, Cache) into a class without manual dependency injection:
use Adtechpotok\Aware\Interfaces\EntityManagerAwareInterface;
use Adtechpotok\Aware\Traits\EntityManagerAwareTrait;
class UserRepository implements EntityManagerAwareInterface
{
use EntityManagerAwareTrait;
public function findActiveUsers()
{
return $this->em->createQuery('SELECT u FROM App\Entity\User u WHERE u.isActive = true')->getResult();
}
}
Where to Look First
EntityManagerAwareTrait, CacheAwareTrait, etc. (located in Traits/).EntityManagerAwareInterface, CacheAwareInterface, etc. (located in Interfaces/).Aware trait.Leveraging Traits for Dependency Injection Use traits to auto-inject Symfony services (e.g., Doctrine, Cache) without manual DI:
use Adtechpotok\Aware\Traits\CacheAwareTrait;
class AnalyticsService
{
use CacheAwareTrait;
public function getCachedData($key)
{
return $this->cache->get($key);
}
}
Service Configuration in Laravel Since Laravel uses a different DI container, wrap the package in a service provider:
// app/Providers/AwareServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Adtechpotok\Aware\Traits\EntityManagerAwareTrait;
class AwareServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->when(EntityManagerAwareTrait::class)
->needs('$em')
->give(function ($app) {
return $app->make('doctrine.orm.entity_manager');
});
}
}
Dynamic Service Binding
For reusable services (e.g., ConnectionAware), bind them dynamically:
// In a service provider
$this->app->bind('connection', function ($app) {
return $app->make('doctrine.dbal.default_connection');
});
Integration with Laravel’s Service Container
Extend Laravel’s container to support Aware traits:
// app/Providers/AppServiceProvider.php
use Adtechpotok\Aware\Interfaces\ContainerAwareInterface;
public function register()
{
$this->app->resolving(ContainerAwareInterface::class, function ($service) {
$service->setContainer($this->app);
});
}
Repository Pattern
Use EntityManagerAware for repositories:
class ProductRepository implements EntityManagerAwareInterface
{
use EntityManagerAwareTrait;
public function findBySku($sku)
{
return $this->em->getRepository(Product::class)->findOneBy(['sku' => $sku]);
}
}
Service Layer Abstraction Abstract Symfony services (e.g., Cache) in a service layer:
class DataService
{
use CacheAwareTrait;
public function fetchWithCache($key, $ttl = 3600)
{
return $this->cache->get($key, function () use ($ttl) {
return $this->fetchFreshData();
}, $ttl);
}
}
Testing
Mock Aware services in tests:
$mockEm = $this->createMock(EntityManagerInterface::class);
$repository = new ProductRepository();
$repository->setEntityManager($mockEm);
Laravel-Symfony Container Mismatch
AwareServiceProvider pattern (see Implementation Patterns).Trait Method Conflicts
Aware traits with methods of the same name (e.g., setContainer), conflicts arise.Outdated Package
symfony/dependency-injection.Missing Laravel-Specific Services
Cache, Filesystem). Extend manually:use Adtechpotok\Aware\Interfaces\CacheAwareInterface;
$this->app->when(CacheAwareInterface::class)
->needs('$cache')
->give(function ($app) {
return $app->make('cache');
});
Null Services
$this->em or $this->cache is null, the service wasn’t injected.Trait Not Applied
use the trait or implement the interface causes silent failures.*AwareInterface and use *AwareTrait.Circular Dependencies
Aware services (e.g., EntityManager needing Cache) may cause issues.singleton or bind to resolve cycles.Compose Traits Combine traits for multi-service classes:
use Adtechpotok\Aware\Traits\{EntityManagerAwareTrait, CacheAwareTrait};
class AnalyticsRepository
{
use EntityManagerAwareTrait, CacheAwareTrait;
public function getTrendingProducts()
{
$key = 'trending_products';
return $this->cache->get($key, function () {
return $this->em->createQuery('...')->getResult();
});
}
}
Custom Aware Services Extend the package to support custom services:
// Create a new trait
trait MailerAwareTrait
{
protected $mailer;
public function setMailer(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
}
// Bind in Laravel
$this->app->when(MailerAwareInterface::class)
->needs('$mailer')
->give(function ($app) {
return $app->make(Mailer::class);
});
Performance
Aware services in Laravel’s container to avoid repeated lookups:$this->app->singleton(EntityManagerInterface::class, function ($app) {
return $app->make('doctrine.orm.entity_manager');
});
Documentation
/**
* @var EntityManagerInterface $em
*/
protected $em;
How can I help you explore Laravel packages today?