Symfony\Component\DependencyInjection\ContainerBuilder::compile PHP Method

compile() public method

This method passes the container to compiler passes whose job is to manipulate and optimize the container. The main compiler passes roughly do four things: * The extension configurations are merged; * Parameter values are resolved; * The parameter bag is frozen; * Extension loading is disabled.
public compile ( )
    public function compile()
    {
        $compiler = $this->getCompiler();

        if ($this->trackResources) {
            foreach ($compiler->getPassConfig()->getPasses() as $pass) {
                $this->addObjectResource($pass);
            }
        }

        $compiler->compile($this);

        foreach ($this->definitions as $id => $definition) {
            if (!$definition->isPublic()) {
                $this->privates[$id] = true;
            }
            if ($this->trackResources && $definition->isLazy() && ($class = $definition->getClass()) && class_exists($class)) {
                $this->addClassResource(new \ReflectionClass($class));
            }
        }

        $this->extensionConfigs = array();
        $bag = $this->getParameterBag();

        parent::compile();

        $this->envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : array();
    }

Usage Example

 /**
  * @param null $configPath
  * @param array $configFilenames
  */
 function __construct($configPath = null, $configFilenames = array())
 {
     $configPath = $configPath == null ? __DIR__ . '/../../../../../../app/config' : $configPath;
     $this->container = new ContainerBuilder();
     // Load app parameters and config files into container
     $loader = new YamlFileLoader($this->container, new FileLocator($configPath));
     $loader->load('parameters.yml');
     foreach ($configFilenames as $filename) {
         $loader->load($filename);
     }
     $appName = $this->container->getParameter('application_name');
     $appVersion = $this->container->getParameter('application_version');
     parent::__construct($appName, $appVersion);
     // Set dispatcher definition, register listeners and subscribers
     $dispatcherDef = $this->container->register('event_dispatcher', 'Symfony\\Component\\EventDispatcher\\ContainerAwareEventDispatcher');
     $dispatcherDef->addArgument($this->container);
     $this->registerEventListeners();
     $this->container->compile();
     // Once container is compiled we can get the event_dispatcher from dic
     $this->dispatcher = $this->container->get('event_dispatcher');
     // Add console commands (services console.command tagged)
     foreach ($this->getTaggedCommands() as $id) {
         $command = $this->container->get($id);
         $this->add($command);
     }
 }
All Usage Examples Of Symfony\Component\DependencyInjection\ContainerBuilder::compile