2015-08-18 00:00:26 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . 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\Console\Application ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\DependencyInjection\Container ;
use Symfony\Component\DependencyInjection\ContainerAwareTrait ;
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2015-08-18 00:00:26 +00:00
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Finder\Finder ;
2015-08-18 00:00:26 +00:00
/**
* An implementation of BundleInterface that adds a few conventions
* for DependencyInjection extensions and Console commands .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
2016-04-20 16:56:34 +00:00
abstract class Bundle implements BundleInterface
2015-08-18 00:00:26 +00:00
{
2018-11-23 12:29:20 +00:00
use ContainerAwareTrait ;
2015-08-18 00:00:26 +00:00
protected $name ;
protected $extension ;
protected $path ;
2018-11-23 12:29:20 +00:00
private $namespace ;
2015-08-18 00:00:26 +00:00
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
public function boot ()
{
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
public function shutdown ()
{
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*
* This method can be overridden to register compilation passes ,
* other extensions , ...
*/
public function build ( ContainerBuilder $container )
{
}
/**
* Returns the bundle ' s container extension .
*
* @ return ExtensionInterface | null The container extension
*
* @ throws \LogicException
*/
public function getContainerExtension ()
{
if ( null === $this -> extension ) {
2016-04-20 16:56:34 +00:00
$extension = $this -> createContainerExtension ();
2015-08-18 00:00:26 +00:00
2016-04-20 16:56:34 +00:00
if ( null !== $extension ) {
2015-08-18 00:00:26 +00:00
if ( ! $extension instanceof ExtensionInterface ) {
2018-11-23 12:29:20 +00:00
throw new \LogicException ( sprintf ( 'Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.' , \get_class ( $extension )));
2015-08-18 00:00:26 +00:00
}
// check naming convention
$basename = preg_replace ( '/Bundle$/' , '' , $this -> getName ());
$expectedAlias = Container :: underscore ( $basename );
2016-04-20 16:56:34 +00:00
2015-08-18 00:00:26 +00:00
if ( $expectedAlias != $extension -> getAlias ()) {
2018-11-23 12:29:20 +00:00
throw new \LogicException ( sprintf ( 'Users will expect the alias of the default extension of a bundle to be the underscored version of the bundle name ("%s"). You can override "Bundle::getContainerExtension()" if you want to use "%s" or another alias.' , $expectedAlias , $extension -> getAlias ()));
2015-08-18 00:00:26 +00:00
}
$this -> extension = $extension ;
} else {
$this -> extension = false ;
}
}
if ( $this -> extension ) {
return $this -> extension ;
}
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
public function getNamespace ()
{
2018-11-23 12:29:20 +00:00
if ( null === $this -> namespace ) {
$this -> parseClassName ();
}
2015-08-18 00:00:26 +00:00
2018-11-23 12:29:20 +00:00
return $this -> namespace ;
2015-08-18 00:00:26 +00:00
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
public function getPath ()
{
if ( null === $this -> path ) {
$reflected = new \ReflectionObject ( $this );
2018-11-23 12:29:20 +00:00
$this -> path = \dirname ( $reflected -> getFileName ());
2015-08-18 00:00:26 +00:00
}
return $this -> path ;
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
public function getParent ()
{
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
final public function getName ()
{
2018-11-23 12:29:20 +00:00
if ( null === $this -> name ) {
$this -> parseClassName ();
2015-08-18 00:00:26 +00:00
}
2018-11-23 12:29:20 +00:00
return $this -> name ;
2015-08-18 00:00:26 +00:00
}
/**
* 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
*/
public function registerCommands ( Application $application )
{
if ( ! is_dir ( $dir = $this -> getPath () . '/Command' )) {
return ;
}
2016-04-20 16:56:34 +00:00
if ( ! class_exists ( 'Symfony\Component\Finder\Finder' )) {
throw new \RuntimeException ( 'You need the symfony/finder component to register bundle commands.' );
}
2015-08-18 00:00:26 +00:00
$finder = new Finder ();
$finder -> files () -> name ( '*Command.php' ) -> in ( $dir );
$prefix = $this -> getNamespace () . '\\Command' ;
foreach ( $finder as $file ) {
$ns = $prefix ;
if ( $relativePath = $file -> getRelativePath ()) {
2016-04-20 16:56:34 +00:00
$ns .= '\\' . str_replace ( '/' , '\\' , $relativePath );
2015-08-18 00:00:26 +00:00
}
$class = $ns . '\\' . $file -> getBasename ( '.php' );
if ( $this -> container ) {
2018-11-23 12:29:20 +00:00
$commandIds = $this -> container -> hasParameter ( 'console.command.ids' ) ? $this -> container -> getParameter ( 'console.command.ids' ) : array ();
2015-08-18 00:00:26 +00:00
$alias = 'console.command.' . strtolower ( str_replace ( '\\' , '_' , $class ));
2018-11-23 12:29:20 +00:00
if ( isset ( $commandIds [ $alias ]) || $this -> container -> has ( $alias )) {
2015-08-18 00:00:26 +00:00
continue ;
}
}
$r = new \ReflectionClass ( $class );
if ( $r -> isSubclassOf ( 'Symfony\\Component\\Console\\Command\\Command' ) && ! $r -> isAbstract () && ! $r -> getConstructor () -> getNumberOfRequiredParameters ()) {
2018-11-23 12:29:20 +00:00
@ trigger_error ( sprintf ( 'Auto-registration of the command "%s" is deprecated since Symfony 3.4 and won\'t be supported in 4.0. Use PSR-4 based service discovery instead.' , $class ), E_USER_DEPRECATED );
2015-08-18 00:00:26 +00:00
$application -> add ( $r -> newInstance ());
}
}
}
/**
* Returns the bundle ' s container extension class .
*
* @ return string
*/
protected function getContainerExtensionClass ()
{
$basename = preg_replace ( '/Bundle$/' , '' , $this -> getName ());
return $this -> getNamespace () . '\\DependencyInjection\\' . $basename . 'Extension' ;
}
2016-04-20 16:56:34 +00:00
/**
* Creates the bundle ' s container extension .
*
* @ return ExtensionInterface | null
*/
protected function createContainerExtension ()
{
if ( class_exists ( $class = $this -> getContainerExtensionClass ())) {
return new $class ();
}
}
2018-11-23 12:29:20 +00:00
private function parseClassName ()
{
$pos = strrpos ( static :: class , '\\' );
$this -> namespace = false === $pos ? '' : substr ( static :: class , 0 , $pos );
if ( null === $this -> name ) {
$this -> name = false === $pos ? static :: class : substr ( static :: class , $pos + 1 );
}
}
2015-08-18 00:00:26 +00:00
}