Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] [DependencyInjection] added logging of unused tags …
…during container compilation
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php
This file contains 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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?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\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Find all service tags which are defined, but not used and yield a warning log message. | ||
* | ||
* @author Florian Pfitzer <pfitzer@wurzel3.de> | ||
*/ | ||
class UnusedTagsPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* whitelisted tags | ||
* | ||
* @var array | ||
*/ | ||
protected $whitelist = array( | ||
"console.command", | ||
"data_collector", | ||
"form.type", | ||
"form.type_extension", | ||
"form.type_guesser", | ||
"kernel.cache_clearer", | ||
"kernel.cache_warmer", | ||
"kernel.event_listener", | ||
"kernel.event_subscriber", | ||
"kernel.fragment_renderer", | ||
"monolog.logger", | ||
"routing.loader", | ||
"security.remember_me_aware", | ||
"security.voter", | ||
"serializer.encoder", | ||
"templating.helper", | ||
"translation.dumper", | ||
"translation.extractor", | ||
"translation.loader", | ||
"twig.extension", | ||
"twig.loader", | ||
"validator.constraint_validator", | ||
"validator.initializer", | ||
); | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$compiler = $container->getCompiler(); | ||
$formatter = $compiler->getLoggingFormatter(); | ||
$tags = $container->findTags(); | ||
|
||
$unusedTags = $container->findUnusedTags(); | ||
foreach ($unusedTags as $tag) { | ||
// skip whitelisted tags | ||
if (in_array($tag, $this->whitelist)) { | ||
continue; | ||
} | ||
// check for typos | ||
$candidates = array(); | ||
foreach ($tags as $definedTag) { | ||
if ($definedTag === $tag) { | ||
continue; | ||
} | ||
if (false !== strpos($definedTag, $tag) || levenshtein($tag, $definedTag) <= strlen($tag) / 3) { | ||
$candidates[] = $definedTag; | ||
} | ||
} | ||
|
||
$services = array_keys($container->findTaggedServiceIds($tag)); | ||
$message = sprintf('Tag "%s" was defined on the service(s) %s, but was never used.', $tag, implode(',', $services)); | ||
if (!empty($candidates)) { | ||
$message .= sprintf(' Did you mean "%s"?', implode('", "', $candidates)); | ||
} | ||
$compiler->addLogMessage($formatter->format($this, $message)); | ||
} | ||
} | ||
} |
This file contains 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 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 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