2012-10-26 3 views
4

아마도 generate : bundle 명령 (번들을 생성 한 후 AppKernel을 업데이트하라는 메시지) 또는 Composer (설치 한 종속성으로 자동로드를 업데이트 함)와 비슷한 것일 수 있습니다.symfony2에서 AppKernel을 자동으로 업데이트하는 방법이 있습니까?

generate : bundle과 비슷한 기능을 원하지만 새로운 번들을 만드는 대신 수동으로 AppKernel을 편집 할 필요없이 방금 다운로드 한 번들을 추가하려고합니다.

답변

4

기존 명령을 확장 할 수있는 방법을 찾지 못했기 때문에 기존 번들에 새 콘솔 명령을 작성하려고했습니다.

namespace Your\OriginalBundle\Command; 

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; 
use Symfony\Component\Console\Input\ArrayInput; 
use Symfony\Component\Console\Input\InputArgument; 
use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Input\InputOption; 
use Symfony\Component\Console\Output\OutputInterface; 

class AppendNewBundleCommand extends ContainerAwareCommand 
{ 
    protected $appKernel; 

    protected function configure() 
    { 
     $this 
      ->setName('yourbundle:appendnewbundle') 
      ->setDescription('Append a new bundle to the AppKernel.php') 
      ->addArgument('namespace', InputArgument::REQUIRED, 'Define your new bundle/namespace') 
     ; 
     $this->appKernel = __DIR__.'/../../../../app/AppKernel.php'; 
    } 

    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     if (!file_exists($this->appKernel)) { 
      throw new \ErrorException(sprintf("Could not locate file %s",$this->appKernel)); 
     } 
     if (!is_writable($this->appKernel)) { 
      throw new \ErrorException(sprintf('Cannot write into AppKernel (%s)',$this->appKernel)); 
     } 

     $namespace = $input->getArgument('namespace'); 
     $appContent = file_get_contents($this->appKernel); 

     $bundle = str_replace("/","\\",$namespace)."\\".str_replace("/","",$namespace); 
     $newBundle = "new {$bundle}(),"; 

     $pattern = '/\$bundles\s?=\s?array\((.*?)\);/is'; 
     preg_match($pattern, $appContent,$matches); 

     $bList = rtrim($matches[1],"\n "); 
     $e = explode(",",$bList); 
     $firstBundle = array_shift($e); 
     $tabs = substr_count($firstBundle,' '); 

     $newBList = "\$bundles = array(" 
          .$bList."\n" 
          .str_repeat(' ', $tabs).$newBundle."\n" 
         .str_repeat(' ',$tabs-1).");"; 

     file_put_contents($this->appKernel,preg_replace($pattern,$newBList,$appContent)); 
    } 
} 

당신은 지금 당장

php app/console yourbundle:appendnewbundle Your/SecondBundle 

를 수행하여 번들을 생성이 기존 번들

new Your\SecondBundle\YourSecondBundle(), 

당신이있는 경우에이 작품의 목록이 추가됩니다 후, 그것을 실행할 수 있습니다 표준 (심포니 2) AppKernel. 예는 :

<?php 

use Symfony\Component\HttpKernel\Kernel; 
use Symfony\Component\Config\Loader\LoaderInterface; 

class AppKernel extends Kernel 
{ 
    public function registerBundles() 
    { 
     $bundles = array(
      new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), 
      new Symfony\Bundle\SecurityBundle\SecurityBundle(), 
      new Symfony\Bundle\TwigBundle\TwigBundle(), 
      new Symfony\Bundle\MonologBundle\MonologBundle(), 
      new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), 
      new Symfony\Bundle\DoctrineBundle\DoctrineBundle(), 
      new Symfony\Bundle\AsseticBundle\AsseticBundle(), 
      new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), 
      new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(), 
      new Ornicar\ApcBundle\OrnicarApcBundle(), 
      new Your\OriginalBundle\YourOriginalBundle(), 
     ); 

     if (in_array($this->getEnvironment(), array('dev', 'test'))) { 
      $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); 
      $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); 
      $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); 
     } 

     return $bundles; 
    } 

    public function registerContainerConfiguration(LoaderInterface $loader) 
    { 
     $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml'); 
    } 
} 
+0

덕분에 테스트하지 않았습니다. 나는 이제 막 이런 일을하려고했다. 나는 기존의 generate : bundle 코드에서 별도의 명령으로 코드를 복사하려고했다. – Jens

+1

쿨! 이 명령은 Symfony 프레임 워크에 포함되어야합니다. – caligari

+0

와우! 노력을 위해 +1하지만 당신이하는 일에 -1000! 이것은 응용 프로그램의 커널입니다! 나는 기분을 상하게하려는 것은 아니지만 때로는 어떤 것에 대한 해결책을 찾기 위해 혼란에 빠지기도한다. (그리고 나는 여기에 자신도 포함시킨다.) 그러나 우리가 얻은 작은 장점 (코드 줄을 자동으로 추가)이 사실 더 많은 문제를 야기합니다. 여기서 말하는 경우 번들 생성 - 어쨌든 어떤 코딩이 포함될 것이라는 것을 의미합니다 - 수동으로 커널에 행을 추가하는 것은 그렇게 큰 작업이 아닙니다. – jakabadambalazs

-2

기술적으로 누군가 기술적으로 가능합니다.

1

나는

use Symfony\Component\HttpKernel\KernelInterface; 
use Sensio\Bundle\GeneratorBundle\Manipulator\KernelManipulator; 
use RuntimeException; 

class SomeClass { 
    /** 
    * Register bundle in Kernel 
    * @param KernelInterface $kernel 
    * @param sting   $namespace 
    * @param sting   $bundle 
    * @return boolean 
    * @throws RuntimeException   When bundle already defined in <comment>AppKernel::registerBundles()</comment> 
    */ 
    protected function registerBundle(KernelInterface $kernel, $namespace, $bundle) 
    { 
     $manip = new KernelManipulator($kernel); 

     return $manip->addBundle($namespace.'\\'.$bundle); 
    } 
}