2012-12-13 14 views
5

वहाँ एक संकलक पास अंदर से गिरी पहुंचने का एक तरीका है? मैं इस की कोशिश की है:Symfony2 - एक संकलक पास से पहुंच गिरी

... 
    public function process(ContainerBuilder $container) 
    { 
     $kernel = $container->get('kernel'); 
    } 
    ... 

यह एक त्रुटि फेंकता है। क्या ऐसा करने का कोई और तरीका है?

उत्तर

11

जहां तक ​​मेरा बता सकते हैं, कर्नेल नहीं उपलब्ध एक CompilerPass में कहीं भी, डिफ़ॉल्ट रूप से है।

लेकिन आप ऐसा करने से उस में जोड़ सकते हैं:। अपने AppKernel में

, $ इस बंडल संकलक पास में है के पास

  • अपने बंडल वस्तु के लिए एक निर्माता जोड़े, जो स्वीकार करता है एक पैरामीटर के रूप में कर्नेल और इसे एक संपत्ति के रूप में स्टोर करता है।
  • अपने बंडल :: बिल्ड() फ़ंक्शन में, कर्नेल को अपने कंपाइलरपास उदाहरण पर पास करें।
  • अपने CompilerPass में, एक निर्माता पर एक पैरामीटर के रूप कर्नेल को स्वीकार करने और एक संपत्ति के रूप में यह स्टोर करने के लिए।
  • फिर आप अपने कंपाइलर पास में $-- कर्नेल का उपयोग कर सकते हैं।

// app/AppKernel.php 
new My\Bundle($this); 

// My\Bundle\MyBundle.php 
use Symfony\Component\HttpKernel\KernelInterface; 

class MyBundle extends Bundle { 
protected $kernel; 

public function __construct(KernelInterface $kernel) 
{ 
    $this->kernel = $kernel; 
} 

public function build(ContainerBuilder $container) 
{ 
    parent::build($container); 
    $container->addCompilerPass(new MyCompilerPass($this->kernel)); 
} 

// My\Bundle\DependencyInjection\MyCompilerPass.php 
use Symfony\Component\HttpKernel\KernelInterface; 

class MyCompilerPass implements CompilerPassInterface 
protected $kernel; 

public function __construct(KernelInterface $kernel) 
{ 
    $this->kernel = $kernel; 
} 

public function process(ContainerBuilder $container) 
{ 
    // Do something with $this->kernel 
} 
+0

वाह, महान जवाब। – orourkedd

6

क्या samanime काम करता है की सिफारिश की है, तो आप पूरे गिरी की जरूरत है।

तुम सिर्फ कुछ मान गिरी शामिल में रुचि रखते हैं, यह सिर्फ मापदंडों सिम्फोनी द्वारा निर्धारित का उपयोग करने के लिए पर्याप्त हो सकता है।

Array 
(
    [0] => kernel.root_dir 
    [1] => kernel.environment 
    [2] => kernel.debug 
    [3] => kernel.name 
    [4] => kernel.cache_dir 
    [5] => kernel.logs_dir 
    [6] => kernel.bundles 
    [7] => kernel.charset 
    [8] => kernel.container_class 
    [9] => kernel.secret 
    [10] => kernel.http_method_override 
    [11] => kernel.trusted_hosts 
    [12] => kernel.trusted_proxies 
    [13] => kernel.default_locale 
) 

उदाहरण के लिए, kernel.bundles प्रारूप [bundle => class] में सभी पंजीकृत बंडलों की एक सूची है:

यहाँ उपलब्ध लोगों की एक सूची है।

पुनश्च: मैं निम्नलिखित संकलक पास का उपयोग कर इस सूची दिलवाया:

<?php 
namespace Acme\InfoBundle\DependencyInjection; 

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 

class InfoCompilerPass implements CompilerPassInterface 
{ 
    public function process(ContainerBuilder $container) 
    { 
     print_r(array_values(array_filter(
      array_keys($container->getParameterBag()->all()), 
      function ($e) { 
       return strpos($e, 'kernel') === 0; 
      } 
     ))); 
     die; 
    } 
} 
+0

आप की जरूरत है, की सुविधा देता है कहते हैं, 'kernel.environment', तो आप इस तरह प्राप्त कर सकते हैं: ' $ container-> getParameter ('kernel.environment') ' –