96 lines
3.4 KiB
PHP
96 lines
3.4 KiB
PHP
<?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;
|
|
use Symfony\Component\DependencyInjection\Container;
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
|
use Symfony\Component\DependencyInjection\Kernel\AbstractBundle as BaseAbstractBundle;
|
|
|
|
/**
|
|
* An implementation of BundleInterface that adds a few conventions for DependencyInjection extensions.
|
|
*
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
*/
|
|
abstract class Bundle extends BaseAbstractBundle implements BundleInterface
|
|
{
|
|
private string $namespace;
|
|
|
|
/**
|
|
* Returns the bundle's container extension.
|
|
*
|
|
* @throws \LogicException
|
|
*/
|
|
public function getContainerExtension(): ?ExtensionInterface
|
|
{
|
|
if (!isset($this->extension)) {
|
|
$extension = $this->createContainerExtension();
|
|
|
|
if (null !== $extension) {
|
|
if (!$extension instanceof ExtensionInterface) {
|
|
throw new \LogicException(\sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_debug_type($extension)));
|
|
}
|
|
|
|
// check naming convention
|
|
$basename = preg_replace('/Bundle$/', '', $this->getName());
|
|
$expectedAlias = Container::underscore($basename);
|
|
|
|
if ($expectedAlias != $extension->getAlias()) {
|
|
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()));
|
|
}
|
|
|
|
$this->extension = $extension;
|
|
} else {
|
|
$this->extension = false;
|
|
}
|
|
}
|
|
|
|
return $this->extension ?: null;
|
|
}
|
|
|
|
public function getNamespace(): string
|
|
{
|
|
return $this->namespace ??= false === ($pos = strrpos(static::class, '\\')) ? '' : substr(static::class, 0, $pos);
|
|
}
|
|
|
|
public function getPath(): string
|
|
{
|
|
return $this->path ??= \dirname((new \ReflectionClass($this))->getFileName());
|
|
}
|
|
|
|
/**
|
|
* @deprecated since Symfony 8.1, use the #[AsCommand] attribute or the "console.command" service tag instead of overriding this method
|
|
*/
|
|
public function registerCommands(Application $application): void
|
|
{
|
|
trigger_deprecation('symfony/http-kernel', '8.1', 'The "%s::registerCommands()" method is deprecated, use the #[AsCommand] attribute or the "console.command" service tag instead of overriding this method', self::class);
|
|
}
|
|
|
|
/**
|
|
* Returns the bundle's container extension class.
|
|
*/
|
|
protected function getContainerExtensionClass(): string
|
|
{
|
|
$basename = preg_replace('/Bundle$/', '', $this->getName());
|
|
|
|
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
|
|
}
|
|
|
|
/**
|
|
* Creates the bundle's container extension.
|
|
*/
|
|
protected function createContainerExtension(): ?ExtensionInterface
|
|
{
|
|
return class_exists($class = $this->getContainerExtensionClass()) ? new $class() : null;
|
|
}
|
|
}
|