annotate vendor/symfony/http-kernel/Bundle/Bundle.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpKernel\Bundle;
Chris@0 13
Chris@17 14 use Symfony\Component\Console\Application;
Chris@17 15 use Symfony\Component\DependencyInjection\Container;
Chris@0 16 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
Chris@0 17 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@17 18 use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
Chris@0 19 use Symfony\Component\Finder\Finder;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * An implementation of BundleInterface that adds a few conventions
Chris@0 23 * for DependencyInjection extensions and Console commands.
Chris@0 24 *
Chris@0 25 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 26 */
Chris@0 27 abstract class Bundle implements BundleInterface
Chris@0 28 {
Chris@0 29 use ContainerAwareTrait;
Chris@0 30
Chris@0 31 protected $name;
Chris@0 32 protected $extension;
Chris@0 33 protected $path;
Chris@0 34 private $namespace;
Chris@0 35
Chris@0 36 /**
Chris@17 37 * {@inheritdoc}
Chris@0 38 */
Chris@0 39 public function boot()
Chris@0 40 {
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@17 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function shutdown()
Chris@0 47 {
Chris@0 48 }
Chris@0 49
Chris@0 50 /**
Chris@17 51 * {@inheritdoc}
Chris@0 52 *
Chris@0 53 * This method can be overridden to register compilation passes,
Chris@0 54 * other extensions, ...
Chris@0 55 */
Chris@0 56 public function build(ContainerBuilder $container)
Chris@0 57 {
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Returns the bundle's container extension.
Chris@0 62 *
Chris@0 63 * @return ExtensionInterface|null The container extension
Chris@0 64 *
Chris@0 65 * @throws \LogicException
Chris@0 66 */
Chris@0 67 public function getContainerExtension()
Chris@0 68 {
Chris@0 69 if (null === $this->extension) {
Chris@0 70 $extension = $this->createContainerExtension();
Chris@0 71
Chris@0 72 if (null !== $extension) {
Chris@0 73 if (!$extension instanceof ExtensionInterface) {
Chris@17 74 throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', \get_class($extension)));
Chris@0 75 }
Chris@0 76
Chris@0 77 // check naming convention
Chris@0 78 $basename = preg_replace('/Bundle$/', '', $this->getName());
Chris@0 79 $expectedAlias = Container::underscore($basename);
Chris@0 80
Chris@0 81 if ($expectedAlias != $extension->getAlias()) {
Chris@17 82 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()));
Chris@0 83 }
Chris@0 84
Chris@0 85 $this->extension = $extension;
Chris@0 86 } else {
Chris@0 87 $this->extension = false;
Chris@0 88 }
Chris@0 89 }
Chris@0 90
Chris@0 91 if ($this->extension) {
Chris@0 92 return $this->extension;
Chris@0 93 }
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@17 97 * {@inheritdoc}
Chris@0 98 */
Chris@0 99 public function getNamespace()
Chris@0 100 {
Chris@0 101 if (null === $this->namespace) {
Chris@0 102 $this->parseClassName();
Chris@0 103 }
Chris@0 104
Chris@0 105 return $this->namespace;
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@17 109 * {@inheritdoc}
Chris@0 110 */
Chris@0 111 public function getPath()
Chris@0 112 {
Chris@0 113 if (null === $this->path) {
Chris@0 114 $reflected = new \ReflectionObject($this);
Chris@17 115 $this->path = \dirname($reflected->getFileName());
Chris@0 116 }
Chris@0 117
Chris@0 118 return $this->path;
Chris@0 119 }
Chris@0 120
Chris@0 121 /**
Chris@17 122 * {@inheritdoc}
Chris@0 123 */
Chris@0 124 public function getParent()
Chris@0 125 {
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@17 129 * {@inheritdoc}
Chris@0 130 */
Chris@0 131 final public function getName()
Chris@0 132 {
Chris@0 133 if (null === $this->name) {
Chris@0 134 $this->parseClassName();
Chris@0 135 }
Chris@0 136
Chris@0 137 return $this->name;
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Finds and registers Commands.
Chris@0 142 *
Chris@0 143 * Override this method if your bundle commands do not follow the conventions:
Chris@0 144 *
Chris@0 145 * * Commands are in the 'Command' sub-directory
Chris@0 146 * * Commands extend Symfony\Component\Console\Command\Command
Chris@0 147 */
Chris@0 148 public function registerCommands(Application $application)
Chris@0 149 {
Chris@0 150 if (!is_dir($dir = $this->getPath().'/Command')) {
Chris@0 151 return;
Chris@0 152 }
Chris@0 153
Chris@0 154 if (!class_exists('Symfony\Component\Finder\Finder')) {
Chris@0 155 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
Chris@0 156 }
Chris@0 157
Chris@0 158 $finder = new Finder();
Chris@0 159 $finder->files()->name('*Command.php')->in($dir);
Chris@0 160
Chris@0 161 $prefix = $this->getNamespace().'\\Command';
Chris@0 162 foreach ($finder as $file) {
Chris@0 163 $ns = $prefix;
Chris@0 164 if ($relativePath = $file->getRelativePath()) {
Chris@0 165 $ns .= '\\'.str_replace('/', '\\', $relativePath);
Chris@0 166 }
Chris@0 167 $class = $ns.'\\'.$file->getBasename('.php');
Chris@0 168 if ($this->container) {
Chris@17 169 $commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : [];
Chris@0 170 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
Chris@14 171 if (isset($commandIds[$alias]) || $this->container->has($alias)) {
Chris@0 172 continue;
Chris@0 173 }
Chris@0 174 }
Chris@0 175 $r = new \ReflectionClass($class);
Chris@0 176 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
Chris@14 177 @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);
Chris@14 178
Chris@0 179 $application->add($r->newInstance());
Chris@0 180 }
Chris@0 181 }
Chris@0 182 }
Chris@0 183
Chris@0 184 /**
Chris@0 185 * Returns the bundle's container extension class.
Chris@0 186 *
Chris@0 187 * @return string
Chris@0 188 */
Chris@0 189 protected function getContainerExtensionClass()
Chris@0 190 {
Chris@0 191 $basename = preg_replace('/Bundle$/', '', $this->getName());
Chris@0 192
Chris@0 193 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
Chris@0 194 }
Chris@0 195
Chris@0 196 /**
Chris@0 197 * Creates the bundle's container extension.
Chris@0 198 *
Chris@0 199 * @return ExtensionInterface|null
Chris@0 200 */
Chris@0 201 protected function createContainerExtension()
Chris@0 202 {
Chris@0 203 if (class_exists($class = $this->getContainerExtensionClass())) {
Chris@0 204 return new $class();
Chris@0 205 }
Chris@0 206 }
Chris@0 207
Chris@0 208 private function parseClassName()
Chris@0 209 {
Chris@0 210 $pos = strrpos(static::class, '\\');
Chris@0 211 $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
Chris@0 212 if (null === $this->name) {
Chris@0 213 $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
Chris@0 214 }
Chris@0 215 }
Chris@0 216 }