Bläddra i källkod

[Framework] cleaned up command registration magic

Kris Wallsmith 15 år sedan
förälder
incheckning
b8f29f18c0
1 ändrade filer med 12 tillägg och 19 borttagningar
  1. 12 19
      src/Symfony/Framework/Bundle/Bundle.php

+ 12 - 19
src/Symfony/Framework/Bundle/Bundle.php

@@ -5,6 +5,7 @@ namespace Symfony\Framework\Bundle;
 use Symfony\Components\DependencyInjection\ContainerInterface;
 use Symfony\Components\DependencyInjection\ContainerInterface;
 use Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface;
 use Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface;
 use Symfony\Components\Console\Application;
 use Symfony\Components\Console\Application;
+use Symfony\Components\Finder\Finder;
 
 
 /*
 /*
  * This file is part of the Symfony framework.
  * This file is part of the Symfony framework.
@@ -115,32 +116,24 @@ abstract class Bundle implements BundleInterface
     }
     }
 
 
     /**
     /**
-     * Registers the Commands for the console.
+     * Finds and registers commands for the current bundle.
      *
      *
      * @param Symfony\Components\Console\Application $application An Application instance
      * @param Symfony\Components\Console\Application $application An Application instance
      */
      */
     public function registerCommands(Application $application)
     public function registerCommands(Application $application)
     {
     {
-        foreach ($application->getKernel()->getBundleDirs() as $dir) {
-            $bundleBase = dirname(str_replace('\\', '/', get_class($this)));
-            $commandDir = $dir.'/'.basename($bundleBase).'/Command';
-            if (!is_dir($commandDir)) {
-                continue;
-            }
-
-            // look for commands
-            foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($commandDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
-                if ($file->isDir() || substr($file, -4) !== '.php') {
-                    continue;
-                }
-
-                $class = str_replace('/', '\\', $bundleBase).'\\Command\\'.str_replace(realpath($commandDir).'/', '', basename(realpath($file), '.php'));
+        if (!is_dir($dir = $this->getPath().'/Command')) {
+            return;
+        }
 
 
-                $r = new \ReflectionClass($class);
+        $finder = new Finder();
+        $finder->files()->name('*Command.php')->in($dir);
 
 
-                if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) {
-                    $application->addCommand(new $class());
-                }
+        $prefix = $this->namespacePrefix.'\\'.$this->name.'\\Command\\';
+        foreach ($finder as $file) {
+            $r = new \ReflectionClass($prefix.basename($file, '.php'));
+            if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) {
+                $application->addCommand($r->newInstance());
             }
             }
         }
         }
     }
     }