tfox / di-tools-bundle
Extended Depencency Injection features such as usage of injected into super class dependencies in sub classes, etc
Installs: 33
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- symfony/framework-bundle: ~2.1
Requires (Dev)
This package is auto-updated.
Last update: 2022-05-16 19:15:00 UTC
README
Add this link to your composer.json file:
"require": {
..
"tfox/di-tools-bundle": "1.0.0"
..
}
...and run php composer.json update
The next step is to add to AppKernel.php the next line:
$bundles = array(
...
new TFox\DiToolsBundle\TFoxDiToolsBundle()
...
);
Usage
Injection of a service into super class and all sub class controllers:
Here is provided a simple example of dependency injection using DiToolsBundle. For instance, we have two controllers:
-
TestBundle\Controller\SuperController : A super class controller with protected property "entityManager". Our purpose is to inject here an Entity Manager so that it would be visible in sub class controllers.
-
TestBundle\Controller\SubController, which extends a SuperController and uses its entity manager.
Our code will be like below:
//SuperController.php
namespace TestBundle\Controller;
use TFox\DiToolsBundle\Annotation as Di;
use TFox\DiToolsBundle\Controller\DiToolsInjectableInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SuperController extends Controller implements DiToolsInjectableInterface {
/**
* @Di\Inject("doctrine.orm.default_entity_manager")
* @var \Doctrine\ORM\EntityManager
*/
protected $entityManager;
}
//SubController.php
namespace TestBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Sensio;
/**
* @Sensio\Route("/sub")
*/
class SubController extends SuperController {
/**
* @Sensio\Route
* @Sensio\Template
*/
public function indexAction()
{
//Any operations with injected entity manager here. For instance we will get some article
$article = $this->entityManager->getRepository('TestBundle:Article')
->findOneById(1);
return array('article' => $article);
}
}