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

Propel Di Behavior Bundle Laravel Package

c33s/propel-di-behavior-bundle

View on GitHub
Deep Wiki
Context7

C33sPropelDIBehaviorBundle

Clean Symfony2 dependency injection for your Propel models

Installation

This bundle depends on GlorpenPropelBundle. Install that one first.

Require c33s/propel-di-behavior-bundle in your composer.json file:

{
    "require": {
        "c33s/propel-di-behavior-bundle": "@stable",
    }
}

Add propel behaviors to your propel config:

# app/config/config.yml

propel:
    # ...
    
    behaviors:
        c33s_di:        vendor.c33s.propel-di-behavior-bundle.src.C33sPropelDependencyCollectorBehavior
        
        # Optional: add another "instance" for global usage that does not interfere with model-specific behavior instances
        c33s_di_global: vendor.c33s.propel-di-behavior-bundle.src.C33sPropelDependencyCollectorBehavior

Register the bundle in app/AppKernel.php:


    // app/AppKernel.php

    public function registerBundles()
    {
        return array(
            // ...

            new C33s\PropelDIBehaviorBundle\C33sPropelDIBehaviorBundle(),
        );
    }

Usage

Add behavior to your propel models - either globally (use the c33s_di_global name for that) or to a specific model. You may inject Symfony2 services or parameters into any Model or Query class. Each definition consists of the service or parameter name to inject (enclose parameters in %-chars) followed by optional getter methods and type hints for this methods, separated by colons:

  • logger:getLogger:\Psr\Log\LoggerInterface injects the Symfony2 logger service, making it accessible by $model->getLogger() (or $query->getLogger()) and providing \Psr\Log\LoggerInterface as a type hint for that getter
  • logger injects the Symfony2 logger service without an explicit getter. Use $model->getInjectedDependency('logger') to access it.
  • %locale%:getLocale:string injects the locale parameter, providing a getLocale() method for accessing it.

Example schema

<!-- my/Bundle/Resources/config/schema.xml -->

    <!-- this will inject the logger into ALL Propel model and query instances -->
    <behavior name="c33s_di_global">
        <parameter name="model" value="logger:getLogger:\Psr\Log\LoggerInterface" />
        <parameter name="query" value="logger:getLogger:\Psr\Log\LoggerInterface" />
    </behavior>

    <table name="book">
        <!-- this will inject the mailer and session into the Book instances and request_stack into BookQuery instances -->
        <behavior name="c33s_di">
            <parameter name="model" value="
                mailer:getMailer:\Swift_Mailer,
                session:getSession:\Symfony\Component\HttpFoundation\Session\Session,
            " />
            <parameter name="query" value="request_stack:getRequestStack:\Symfony\Component\HttpFoundation\RequestStack" />
        </behavior>
        
        <...>
    </table>

How it works

C33sPropelDIBehaviorBundle registers event listeners for model.create and query.create that are processed by GlorpenPropelBundle. Upon each model or query creation anonymous callbacks for each service will be injected into the classes, making sure that only the services that were specified in the schema can be accessed. The callback furthermore ensures that no services will be instantiated without actually being used.

So far this is the cleanest way I have found to inject specific Symfony2 dependencies into Propel models without messing around with the full DI container.

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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
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