Symfony\Component\HttpKernel\Bundle\Bundle::registerCommands PHP Method

registerCommands() public method

Override this method if your bundle commands do not follow the conventions: * Commands are in the 'Command' sub-directory * Commands extend Symfony\Component\Console\Command\Command
public registerCommands ( Application $application )
$application Symfony\Component\Console\Application An Application instance
    public function registerCommands(Application $application)
    {
        if (!is_dir($dir = $this->getPath().'/Command')) {
            return;
        }

        if (!class_exists('Symfony\Component\Finder\Finder')) {
            throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
        }

        $finder = new Finder();
        $finder->files()->name('*Command.php')->in($dir);

        $prefix = $this->getNamespace().'\\Command';
        foreach ($finder as $file) {
            $ns = $prefix;
            if ($relativePath = $file->getRelativePath()) {
                $ns .= '\\'.str_replace('/', '\\', $relativePath);
            }
            $class = $ns.'\\'.$file->getBasename('.php');
            if ($this->container) {
                $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
                if ($this->container->has($alias)) {
                    continue;
                }
            }
            $r = new \ReflectionClass($class);
            if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
                $application->add($r->newInstance());
            }
        }
    }

Usage Example

 public function registerCommands(Application $app)
 {
     if ($app instanceof FrameworkApplication) {
         $this->addConsoleHelpers($app);
     }
     return parent::registerCommands($app);
 }
All Usage Examples Of Symfony\Component\HttpKernel\Bundle\Bundle::registerCommands