Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Aware Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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.

  2. 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();
        }
    }
    
  3. Where to Look First

    • Traits: EntityManagerAwareTrait, CacheAwareTrait, etc. (located in Traits/).
    • Interfaces: EntityManagerAwareInterface, CacheAwareInterface, etc. (located in Interfaces/).
    • Service Mapping Table: The README’s table defines which Symfony service maps to which Aware trait.

Implementation Patterns

Usage Patterns

  1. 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);
        }
    }
    
  2. 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');
                     });
        }
    }
    
  3. 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');
    });
    
  4. 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);
        });
    }
    

Workflows

  1. 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]);
        }
    }
    
  2. 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);
        }
    }
    
  3. Testing Mock Aware services in tests:

    $mockEm = $this->createMock(EntityManagerInterface::class);
    $repository = new ProductRepository();
    $repository->setEntityManager($mockEm);
    

Gotchas and Tips

Pitfalls

  1. Laravel-Symfony Container Mismatch

    • The package assumes Symfony’s DI container. In Laravel, manually bind services or use a wrapper provider.
    • Fix: Use the AwareServiceProvider pattern (see Implementation Patterns).
  2. Trait Method Conflicts

    • If a class uses multiple Aware traits with methods of the same name (e.g., setContainer), conflicts arise.
    • Fix: Prefix methods or use interfaces to enforce implementation.
  3. Outdated Package

    • Last release was in 2018. May not support newer Symfony/Laravel versions.
    • Fix: Fork and update dependencies or use alternatives like symfony/dependency-injection.
  4. Missing Laravel-Specific Services

    • The package doesn’t map Laravel services (e.g., Cache, Filesystem). Extend manually:
    use Adtechpotok\Aware\Interfaces\CacheAwareInterface;
    
    $this->app->when(CacheAwareInterface::class)
              ->needs('$cache')
              ->give(function ($app) {
                  return $app->make('cache');
              });
    

Debugging

  1. Null Services

    • If $this->em or $this->cache is null, the service wasn’t injected.
    • Debug: Check if the provider is registered and services are bound correctly.
  2. Trait Not Applied

    • Forgetting to use the trait or implement the interface causes silent failures.
    • Debug: Verify the class implements *AwareInterface and use *AwareTrait.
  3. Circular Dependencies

    • Circular references between Aware services (e.g., EntityManager needing Cache) may cause issues.
    • Debug: Use Laravel’s singleton or bind to resolve cycles.

Tips

  1. 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();
            });
        }
    }
    
  2. 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);
              });
    
  3. Performance

    • Cache Aware services in Laravel’s container to avoid repeated lookups:
    $this->app->singleton(EntityManagerInterface::class, function ($app) {
        return $app->make('doctrine.orm.entity_manager');
    });
    
  4. Documentation

    • Add PHPDoc blocks to clarify injected services:
    /**
     * @var EntityManagerInterface $em
     */
    protected $em;
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager