theofidry/sfcontext-bundle

A context to access statically to the symfony container.


Keywords
symfony, debug, facade, symfony-bundle
License
MIT

Documentation

SfContextBundle

Package version Build Status License

A dead simple bundle to be able to access the Symfony Container statically.

Install

You can use Composer to install the bundle in your project:

composer require theofidry/sfcontext-bundle

Then, enable the bundle by updating your app/AppKernel.php file to enable the bundle:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Fidry\SfContextBundle\SfContextBundle(),
    );

    return $bundles;
}

Usage

There are two cases where you might use this bundle:

  1. If you don't like dependency injection
  2. For quick & dirty debugging where you can't afford to do a dump
class DummyService
{
    public function foo()
    {
        // Do something

        SfContext::get('logger')->debug('it worked');

        // Do something else
    }
}

Credits

I got the original idea from Laravel facades although the idea is actually not new and you can find it in the Symfony world as sfContext and which has been ported to Symfony 2.x with sfContextBundle.