2011-07-07 3 views
10

मेरे पास एक लोडर बंडल है (LoaderBundle) जो एक ही निर्देशिका में अन्य बंडल पंजीकृत करना चाहिए।क्या सिमफ़ोनी 2 में गतिशील रूप से बंडल पंजीकृत करना संभव है?

/Acme/LoaderBundle/... 
/Acme/ToBeLoadedBundle1/... 
/Acme/ToBeLoadedBundle2/... 

मैं मैन्युअल रूप से हर नया बंडल AppKernel::registerBundles() में पंजीकरण (Acme निर्देशिका में) से बचने के लिए चाहते हैं। पसंदीदा रूप से मुझे प्रत्येक अनुरोध पर चलाने के लिए LoaderBundle में कुछ चाहिए और गतिशील रूप से ToBeLoadedBundle1 और ToBeLoadedBundle2 पंजीकृत करें। क्या यह संभव है?

+0

करने की कोशिश करो है PHP खुली निर्देशिका और/या स्टेट फ़ाइलों को बनाने की आवश्यकता से बचें। यहां तक ​​कि बड़े ओएस कैश के साथ यह प्रदर्शन के लिए हानिकारक है। बेशक यह विकास के लिए काफी आसान हो सकता है। – PAStheLoD

उत्तर

7

untested है, लेकिन पिछले जवाब की तरह कुछ

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

class AppKernel extends Kernel 
{ 
    public function registerBundles() 
    { 
     $bundles = array(
      new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), 
      //... default bundles 
     ); 

     if (in_array($this->getEnvironment(), array('dev', 'test'))) { 
      $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); 
      // ... debug and development bundles 
     } 

     $searchPath = __DIR__.'/../src'; 
     $finder  = new Finder(); 
     $finder->files() 
       ->in($searchPath) 
       ->name('*Bundle.php'); 

     foreach ($finder as $file) { 
      $path  = substr($file->getRealpath(), strlen($searchPath) + 1, -4); 
      $parts  = explode('/', $path); 
      $class  = array_pop($parts); 
      $namespace = implode('\\', $parts); 
      $class  = $namespace.'\\'.$class; 
      $bundles[] = new $class(); 
     } 

     return $bundles; 
    } 

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

धन्यवाद। यह मुझे समाधान को सही करने के लिए नेतृत्व किया। मुझे एहसास नहीं हुआ कि मैं सामान्य रूप से कक्षाओं के साथ काम कर सकता हूं भले ही बंडल पंजीकृत न हों। –

-2

एक छोटी सी गलती जहां यह एक साथ वर्ग को शामिल किया जाएगा निहित आप की कोशिश कर सकते/सामने, यहाँ अद्यतन कोड

foreach ($finder as $file) { 
      $path  = substr($file->getRealpath(), strrpos($file->getRealpath(), "src") + 4); 
      $parts  = explode('/', $path); 
      $class  = array_pop($parts); 
      $namespace = implode('\\', $parts); 
      $class  = $namespace.'\\'.$class; 
      //remove first slash 
      $class = substr($class, 1, -4); 
      $bundles[] = new $class(); 
     }