2012-12-13 24 views

Trả lời

11

Theo như tôi có thể nói, hạt nhân không có sẵn ở bất kỳ đâu trong CompilerPass, theo mặc định.

Nhưng bạn có thể thêm nó vào bằng cách làm này:.

Trong AppKernel của bạn, vượt qua $ này để bó các trình biên dịch qua là trong

  • Thêm một constructor để đối tượng Bundle của bạn, mà chấp nhận Kernel dưới dạng tham số và lưu trữ nó dưới dạng thuộc tính.
  • Trong hàm Bundle :: build() của bạn, chuyển Kernel vào cá thể CompilerPass của bạn.
  • Trong CompilerPass của bạn, tại một hàm tạo để chấp nhận hạt nhân làm tham số và lưu trữ nó làm thuộc tính.
  • Sau đó, bạn có thể sử dụng $ this-> kernel này trong quá trình biên dịch của bạn.

// 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

Wow, câu trả lời tuyệt vời. – orourkedd

6

samanime gì khuyến cáo công trình nếu bạn cần phải toàn hạt nhân.

Nếu bạn chỉ quan tâm đến một số giá trị mà hạt nhân chứa, có thể đủ để chỉ sử dụng các tham số do symfony thiết lập.

Dưới đây là danh sách những người có sẵn:

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 
) 

Ví dụ, kernel.bundles chứa một danh sách của tất cả các gói đăng ký theo định dạng [bundle => class].

PS: Tôi lấy danh sách này bằng cách sử dụng trình biên dịch thông qua sau:

<?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

Nếu bạn cần, cho phép nói, 'kernel.environment', bạn có thể làm như sau: ' $ container-> getParameter ('kernel.environment') ' –

Các vấn đề liên quan