Skip to content

Commit

Permalink
Enhancing Bundle CLI Command loading to allow a Bundle class to manua…
Browse files Browse the repository at this point in the history
…lly add commands to the Console Application instead of trying to read them from disk.
  • Loading branch information
jwage authored and fabpot committed Apr 23, 2010
1 parent 20cc9b9 commit 9cf9430
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
32 changes: 32 additions & 0 deletions src/Symfony/Foundation/Bundle/Bundle.php
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Foundation\Bundle;

use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\Console\Application;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -33,4 +34,35 @@ public function boot(ContainerInterface $container)
public function shutdown(ContainerInterface $container)
{
}

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'));

$r = new \ReflectionClass($class);

if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract())
{
$application->addCommand(new $class());
}
}
}
}
}
34 changes: 3 additions & 31 deletions src/Symfony/Framework/WebBundle/Console/Application.php
Expand Up @@ -80,37 +80,9 @@ public function doRun(InputInterface $input, OutputInterface $output)

protected function registerCommands()
{
// search all places where there are bundles
foreach ($this->kernel->getContainer()->getParameter('kernel.bundle_dirs') as $dir)
foreach ($this->kernel->getBundles() as $bundle)
{
// search all registered bundles
foreach ($this->kernel->getBundles() as $bundle)
{
$bundleBase = dirname(str_replace('\\', '/', get_class($bundle)));
$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'));

$r = new \ReflectionClass($class);

if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract())
{
$this->addCommand(new $class());
}
}
}
$bundle->registerCommands($this);
}
}
}
}

0 comments on commit 9cf9430

Please sign in to comment.