Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08ec911

Browse files
committed
merged branch Tobion/deprecate-debuggerinterface (PR #8929)
This PR was merged into the master branch. Discussion ---------- [Templating] deprecate DebuggerInterface | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes: deprecated DebuggerInterface and Debugger | Tests pass? | yes | Fixed tickets | #8926 | License | MIT - [x] add suggested optional dependency in templating component to Psr log - [x] remove ProjectTemplateDebugger from tests Commits ------- b853438 [Templating] deprecate DebuggerInterface
2 parents ccf2094 + b853438 commit 08ec911

File tree

13 files changed

+88
-62
lines changed

13 files changed

+88
-62
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1516
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -372,6 +373,15 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
372373
if ($container->getParameter('kernel.debug')) {
373374
$loader->load('templating_debug.xml');
374375

376+
$logger = new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);
377+
378+
$container->getDefinition('templating.loader.cache')
379+
->addTag('monolog.logger', array('channel' => 'templating'))
380+
->addMethodCall('setLogger', array($logger));
381+
$container->getDefinition('templating.loader.chain')
382+
->addTag('monolog.logger', array('channel' => 'templating'))
383+
->addMethodCall('setLogger', array($logger));
384+
375385
$container->setDefinition('templating.engine.php', $container->findDefinition('debug.templating.engine.php'));
376386
$container->setAlias('debug.templating.engine.php', 'templating.engine.php');
377387
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@
5252
<service id="templating.loader.cache" class="%templating.loader.cache.class%" public="false">
5353
<argument type="service" id="templating.loader.wrapped" />
5454
<argument>%templating.loader.cache.path%</argument>
55-
<call method="setDebugger"><argument type="service" id="templating.debugger" on-invalid="ignore" /></call>
5655
</service>
5756

5857
<service id="templating.loader.chain" class="%templating.loader.chain.class%" public="false">
59-
<call method="setDebugger"><argument type="service" id="templating.debugger" on-invalid="ignore" /></call>
6058
</service>
6159

6260
<service id="templating.loader" alias="templating.loader.filesystem" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_debug.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="templating.debugger.class">Symfony\Bundle\FrameworkBundle\Templating\Debugger</parameter>
98
<parameter key="debug.templating.engine.php.class">Symfony\Bundle\FrameworkBundle\Templating\TimedPhpEngine</parameter>
109
</parameters>
1110

1211
<services>
13-
<service id="templating.debugger" class="%templating.debugger.class%" public="false">
14-
<tag name="monolog.logger" channel="templating" />
15-
<argument type="service" id="logger" on-invalid="null" />
16-
</service>
1712
<service id="debug.templating.engine.php" class="%debug.templating.engine.php.class%" public="false">
1813
<argument type="service" id="templating.name_parser" />
1914
<argument type="service" id="service_container" />

src/Symfony/Bundle/FrameworkBundle/Templating/Debugger.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* Binds the Symfony templating loader debugger to the Symfony logger.
1919
*
2020
* @author Fabien Potencier <[email protected]>
21+
*
22+
* @deprecated Deprecated in 2.4, to be removed in 3.0. Use Psr\Log\LoggerInterface instead.
2123
*/
2224
class Debugger implements DebuggerInterface
2325
{

src/Symfony/Component/Templating/DebuggerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* to debug template loader instances.
1717
*
1818
* @author Fabien Potencier <[email protected]>
19+
*
20+
* @deprecated Deprecated in 2.4, to be removed in 3.0. Use Psr\Log\LoggerInterface instead.
1921
*/
2022
interface DebuggerInterface
2123
{

src/Symfony/Component/Templating/Loader/CacheLoader.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public function load(TemplateReferenceInterface $template)
5656
$path = $dir.DIRECTORY_SEPARATOR.$file;
5757

5858
if (is_file($path)) {
59-
if (null !== $this->debugger) {
59+
if (null !== $this->logger) {
60+
$this->logger->debug(sprintf('Fetching template "%s" from cache', $template->get('name')));
61+
} elseif (null !== $this->debugger) {
62+
// just for BC, to be removed in 3.0
6063
$this->debugger->log(sprintf('Fetching template "%s" from cache', $template->get('name')));
6164
}
6265

@@ -75,7 +78,10 @@ public function load(TemplateReferenceInterface $template)
7578

7679
file_put_contents($path, $content);
7780

78-
if (null !== $this->debugger) {
81+
if (null !== $this->logger) {
82+
$this->logger->debug(sprintf('Storing template "%s" in cache', $template->get('name')));
83+
} elseif (null !== $this->debugger) {
84+
// just for BC, to be removed in 3.0
7985
$this->debugger->log(sprintf('Storing template "%s" in cache', $template->get('name')));
8086
}
8187

src/Symfony/Component/Templating/Loader/FilesystemLoader.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,25 @@ public function load(TemplateReferenceInterface $template)
6363
$logs = array();
6464
foreach ($this->templatePathPatterns as $templatePathPattern) {
6565
if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) {
66-
if (null !== $this->debugger) {
66+
if (null !== $this->logger) {
67+
$this->logger->debug(sprintf('Loaded template file "%s"', $file));
68+
} elseif (null !== $this->debugger) {
69+
// just for BC, to be removed in 3.0
6770
$this->debugger->log(sprintf('Loaded template file "%s"', $file));
6871
}
6972

7073
return new FileStorage($file);
7174
}
7275

73-
if (null !== $this->debugger) {
76+
if (null !== $this->logger || null !== $this->debugger) {
7477
$logs[] = sprintf('Failed loading template file "%s"', $file);
7578
}
7679
}
7780

78-
if (null !== $this->debugger) {
79-
foreach ($logs as $log) {
81+
foreach ($logs as $log) {
82+
if (null !== $this->logger) {
83+
$this->logger->debug($log);
84+
} elseif (null !== $this->debugger) {
8085
$this->debugger->log($log);
8186
}
8287
}

src/Symfony/Component/Templating/Loader/Loader.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Templating\Loader;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Templating\DebuggerInterface;
1516

1617
/**
@@ -20,12 +21,32 @@
2021
*/
2122
abstract class Loader implements LoaderInterface
2223
{
24+
/**
25+
* @var LoggerInterface|null
26+
*/
27+
protected $logger;
28+
29+
/**
30+
* @deprecated Deprecated in 2.4, to be removed in 3.0. Use $this->logger instead.
31+
*/
2332
protected $debugger;
2433

34+
/**
35+
* Sets the debug logger to use for this loader.
36+
*
37+
* @param LoggerInterface $logger A logger instance
38+
*/
39+
public function setLogger(LoggerInterface $logger)
40+
{
41+
$this->logger = $logger;
42+
}
43+
2544
/**
2645
* Sets the debugger to use for this loader.
2746
*
2847
* @param DebuggerInterface $debugger A debugger instance
48+
*
49+
* @deprecated Deprecated in 2.4, to be removed in 3.0. Use $this->setLogger() instead.
2950
*/
3051
public function setDebugger(DebuggerInterface $debugger)
3152
{

src/Symfony/Component/Templating/Tests/Fixtures/ProjectTemplateDebugger.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Symfony/Component/Templating/Tests/Loader/CacheLoaderTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ public function testLoad()
3131
{
3232
$dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
3333
mkdir($dir, 0777, true);
34+
3435
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), $dir);
35-
$loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
3636
$this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
37+
38+
$logger = $this->getMock('Psr\Log\LoggerInterface');
39+
$logger->expects($this->once())->method('debug')->with('Storing template "index" in cache');
40+
$loader->setLogger($logger);
3741
$loader->load(new TemplateReference('index'));
38-
$this->assertTrue($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
42+
43+
$logger = $this->getMock('Psr\Log\LoggerInterface');
44+
$logger->expects($this->once())->method('debug')->with('Fetching template "index" from cache');
45+
$loader->setLogger($logger);
3946
$loader->load(new TemplateReference('index'));
40-
$this->assertTrue($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
4147
}
4248
}
4349

src/Symfony/Component/Templating/Tests/Loader/FilesystemLoaderTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ public function testLoad()
5959
$this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass a relative template that exists');
6060
$this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the absolute path of the template');
6161

62+
$logger = $this->getMock('Psr\Log\LoggerInterface');
63+
$logger->expects($this->exactly(2))->method('debug');
64+
6265
$loader = new ProjectTemplateLoader2($pathPattern);
63-
$loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
66+
$loader->setLogger($logger);
6467
$this->assertFalse($loader->load(new TemplateReference('foo.xml', 'php')), '->load() returns false if the template does not exist for the given engine');
65-
$this->assertTrue($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found');
6668

6769
$loader = new ProjectTemplateLoader2(array(self::$fixturesPath.'/null/%name%', $pathPattern));
68-
$loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
70+
$loader->setLogger($logger);
6971
$loader->load(new TemplateReference('foo.php', 'php'));
70-
$this->assertTrue($debugger->hasMessage('Loaded template file'), '->load() logs a "Loaded template file" message if the template is found');
7172
}
7273
}
7374

src/Symfony/Component/Templating/Tests/Loader/LoaderTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717

1818
class LoaderTest extends \PHPUnit_Framework_TestCase
1919
{
20+
public function testGetSetLogger()
21+
{
22+
$loader = new ProjectTemplateLoader4(new TemplateNameParser());
23+
$logger = $this->getMock('Psr\Log\LoggerInterface');
24+
$loader->setLogger($logger);
25+
$this->assertSame($logger, $loader->getLogger(), '->setLogger() sets the logger instance');
26+
}
27+
2028
public function testGetSetDebugger()
2129
{
2230
$loader = new ProjectTemplateLoader4(new TemplateNameParser());
23-
$loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
24-
$this->assertTrue($loader->getDebugger() === $debugger, '->setDebugger() sets the debugger instance');
31+
$debugger = $this->getMock('Symfony\Component\Templating\DebuggerInterface');
32+
$loader->setDebugger($debugger);
33+
$this->assertSame($debugger, $loader->getDebugger(), '->setDebugger() sets the debugger instance');
2534
}
2635
}
2736

@@ -31,6 +40,11 @@ public function load(TemplateReferenceInterface $template)
3140
{
3241
}
3342

43+
public function getLogger()
44+
{
45+
return $this->logger;
46+
}
47+
3448
public function getDebugger()
3549
{
3650
return $this->debugger;

src/Symfony/Component/Templating/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"require": {
1919
"php": ">=5.3.3"
2020
},
21+
"require-dev": {
22+
"psr/log": "~1.0"
23+
},
24+
"suggest": {
25+
"psr/log": "For using debug logging in loaders"
26+
},
2127
"autoload": {
2228
"psr-0": { "Symfony\\Component\\Templating\\": "" }
2329
},

0 commit comments

Comments
 (0)