egulias/tag-debug
Fetch and inspect tagged services from a Symfony DependencyInjection ContainerBuilder. TagFetcher returns tags grouped by tag name with service class, Tag metadata, and Definition. Supports composable AND filters (e.g., by tag name) or custom filters.
To start using egulias/tag-debug in Laravel, install it via Composer:
composer require egulias/tags-debug
Leverage the package to debug tagged services in Laravel's container (which is built on Symfony's DIC). Add this to a service provider's boot() method or a custom Artisan command:
use Egulias\TagDebug\Tag\TagFetcher;
use Egulias\TagDebug\Tag\FilterList;
use Illuminate\Support\ServiceProvider;
class DebugServiceProvider extends ServiceProvider
{
public function boot()
{
$container = $this->app->getContainer();
$fetcher = new TagFetcher($container);
$filters = new FilterList();
$tags = $fetcher->fetch($filters);
// Dump all tagged services (e.g., in Tinker or a debug route)
dd($tags);
}
}
Create a custom Artisan command for ad-hoc debugging:
php artisan make:command DebugTags
Then implement:
use Egulias\TagDebug\Tag\TagFetcher;
use Egulias\TagDebug\Tag\FilterList;
class DebugTagsCommand extends Command
{
protected $signature = 'debug:tags {--tag= : Filter by tag name}';
protected $description = 'Debug tagged services in the container';
public function handle()
{
$container = app();
$fetcher = new TagFetcher($container);
$filters = new FilterList();
if ($tag = $this->option('tag')) {
$filters->addFilter(new \Egulias\TagDebug\Tag\Filter\Tag($tag));
}
$tags = $fetcher->fetch($filters);
$this->line(print_r($tags, true));
}
}
Run it with:
php artisan debug:tags --tag=mailer
Use the package to validate or inspect tagged services during bootstrapping:
public function register()
{
$this->app->tag(
[\App\Services\Mailer::class, \App\Services\Logger::class],
'app.services'
);
}
public function boot()
{
$container = $this->app->getContainer();
$fetcher = new TagFetcher($container);
$filters = new FilterList();
$filters->addFilter(new \Egulias\TagDebug\Tag\Filter\Tag('app.services'));
$tags = $fetcher->fetch($filters);
// Log or validate tagged services
foreach ($tags['app.services'] as $service => $data) {
$this->log("Tagged service: {$service}");
}
}
Combine filters to narrow down results dynamically:
$filters = new FilterList();
$filters->addFilter(new \Egulias\TagDebug\Tag\Filter\Tag('mailer'));
$filters->addFilter(new \Egulias\TagDebug\Tag\Filter\Name('*MailerService')); // Wildcard support
$tags = $fetcher->fetch($filters);
Implement a custom filter for Laravel-specific logic (e.g., filter by binding type):
use Egulias\TagDebug\Tag\Filter;
class BindingTypeFilter implements Filter
{
private $bindingType;
public function __construct($bindingType)
{
$this->bindingType = $bindingType;
}
public function matches($tag)
{
$definition = $tag->getDefinition();
return $definition->getClass() === $this->bindingType;
}
}
// Usage:
$filters->addFilter(new BindingTypeFilter(\App\Services\Mailer::class));
Attach the debugger to a route or middleware for runtime inspection:
Route::get('/debug/tags', function () {
$container = app();
$fetcher = new TagFetcher($container);
$filters = new FilterList();
$tags = $fetcher->fetch($filters);
return response()->json($tags);
})->middleware('auth');
$this->app->getContainer() or app() to access the underlying ContainerBuilder.app()->tag() or $this->app->tag() in providers. Ensure tags are registered before debugging.Outdated Data:
Circular References:
Filter Logic:
AND logic by default. Misconfigured filters may return empty results.Tag) before combining.Custom Output:
TagFetcher or processing the $tags array:
$formatted = [];
foreach ($tags as $tagName => $services) {
$formatted[$tagName] = array_keys($services);
}
Integration with Laravel Debugbar:
use Barryvdh\Debugbar\DataCollector\DataCollector;
class TagDebugCollector extends DataCollector
{
public function collect()
{
$fetcher = new TagFetcher(app());
$this->data['tags'] = $fetcher->fetch(new FilterList());
}
}
Tag Validation:
$requiredTags = ['mailer', 'logger'];
foreach ($requiredTags as $tag) {
$filters = new FilterList();
$filters->addFilter(new \Egulias\TagDebug\Tag\Filter\Tag($tag));
if (empty($fetcher->fetch($filters)[$tag])) {
throw new \RuntimeException("Missing required tag: {$tag}");
}
}
composer.json:
"autoload": {
"psr-4": {
"Egulias\\TagDebug\\": "vendor/egulias/tags-debug/src/"
}
}
laravel-debugbar or custom solutions.How can I help you explore Laravel packages today?