5
5
use Symfony \Components \DependencyInjection \ContainerInterface ;
6
6
use Symfony \Components \DependencyInjection \ParameterBag \ParameterBagInterface ;
7
7
use Symfony \Components \Console \Application ;
8
+ use Symfony \Components \Finder \Finder ;
8
9
9
10
/*
10
11
* This file is part of the Symfony framework.
@@ -115,32 +116,24 @@ public function getReflection()
115
116
}
116
117
117
118
/**
118
- * Registers the Commands for the console .
119
+ * Finds and registers commands for the current bundle .
119
120
*
120
121
* @param Symfony\Components\Console\Application $application An Application instance
121
122
*/
122
123
public function registerCommands (Application $ application )
123
124
{
124
- foreach ($ application ->getKernel ()->getBundleDirs () as $ dir ) {
125
- $ bundleBase = dirname (str_replace ('\\' , '/ ' , get_class ($ this )));
126
- $ commandDir = $ dir .'/ ' .basename ($ bundleBase ).'/Command ' ;
127
- if (!is_dir ($ commandDir )) {
128
- continue ;
129
- }
130
-
131
- // look for commands
132
- foreach (new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ commandDir ), \RecursiveIteratorIterator::LEAVES_ONLY ) as $ file ) {
133
- if ($ file ->isDir () || substr ($ file , -4 ) !== '.php ' ) {
134
- continue ;
135
- }
136
-
137
- $ class = str_replace ('/ ' , '\\' , $ bundleBase ).'\\Command \\' .str_replace (realpath ($ commandDir ).'/ ' , '' , basename (realpath ($ file ), '.php ' ));
125
+ if (!is_dir ($ dir = $ this ->getPath ().'/Command ' )) {
126
+ return ;
127
+ }
138
128
139
- $ r = new \ReflectionClass ($ class );
129
+ $ finder = new Finder ();
130
+ $ finder ->files ()->name ('*Command.php ' )->in ($ dir );
140
131
141
- if ($ r ->isSubclassOf ('Symfony \\Components \\Console \\Command \\Command ' ) && !$ r ->isAbstract ()) {
142
- $ application ->addCommand (new $ class ());
143
- }
132
+ $ prefix = $ this ->namespacePrefix .'\\' .$ this ->name .'\\Command \\' ;
133
+ foreach ($ finder as $ file ) {
134
+ $ r = new \ReflectionClass ($ prefix .basename ($ file , '.php ' ));
135
+ if ($ r ->isSubclassOf ('Symfony \\Components \\Console \\Command \\Command ' ) && !$ r ->isAbstract ()) {
136
+ $ application ->addCommand ($ r ->newInstance ());
144
137
}
145
138
}
146
139
}
0 commit comments