annotate vendor/symfony/http-kernel/Bundle/Bundle.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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@0 14 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
Chris@0 15 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@0 16 use Symfony\Component\DependencyInjection\Container;
Chris@0 17 use Symfony\Component\Console\Application;
Chris@0 18 use Symfony\Component\Finder\Finder;
Chris@0 19 use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
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@0 37 * Boots the Bundle.
Chris@0 38 */
Chris@0 39 public function boot()
Chris@0 40 {
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Shutdowns the Bundle.
Chris@0 45 */
Chris@0 46 public function shutdown()
Chris@0 47 {
Chris@0 48 }
Chris@0 49
Chris@0 50 /**
Chris@0 51 * Builds the bundle.
Chris@0 52 *
Chris@0 53 * It is only ever called once when the cache is empty.
Chris@0 54 *
Chris@0 55 * This method can be overridden to register compilation passes,
Chris@0 56 * other extensions, ...
Chris@0 57 *
Chris@0 58 * @param ContainerBuilder $container A ContainerBuilder instance
Chris@0 59 */
Chris@0 60 public function build(ContainerBuilder $container)
Chris@0 61 {
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Returns the bundle's container extension.
Chris@0 66 *
Chris@0 67 * @return ExtensionInterface|null The container extension
Chris@0 68 *
Chris@0 69 * @throws \LogicException
Chris@0 70 */
Chris@0 71 public function getContainerExtension()
Chris@0 72 {
Chris@0 73 if (null === $this->extension) {
Chris@0 74 $extension = $this->createContainerExtension();
Chris@0 75
Chris@0 76 if (null !== $extension) {
Chris@0 77 if (!$extension instanceof ExtensionInterface) {
Chris@0 78 throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
Chris@0 79 }
Chris@0 80
Chris@0 81 // check naming convention
Chris@0 82 $basename = preg_replace('/Bundle$/', '', $this->getName());
Chris@0 83 $expectedAlias = Container::underscore($basename);
Chris@0 84
Chris@0 85 if ($expectedAlias != $extension->getAlias()) {
Chris@0 86 throw new \LogicException(sprintf(
Chris@0 87 '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.',
Chris@0 88 $expectedAlias, $extension->getAlias()
Chris@0 89 ));
Chris@0 90 }
Chris@0 91
Chris@0 92 $this->extension = $extension;
Chris@0 93 } else {
Chris@0 94 $this->extension = false;
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 if ($this->extension) {
Chris@0 99 return $this->extension;
Chris@0 100 }
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * Gets the Bundle namespace.
Chris@0 105 *
Chris@0 106 * @return string The Bundle namespace
Chris@0 107 */
Chris@0 108 public function getNamespace()
Chris@0 109 {
Chris@0 110 if (null === $this->namespace) {
Chris@0 111 $this->parseClassName();
Chris@0 112 }
Chris@0 113
Chris@0 114 return $this->namespace;
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * Gets the Bundle directory path.
Chris@0 119 *
Chris@0 120 * @return string The Bundle absolute path
Chris@0 121 */
Chris@0 122 public function getPath()
Chris@0 123 {
Chris@0 124 if (null === $this->path) {
Chris@0 125 $reflected = new \ReflectionObject($this);
Chris@0 126 $this->path = dirname($reflected->getFileName());
Chris@0 127 }
Chris@0 128
Chris@0 129 return $this->path;
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Returns the bundle parent name.
Chris@0 134 *
Chris@0 135 * @return string|null The Bundle parent name it overrides or null if no parent
Chris@0 136 */
Chris@0 137 public function getParent()
Chris@0 138 {
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Returns the bundle name (the class short name).
Chris@0 143 *
Chris@0 144 * @return string The Bundle name
Chris@0 145 */
Chris@0 146 final public function getName()
Chris@0 147 {
Chris@0 148 if (null === $this->name) {
Chris@0 149 $this->parseClassName();
Chris@0 150 }
Chris@0 151
Chris@0 152 return $this->name;
Chris@0 153 }
Chris@0 154
Chris@0 155 /**
Chris@0 156 * Finds and registers Commands.
Chris@0 157 *
Chris@0 158 * Override this method if your bundle commands do not follow the conventions:
Chris@0 159 *
Chris@0 160 * * Commands are in the 'Command' sub-directory
Chris@0 161 * * Commands extend Symfony\Component\Console\Command\Command
Chris@0 162 *
Chris@0 163 * @param Application $application An Application instance
Chris@0 164 */
Chris@0 165 public function registerCommands(Application $application)
Chris@0 166 {
Chris@0 167 if (!is_dir($dir = $this->getPath().'/Command')) {
Chris@0 168 return;
Chris@0 169 }
Chris@0 170
Chris@0 171 if (!class_exists('Symfony\Component\Finder\Finder')) {
Chris@0 172 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
Chris@0 173 }
Chris@0 174
Chris@0 175 $finder = new Finder();
Chris@0 176 $finder->files()->name('*Command.php')->in($dir);
Chris@0 177
Chris@0 178 $prefix = $this->getNamespace().'\\Command';
Chris@0 179 foreach ($finder as $file) {
Chris@0 180 $ns = $prefix;
Chris@0 181 if ($relativePath = $file->getRelativePath()) {
Chris@0 182 $ns .= '\\'.str_replace('/', '\\', $relativePath);
Chris@0 183 }
Chris@0 184 $class = $ns.'\\'.$file->getBasename('.php');
Chris@0 185 if ($this->container) {
Chris@0 186 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
Chris@0 187 if ($this->container->has($alias)) {
Chris@0 188 continue;
Chris@0 189 }
Chris@0 190 }
Chris@0 191 $r = new \ReflectionClass($class);
Chris@0 192 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
Chris@0 193 $application->add($r->newInstance());
Chris@0 194 }
Chris@0 195 }
Chris@0 196 }
Chris@0 197
Chris@0 198 /**
Chris@0 199 * Returns the bundle's container extension class.
Chris@0 200 *
Chris@0 201 * @return string
Chris@0 202 */
Chris@0 203 protected function getContainerExtensionClass()
Chris@0 204 {
Chris@0 205 $basename = preg_replace('/Bundle$/', '', $this->getName());
Chris@0 206
Chris@0 207 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Creates the bundle's container extension.
Chris@0 212 *
Chris@0 213 * @return ExtensionInterface|null
Chris@0 214 */
Chris@0 215 protected function createContainerExtension()
Chris@0 216 {
Chris@0 217 if (class_exists($class = $this->getContainerExtensionClass())) {
Chris@0 218 return new $class();
Chris@0 219 }
Chris@0 220 }
Chris@0 221
Chris@0 222 private function parseClassName()
Chris@0 223 {
Chris@0 224 $pos = strrpos(static::class, '\\');
Chris@0 225 $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
Chris@0 226 if (null === $this->name) {
Chris@0 227 $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
Chris@0 228 }
Chris@0 229 }
Chris@0 230 }