This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpKernel/Bundle/Bundle.php

136 lines
3.6 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Bundle;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Console\Application;
use Symfony\Component\Finder\Finder;
/**
* An implementation of BundleInterface that adds a few conventions
* for DependencyInjection extensions and Console commands.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
abstract class Bundle extends ContainerAware implements BundleInterface
{
protected $name;
/**
* {@inheritDoc}
*/
public function boot()
{
}
/**
* {@inheritDoc}
*/
public function shutdown()
{
}
/**
* {@inheritDoc}
*/
public function getParent()
{
return null;
}
/**
* {@inheritDoc}
*/
final public function getName()
{
if (null !== $this->name) {
return $this->name;
}
$name = get_class($this);
$pos = strrpos($name, '\\');
return $this->name = false === $pos ? $name : substr($name, $pos + 1);
}
/**
* {@inheritDoc}
*/
final public function getNormalizedPath()
{
return strtr($this->getPath(), '\\', '/');
}
/**
* Finds and registers Dependency Injection Container extensions.
*
* Override this method if your DIC extensions do not follow the conventions:
*
* * Extensions are in the 'DependencyInjection/' sub-directory
* * Extension class names ends with 'Extension'
*
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function registerExtensions(ContainerBuilder $container)
{
if (!$dir = realpath($this->getNormalizedPath().'/DependencyInjection')) {
return;
}
$finder = new Finder();
$finder->files()->name('*Extension.php')->in($dir);
$prefix = $this->getNamespace().'\\DependencyInjection';
foreach ($finder as $file) {
$class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php');
$container->registerExtension(new $class());
}
}
/**
* Finds and registers Commands.
*
* 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
*
* @param Application $application An Application instance
*/
public function registerCommands(Application $application)
{
if (!$dir = realpath($this->getNormalizedPath().'/Command')) {
return;
}
$finder = new Finder();
$finder->files()->name('*Command.php')->in($dir);
$prefix = $this->getNamespace().'\\Command';
foreach ($finder as $file) {
$r = new \ReflectionClass($prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php'));
if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
$application->add($r->newInstance());
}
}
}
/**
* Gets the Bundle directory path.
*
* @return string The Bundle absolute path
*/
abstract protected function getPath();
}