Skip to content

Instantly share code, notes, and snippets.

@docteurklein
Created November 4, 2011 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save docteurklein/1339968 to your computer and use it in GitHub Desktop.
Save docteurklein/1339968 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Finder\Finder;
class SmartKernel extends Kernel
{
public function registerBundles()
{
$finder = new Finder();
$finder
->files()
->name('*Bundle.php')
->in(array(
'src',
'vendor/bundles',
'vendor/symfony/src/Symfony/Bundle',
))
/*->sort(function(\SplFileInfo $a, \SplFileInfo $b) {
return strcmp($a->getRealpath(), $b->getRealpath());
})*/
;
$bundles = array();
foreach ($finder as $file) {
$path = $this->trim($file->getPathName());
$parentPath = $this->trim($file->getPath());
foreach ($bundles as $parent => $bundle) {
if (strpos($path, $parent) === 0) {
// do not register bundles that are in already registered bundles
continue 2;
}
}
var_dump($path, $parentPath, strpos($path, $parentPath) === 0);
$namespace = str_replace('/', '\\', $path);
$bundles[$parentPath] = new $namespace;
}
return $bundles;
}
private function trim($path)
{
$path = ltrim($path, 'src/');
$path = ltrim($path, 'vendor/bundles/');
$path = ltrim($path, 'symfony/src');
$path = rtrim($path, '.php');
return $path;
}
}
@willdurand
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment