-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle] Add configureContainer(), configureRoutes() and getConfigDir() to MicroKernelTrait #42991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
The head ref may contain hidden characters: "fwb-\u00B5k"
Merged
[FrameworkBundle] Add configureContainer(), configureRoutes() and getConfigDir() to MicroKernelTrait #42991
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,41 +27,79 @@ | |
* | ||
* @author Ryan Weaver <[email protected]> | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @method void configureRoutes(RoutingConfigurator $routes) | ||
* @method void configureContainer(ContainerConfigurator $container) | ||
*/ | ||
trait MicroKernelTrait | ||
{ | ||
/** | ||
* Adds or imports routes into your application. | ||
* | ||
* $routes->import($this->getProjectDir().'/config/*.{yaml,php}'); | ||
* $routes | ||
* ->add('admin_dashboard', '/admin') | ||
* ->controller('App\Controller\AdminController::dashboard') | ||
* ; | ||
*/ | ||
//abstract protected function configureRoutes(RoutingConfigurator $routes): void; | ||
|
||
/** | ||
* Configures the container. | ||
* | ||
* You can register extensions: | ||
* | ||
* $c->extension('framework', [ | ||
* $container->extension('framework', [ | ||
* 'secret' => '%secret%' | ||
* ]); | ||
* | ||
* Or services: | ||
* | ||
* $c->services()->set('halloween', 'FooBundle\HalloweenProvider'); | ||
* $container->services()->set('halloween', 'FooBundle\HalloweenProvider'); | ||
* | ||
* Or parameters: | ||
* | ||
* $c->parameters()->set('halloween', 'lot of fun'); | ||
* $container->parameters()->set('halloween', 'lot of fun'); | ||
*/ | ||
//abstract protected function configureContainer(ContainerConfigurator $container): void; | ||
private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void | ||
{ | ||
$configDir = $this->getConfigDir(); | ||
|
||
$container->import($configDir.'/{packages}/*.yaml'); | ||
$container->import($configDir.'/{packages}/'.$this->environment.'/*.yaml'); | ||
|
||
if (is_file($configDir.'/services.yaml')) { | ||
$container->import($configDir.'/services.yaml'); | ||
$container->import($configDir.'/{services}_'.$this->environment.'.yaml'); | ||
} else { | ||
$container->import($configDir.'/{services}.php'); | ||
} | ||
} | ||
|
||
/** | ||
* Adds or imports routes into your application. | ||
* | ||
* $routes->import($this->getConfigDir().'/*.{yaml,php}'); | ||
* $routes | ||
* ->add('admin_dashboard', '/admin') | ||
* ->controller('App\Controller\AdminController::dashboard') | ||
* ; | ||
*/ | ||
private function configureRoutes(RoutingConfigurator $routes): void | ||
{ | ||
$configDir = $this->getConfigDir(); | ||
|
||
$routes->import($configDir.'/{routes}/'.$this->environment.'/*.yaml'); | ||
$routes->import($configDir.'/{routes}/*.yaml'); | ||
|
||
if (is_file($configDir.'/routes.yaml')) { | ||
$routes->import($configDir.'/routes.yaml'); | ||
} else { | ||
$routes->import($configDir.'/{routes}.php'); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the path to the configuration directory. | ||
*/ | ||
private function getConfigDir(): string | ||
{ | ||
return $this->getProjectDir().'/config'; | ||
} | ||
|
||
/** | ||
* Gets the path to the bundles configuration file. | ||
*/ | ||
private function getBundlesPath(): string | ||
{ | ||
return $this->getConfigDir().'/bundles.php'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
|
@@ -126,23 +164,18 @@ public function registerContainerConfiguration(LoaderInterface $loader) | |
$container->addObjectResource($this); | ||
$container->fileExists($this->getBundlesPath()); | ||
|
||
try { | ||
$configureContainer = new \ReflectionMethod($this, 'configureContainer'); | ||
} catch (\ReflectionException $e) { | ||
throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureContainer(ContainerConfigurator $container): void".', get_debug_type($this), MicroKernelTrait::class), 0, $e); | ||
} | ||
|
||
$configureContainer = new \ReflectionMethod($this, 'configureContainer'); | ||
$configuratorClass = $configureContainer->getNumberOfParameters() > 0 && ($type = $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null; | ||
|
||
if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClass, true)) { | ||
$this->configureContainer($container, $loader); | ||
$configureContainer->getClosure($this)($container, $loader); | ||
|
||
return; | ||
} | ||
|
||
// the user has opted into using the ContainerConfigurator | ||
$file = (new \ReflectionObject($this))->getFileName(); | ||
/* @var ContainerPhpFileLoader $kernelLoader */ | ||
$kernelLoader = $loader->getResolver()->resolve($file = $configureContainer->getFileName()); | ||
$kernelLoader = $loader->getResolver()->resolve($file); | ||
$kernelLoader->setCurrentDir(\dirname($file)); | ||
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)(); | ||
|
||
|
@@ -152,7 +185,7 @@ public function registerContainerConfiguration(LoaderInterface $loader) | |
}; | ||
|
||
try { | ||
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader); | ||
$configureContainer->getClosure($this)(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader, $container); | ||
} finally { | ||
$instanceof = []; | ||
$kernelLoader->registerAliasesForSinglyImplementedInterfaces(); | ||
|
@@ -174,12 +207,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection | |
$kernelLoader->setCurrentDir(\dirname($file)); | ||
$collection = new RouteCollection(); | ||
|
||
try { | ||
$configureRoutes = new \ReflectionMethod($this, 'configureRoutes'); | ||
} catch (\ReflectionException $e) { | ||
throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureRoutes(RoutingConfigurator $routes): void".', get_debug_type($this), MicroKernelTrait::class), 0, $e); | ||
} | ||
|
||
$configureRoutes = new \ReflectionMethod($this, 'configureRoutes'); | ||
$configuratorClass = $configureRoutes->getNumberOfParameters() > 0 && ($type = $configureRoutes->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null; | ||
|
||
if ($configuratorClass && !is_a(RoutingConfigurator::class, $configuratorClass, true)) { | ||
|
@@ -191,7 +219,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection | |
return $routes->build(); | ||
} | ||
|
||
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment())); | ||
$configureRoutes->getClosure($this)(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment())); | ||
|
||
foreach ($collection as $route) { | ||
$controller = $route->getDefault('_controller'); | ||
|
@@ -203,12 +231,4 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection | |
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* Gets the path to the bundles configuration file. | ||
*/ | ||
private function getBundlesPath(): string | ||
{ | ||
return $this->getProjectDir().'/config/bundles.php'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3rd argument to help with #35554