|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codemonster\Annabel\Console\Commands; |
| 4 | + |
| 5 | +use Codemonster\Annabel\Console\Command; |
| 6 | + |
| 7 | +class ContainerListCommand extends Command |
| 8 | +{ |
| 9 | + public function getName(): string |
| 10 | + { |
| 11 | + return 'container:list'; |
| 12 | + } |
| 13 | + |
| 14 | + public function getDescription(): string |
| 15 | + { |
| 16 | + return 'Show container bindings and instantiated singletons.'; |
| 17 | + } |
| 18 | + |
| 19 | + public function getUsage(): string |
| 20 | + { |
| 21 | + return 'container:list'; |
| 22 | + } |
| 23 | + |
| 24 | + public function handle(array $arguments = []): int |
| 25 | + { |
| 26 | + $console = $this->console(); |
| 27 | + $app = $console->getApplication(); |
| 28 | + $container = $app->getContainer(); |
| 29 | + |
| 30 | + $bindings = $container->getBindings(); |
| 31 | + $instances = $container->getInstances(); |
| 32 | + |
| 33 | + $console->writeln($console->color('Bindings:', 'label')); |
| 34 | + |
| 35 | + if (empty($bindings)) { |
| 36 | + $console->writeln(' ' . $console->color('none', 'muted')); |
| 37 | + } else { |
| 38 | + foreach ($bindings as $abstract => $binding) { |
| 39 | + $concrete = is_string($binding['concrete']) ? $binding['concrete'] : 'Closure'; |
| 40 | + $console->writeln(sprintf( |
| 41 | + ' %s => %s%s', |
| 42 | + $console->color($abstract, 'command'), |
| 43 | + $concrete, |
| 44 | + $binding['singleton'] ? ' (singleton)' : '' |
| 45 | + )); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + $console->writeln(''); |
| 50 | + $console->writeln($console->color('Instances:', 'label')); |
| 51 | + |
| 52 | + if (empty($instances)) { |
| 53 | + $console->writeln(' ' . $console->color('none', 'muted')); |
| 54 | + } else { |
| 55 | + foreach ($instances as $abstract => $instance) { |
| 56 | + $console->writeln(sprintf( |
| 57 | + ' %s => %s', |
| 58 | + $console->color($abstract, 'command'), |
| 59 | + get_class($instance) |
| 60 | + )); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return 0; |
| 65 | + } |
| 66 | +} |
0 commit comments