PHP code example of issei-m / sf-dependency-injection-plugin

1. Go to this page and download the library: Download issei-m/sf-dependency-injection-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

issei-m / sf-dependency-injection-plugin example snippets


class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins('sfDependencyInjectionPlugin');
    ...
  }
}

// Get the ServiceContainer.
$container = sfContext::getInstance()->getContainer();

// Retrieve the NewsletterManager class which was initialized with the Mailer.
$newsletterManager = $container->get('newsletter_manager');

$container = sfContext::getInstance()->getServiceContainer();
$newsletterManager = sfContext::getInstance()->getService('newsletter_manager');

$containerClass = $containerClass();

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->dispatcher->connect('service_container.build', function (sfEvent $event) {
      /** @var \Symfony\Component\DependencyInjection\ContainerBuilder $container */
      $container = $event->getSubject();
      $container->addObjectResource($this);

      // additional parameter
      $container->setParameter('foo', 'bar');

      // add extension
      $container->registerExtension(new YourExtension());

      // add compiler pass
      $container->addCompilerPass(new YourPass());
    });
  }
}