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