8000 [RFC][DependencyInjection][HttpKernel] Kernel as a service by ro0NL · Pull Request #19606 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[RFC][DependencyInjection][HttpKernel] Kernel as a service #19606

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\ServiceAwareDefinition;

/**
* Merges extension configs into the container builder.
Expand Down Expand Up @@ -52,6 +53,10 @@ public function process(ContainerBuilder $container)
$tmpContainer->addExpressionLanguageProvider($provider);
}

foreach ($container->getSynthetics() as $id => $service) {
$tmpContainer->set($id, $service);
}

$extension->load($config, $tmpContainer);

$container->merge($tmpContainer);
Expand Down
45 changes: 38 additions & 7 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up 8000 @@ -419,10 +419,6 @@ public function has($id)
*/
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
if (!$this->compiled) {
@trigger_error(sprintf('Calling %s() before compiling the container is deprecated since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED);
}

$id = strtolower($id);

if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
Expand All @@ -435,12 +431,24 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV

try {
$definition = $this->getDefinition($id);
if ($definition instanceof ServiceAwareDefinition) {
return $definition->getService();
}
} catch (ServiceNotFoundException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
if ($this->compiled && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
Copy link
Contributor Author
@ro0NL ro0NL Aug 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For BC, however i tend to debug unknown id's before state. Just throwing here would be fine by me.

throw $e;
}
$definition = null;
}
if (!$this->compiled) {
@trigger_error(sprintf('Calling %s() before compiling the container is deprecated for non-synthetic services since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED);
Copy link
Contributor Author
@ro0NL ro0NL Aug 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to be specific about non-synthetic services, besides that it will be triggered only once now when aliases kick in.

}
if (null === $definition) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new ServiceNotFoundException($id);
}

throw $e;
return;
}

$this->loading[$id] = true;
Expand All @@ -454,6 +462,29 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
return $service;
}

/**
* Get synthetic services.
*
* @return object[]
*/
public function getSynthetics()
{
$synthetics = array();
foreach ($this->definitions as $id => $definition) {
if ($definition instanceof ServiceAwareDefinition) {
$synthetics[$id] = $definition->getService();
}
}
foreach (parent::getServiceIds() as $id) {
if ('service_container' === $id) {
continue;
}
$synthetics[$id] = parent::get($id);
}

return $synthetics;
}

/**
* Merges a ContainerBuilder with the current ContainerBuilder configuration.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection;

/**
* Definition that is aware of its service.
*
* The definition and service must remain in sync, in a way the created service object from definition is interchangeable with the aware service object.
*
* @author Roland Franssen <franssen.roland@gmail.com>
*/
class ServiceAwareDefinition extends Definition
{
private $service;

/**
* Sets the service this definition is aware of.
*
* @param object $service The service object tight to this definition
*
* @return ServiceAwareDefinition The current instance
*/
public function setService($object)
{
$this->service = $object;

return $this;
}

/**
* Gets the aware service.
*
* @return object
*
* @throws \DomainException If the definition is not aware of a service object or the service object is invalid.
*/
public function getService()
{
if (null === $this->service) {
throw new \DomainException('A service aware definition must have a service object.');
}
$class = $this->getClass();
if (null !== $class && !$this->service instanceof $class) {
throw new \DomainException('The service object must be an instance of "'.$class.'", "'.get_class($this->service).'" given.');
}

return $this->service;
}

/**
* {@inheritdoc}
*
* @throws \BadMethodCallException When trying to prototype this definition
*/
public function setShared($shared)
{
if (!$shared) {
throw new \BadMethodCallException('A service aware definition must always be shared.');
}

return parent::setShared($shared);
}
}
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
F438
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\ServiceAwareDefinition;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
Expand Down Expand Up @@ -447,6 +448,16 @@ protected function initializeBundles()
}
}

/**
* Gets the kernel service class.
*
* @return string The service class
*/
protected function getServiceClass()
{
return __NAMESPACE__.'\\Service\\Kernel';
}

