Skip to content

[DX][FrameworkBundle] Show private aliases in debug:container #22385

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
merged 1 commit into from
May 3, 2017
Merged
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 @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -176,19 +177,17 @@ protected function getContainerBuilder()
return $this->containerBuilder;
}

if (!$this->getApplication()->getKernel()->isDebug()) {
throw new \LogicException('Debug information about the container is only available in debug mode.');
}
$kernel = $this->getApplication()->getKernel();

if (!is_file($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) {
throw new \LogicException('Debug information about the container could not be found. Please clear the cache and try again.');
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, get_class($kernel));
$container = $buildContainer();
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
} else {
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
}

$container = new ContainerBuilder();

$loader = new XmlFileLoader($container, new FileLocator());
$loader->load($cachedFile);

return $this->containerBuilder = $container;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$service = $this->resolveServiceDefinition($builder, $serviceId);

if ($service instanceof Alias) {
$data['aliases'][$serviceId] = $this->getContainerAliasData($service);
if ($showPrivate || $service->isPublic()) {
$data['aliases'][$serviceId] = $this->getContainerAliasData($service);
}
} elseif ($service instanceof Definition) {
if (($showPrivate || $service->isPublic())) {
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$this->write($title."\n".str_repeat('=', strlen($title)));

$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$showPrivate = isset($options['show_private']) && $options['show_private'];
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$services = array('definitions' => array(), 'aliases' => array(), 'services' => array());

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

if ($service instanceof Alias) {
$services['aliases'][$serviceId] = $service;
if ($showPrivate || $service->isPublic()) {
$services['aliases'][$serviceId] = $service;
}
} elseif ($service instanceof Definition) {
if (($showPrivate || $service->isPublic())) {
$services['definitions'][$serviceId] = $service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
}
}
}
} elseif ($definition instanceof Alias) {
if (!$showPrivate && !$definition->isPublic()) {
unset($serviceIds[$key]);
continue;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private function getContainerServicesDocument(ContainerBuilder $builder, $tag =
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

if ($service instanceof Definition && !($showPrivate || $service->isPublic())) {
if (($service instanceof Definition || $service instanceof Alias) && !($showPrivate || $service->isPublic())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;

/**
* Dumps the ContainerBuilder to a cache file so that it can be used by
Expand All @@ -28,14 +27,9 @@ class ContainerBuilderDebugDumpPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$dumper = new XmlDumper($container);
$filename = $container->getParameter('debug.container.dump');
$filesystem = new Filesystem();
$filesystem->dumpFile($filename, $dumper->dump(), null);
try {
$filesystem->chmod($filename, 0666, umask());
} catch (IOException $e) {
// discard chmod failure (some filesystem may not support it)
$cache = new ConfigCache($container->getParameter('debug.container.dump'), true);
if (!$cache->isFresh()) {
$cache->write((new XmlDumper($container))->dump(), $container->getResources());
}
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function build(ContainerBuilder $container)
if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
$this->addCompilerPassIfExists($container, ConfigCachePass::class);
$container->addCompilerPass(new CacheCollectorPass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@
"alias_1": {
"service": "service_1",
"public": true
},
"alias_2": {
"service": "service_2",
"public": false
}
},
"services": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ Aliases
- Service: `service_1`
- Public: yes

### alias_2

- Service: `service_2`
- Public: no


Services
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
 Service ID   Class name 
------------------- --------------------------------------------------------
alias_1 alias for "service_1"
alias_2 alias for "service_2"
definition_1 Full\Qualified\Class1
service_container Symfony\Component\DependencyInjection\ContainerBuilder
------------------- --------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" public="true" synthetic="false" lazy="true" shared="true" abstract="true" autowired="false" autoconfigured="false" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
<argument type="service" id="definition2"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
"alias_1": {
"service": "service_1",
"public": true
},
"alias_2": {
"service": "service_2",
"public": false
}
},
"services": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ Aliases
- Service: `service_1`
- Public: yes

### alias_2

- Service: `service_2`
- Public: no


Services
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
 Service ID   Class name 
------------------- --------------------------------------------------------
alias_1 alias for "service_1"
alias_2 alias for "service_2"
definition_1 Full\Qualified\Class1
service_container Symfony\Component\DependencyInjection\ContainerBuilder
------------------- --------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" public="true" synthetic="false" lazy="true" shared="true" abstract="true" autowired="false" autoconfigured="false" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\ApplicationTester;

/**
* @group functional
*/
class ContainerDebugCommandTest extends WebTestCase
{
public function testDumpContainerIfNotExists()
{
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));

$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$kernel->getContainer()->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(array('command' => 'debug:container'));

$this->assertFileExists(static::$kernel->getContainer()->getParameter('debug.container.dump'));
}

public function testNoDebug()
{
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => false));

$application = new Application(static::$kernel);
$application->setAutoExit(false);

$tester = new ApplicationTester($application);
$tester->run(array('command' => 'debug:container'));

$this->assertContains('public', $tester->getDisplay());
}

public function testPrivateAlias()
{
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));

$application = new Application(static::$kernel);
$application->setAutoExit(false);

$tester = new ApplicationTester($application);
$tester->run(array('command' => 'debug:container', '--show-private' => true));
$this->assertContains('public', $tester->getDisplay());
$this->assertContains('private_alias', $tester->getDisplay());

$tester->run(array('command' => 'debug:container'));
$this->assertContains('public', $tester->getDisplay());
$this->assertNotContains('private_alias', $tester->getDisplay());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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.
*/

use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return array(
new FrameworkBundle(),
new TestBundle(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: ../config/default.yml }

services:
public:
class: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\DeclaredClass
private_alias:
alias: public
public: false