|
| 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\Bundle\FrameworkBundle\Kernel; |
| 13 | + |
| 14 | +use Symfony\Component\Config\Loader\Loader; |
| 15 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 18 | +use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
| 19 | +use Symfony\Component\HttpKernel\Kernel; |
| 20 | + |
| 21 | +/** |
| 22 | + * A Kernel that allows you to configure services. |
| 23 | + * |
| 24 | + * @author Ryan Weaver <ryan@knpuniversity.com> |
| 25 | + */ |
| 26 | +abstract class MicroKernel extends Kernel |
| 27 | +{ |
| 28 | + private $bundlesRegistered = false; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var BundleInterface[] |
| 32 | + */ |
| 33 | + private $bundlesToRegister = array(); |
| 34 | + |
| 35 | + /** |
| 36 | + * An associative array of bundle names and their configuration arrays. |
| 37 | + * |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + private $bundleConfiguration = array(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Call $this->addBundle() to add (and optionally configure) your bundles. |
| 44 | + */ |
| 45 | + abstract protected function configureBundles(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Add any service definitions to your container. |
| 49 | + * |
| 50 | + * @param ContainerBuilder $c |
| 51 | + * @param LoaderInterface $loader |
| 52 | + */ |
| 53 | + abstract protected function configureServices(ContainerBuilder $c, LoaderInterface $loader); |
| 54 | + |
| 55 | + /** |
| 56 | + * Adds a bundle to the kernel, which can optionally be configured. |
| 57 | + * |
| 58 | + * @param BundleInterface $bundle |
| 59 | + * @param array $configuration Configuration for this bundle |
| 60 | + * |
| 61 | + * @return $this |
| 62 | + */ |
| 63 | + protected function addBundle(BundleInterface $bundle, $configuration = array()) |
| 64 | + { |
| 65 | + if ($this->bundlesRegistered) { |
| 66 | + throw new \LogicException('addBundle() cannot be called after configureBundles() is called!'); |
| 67 | + } |
| 68 | + |
| 69 | + $this->bundlesToRegister[$bundle->getName()] = $bundle; |
| 70 | + |
| 71 | + if ($configuration) { |
| 72 | + $this->bundleConfiguration[$bundle->getName()] = $configuration; |
| 73 | + } |
| 74 | + |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritdoc} |
| 80 | + */ |
| 81 | + public function registerBundles() |
| 82 | + { |
| 83 | + $this->configureBundles(); |
| 84 | + $this->bundlesRegistered = true; |
| 85 | + |
| 86 | + return array_values($this->bundlesToRegister); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Applies the bundle configuration and calls configureServices() for continued building. |
| 91 | + * |
| 92 | + * @param LoaderInterface $loader |
| 93 | + */ |
| 94 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 95 | + { |
| 96 | + if (!$loader instanceof ContainerBuilderAwareLoader) { |
| 97 | + throw new \LogicException('registerContainerConfiguration requires the LoaderInterface to be a ContainerBuilderAwareLoader.'); |
| 98 | + } |
| 99 | + |
| 100 | + /* @var ContainerBuilderAwareLoader $loader */ |
| 101 | + $builder = $loader->getContainerBuilder(); |
| 102 | + $loader = $loader->getResourceLoader(); |
| 103 | + |
| 104 | + // load the bundle configuration |
| 105 | + foreach ($this->bundleConfiguration as $bundleName => $configuration) { |
| 106 | + $extension = $this->bundlesToRegister[$bundleName]->getContainerExtension(); |
| 107 | + if (!$extension) { |
| 108 | + throw new \LogicException('Bundle "%s" does not have an extension that can be configured.'); |
| 109 | + } |
| 110 | + |
| 111 | + $alias = $extension->getAlias(); |
| 112 | + $builder->loadFromExtension($alias, $configuration); |
| 113 | + } |
| 114 | + |
| 115 | + // allow the kernel to configure its services |
| 116 | + $this->configureServices( |
| 117 | + $builder, |
| 118 | + $loader |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns a loader with the ContainerBuilder embedded inside of it. |
| 124 | + * |
| 125 | + * @param ContainerInterface $container |
| 126 | + * |
| 127 | + * @return ContainerBuilderAwareLoader |
| 128 | + */ |
| 129 | + protected function getContainerLoader(ContainerInterface $container) |
| 130 | + { |
| 131 | + if (!$container instanceof ContainerBuilder) { |
| 132 | + throw new \LogicException('Only ContainerBuilder instances are supported.'); |
| 133 | + } |
| 134 | + |
| 135 | + $loader = parent::getContainerLoader($container); |
| 136 | + |
| 137 | + return new ContainerBuilderAwareLoader($container, $loader); |
| 138 | + } |
| 139 | +} |
0 commit comments