/**
* Gets the container class.
*
Expand Down Expand Up @@ -481,7 +492,21 @@ protected function initializeContainer()
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
$fresh = true;
if (!$cache->isFresh()) {
$serviceClass = $this->getServiceClass();
$bundles = array();
foreach ($this->bundles as $name => $bundle) {
$bundles[$name] = array(
'service_class' => method_exists($bundle, 'getServiceClass') ? $bundle->getServiceClass() : __NAMESPACE__.'\\Service\Bundle',
'class' => get_class($bundle),
'namespace' => $bundle->getNamespace(),
'parent' => $bundle->getParent(),
'path' => $bundle->getPath(),
);
}
$serviceDefinition = new ServiceAwareDefinition($serviceClass, array($this->environment, $this->debug, $bundles));
$serviceDefinition->setService(new $serviceClass($this->environment, $this->debug, $bundles));
$container = $this->buildContainer();
$container->setDefinition('kernel_as_a_service', $serviceDefinition);
Copy link
Contributor
@ogizanagi ogizanagi Aug 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit: Btw.. it becomes real quirky in MergeExtensionConfigurationPass (see my first attempt) where synthetic services are vanished anyway during extension loading ;-)
#19606 (comment)

If you want to use this service in an extension, you'll still have to do quirky things in MergeExtensionConfigurationPass as in your first attempt to make it available in the $tmpContainer passed to the ExtensionInterface::load method, right ?
Or perhaps you're only targeting compiler passes now ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it ;-)... however there are some design choices to be made.

$container->compile();
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

Expand Down
68 changes: 68 additions & 0 deletions src/Symfony/Component/HttpKernel/Service/Bundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Service;

/**
* The bundle service represents a bundle as a service throughout the ecosystem.
*
* @author Roland Franssen <franssen.roland@gmail.com>
*/
class Bundle
{
private $name;
private $namespace;
private $className;
private $path;
private $parent;

/**
* Constructor.
*
* @param string $name
* @param string $namespace
* @param string $className
* @param string $path
* @param Bundle|null $parent
*/
public function __construct($name, $namespace, $className, $path, Bundle $parent = null)
{
$this->name = $name;
$this->className = $className;
$this->path = $path;
$this->parent = $parent;
}

final public function getName()
{
return $this->name;
}

final public function getNamespace()
{
return $this->namespace;
}

final public function getClassName()
{
return $this->className;
}

final public function getPath()
{
return $this->path;
}

final public function getParent()
{
return $this->parent;
}
}
69 changes: 69 additions & 0 deletions src/Symfony/Component/HttpKernel/Service/Kernel.php
Original file line number Diff line number Diff line change
@@ -0 CDAF ,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Service;

/**
* The kernel service represents a kernel as a service throughout the ecosystem.
*
* @author Roland Franssen <franssen.roland@gmail.com>
*/
class Kernel
{
private $environment;
private $debug;
private $bundles;

/**
* Constructor.
*
* @param string $environment
* @param bool $debug
* @param array $bundles
*/
public function __construct($environment, $debug, array $bundles = array())
{
$this->environment = $environment;
$this->debug = $debug;
$this->bundles = array();
$numBundles = count($bundles);
$numProcessedBundles = 0;
do {
foreach ($bundles as $name => $bundle) {
$parent = $bundle['parent'];
if (null !== $parent && !isset($this->bundles[$parent])) {
continue;
}
if (!isset($this->bundles[$name])) {
$serviceClass = $bundle['service_class'];
$parentBundle = isset($this->bundles[$parent]) ? $this->bundles[$parent] : null;
$this->bundles[$name] = new $serviceClass($name, $bundle['namespace'], $bundle['class'], $bundle['path'], $parentBundle);
++$numProcessedBundles;
}
}
} while ($numProcessedBundles < $numBundles);
}

final public function getEnvironment()
{
return $this->environment;
}

final public function isDebug()
{
return $this->debug;
}

final public function getBundles()
{
return $this->bundles;
}
}
0