1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PPI Framework. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2011-2016 Paul Dragoonis <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* |
8
|
|
|
* @link http://www.ppi.io |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PPI\Framework\Console\Command; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A console command for retrieving information about services. |
19
|
|
|
* |
20
|
|
|
* @author Vítor Brandão <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ServiceManagerDebugCommand extends AbstractCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
|
|
->setName('service-manager:debug') |
31
|
|
|
->setDescription('Displays current services for an application') |
32
|
|
|
->addOption( |
33
|
|
|
'invoke', |
34
|
|
|
null, |
35
|
|
|
InputOption::VALUE_NONE, |
36
|
|
|
'If set, invoke each service and display the instance type returned' |
37
|
|
|
) |
38
|
|
|
->setHelp(<<<EOF |
39
|
|
|
The <info>%command.name%</info> command displays all configured services: |
40
|
|
|
|
41
|
|
|
<info>php %command.full_name%</info> |
42
|
|
|
|
43
|
|
|
Use the <info>--invoke</info> option to call each service and display the instance type returned along |
44
|
|
|
with success of the operation: |
45
|
|
|
|
46
|
|
|
<info>php %command.full_name% --invoke</info> |
47
|
|
|
EOF |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @throws \LogicException |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
$verbose = OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity(); |
|
|
|
|
59
|
|
|
$invoke = $input->getOption('invoke'); |
60
|
|
|
|
61
|
|
|
$sm = $this->getServiceManager()->get('ServiceManager'); |
62
|
|
|
$registeredServices = $sm->getRegisteredServicesReal(); |
63
|
|
|
|
64
|
|
|
$lines = array(); |
65
|
|
|
$pad = array( |
66
|
|
|
'id' => 0, |
67
|
|
|
'type' => strlen('Instance '), |
68
|
|
|
'class' => strlen('Class name|type|alias'), |
69
|
|
|
); |
70
|
|
|
$serviceTypeToColumnName = array( |
71
|
|
|
'invokableClasses' => 'Invokable', |
72
|
|
|
'factories' => 'Factory', |
73
|
|
|
'aliases' => 'Alias', |
74
|
|
|
'instances' => 'Instance', |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
foreach ($registeredServices as $type => $services) { |
78
|
|
|
foreach ($services as $key => $service) { |
79
|
|
|
$lines[$key]['type'] = $serviceTypeToColumnName[$type]; |
80
|
|
|
if (strlen($key) > $pad['id']) { |
81
|
|
|
$pad['id'] = strlen($key); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (is_object($service)) { |
85
|
|
|
// As of PHP 5.4 you can rely on Closure being a Closure: php.net/manual/en/class.closure.php |
86
|
|
|
if ($service instanceof \Closure) { |
87
|
|
|
$r = new \ReflectionFunction($service); |
88
|
|
|
if ($ns = $r->getNamespaceName()) { |
89
|
|
|
$filename = basename($r->getFileName(), '.php'); |
90
|
|
|
$lines[$key]['class'] = $ns . '\\' . $filename . '\{closure}'; |
91
|
|
|
} else { |
92
|
|
|
$lines[$key]['class'] = 'Closure in ' . $r->getFileName(); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$r = new \ReflectionObject($service); |
96
|
|
|
$lines[$key]['class'] = $r->getName(); |
97
|
|
|
} |
98
|
|
|
} elseif (is_array($service)) { |
99
|
|
|
$lines[$key]['class'] = 'Array'; |
100
|
|
|
} elseif (is_string($service) && ($type != 'aliases')) { |
101
|
|
|
$r = new \ReflectionClass($service); |
102
|
|
|
$lines[$key]['class'] = $r->getName(); |
103
|
|
|
} else { // Alias |
104
|
|
|
$lines[$key]['class'] = $service; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$len = strlen($lines[$key]['class']); |
108
|
|
|
if ('aliases' == $type) { |
109
|
|
|
$len += 10; // add the "alias for " prefix |
110
|
|
|
} |
111
|
|
|
if ($len > $pad['class']) { |
112
|
|
|
$pad['class'] = $len; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
ksort($lines); |
118
|
|
|
$output->write(sprintf('<comment>%s</comment> <comment>%s</comment> <comment>%s</comment>', |
119
|
|
|
str_pad('Service Id', $pad['id']), |
120
|
|
|
str_pad('Type', $pad['type']), |
121
|
|
|
str_pad('Class Name|Type|Alias', $pad['class']))); |
122
|
|
|
$output->writeln($invoke ? ' <comment>Invokation Status [result]</comment>' : ''); |
123
|
|
|
foreach ($lines as $id => $line) { |
124
|
|
|
$output->write(sprintf('<info>%s</info> ', str_pad($id, $pad['id']))); |
125
|
|
|
$output->write(sprintf('%s ', str_pad($line['type'], $pad['type']))); |
126
|
|
|
if ('Alias' == $line['type']) { |
127
|
|
|
$output->write(sprintf('<comment>alias for</comment> <info>%s </info>', str_pad($line['class'], $pad['class'] - 10))); |
128
|
|
|
} else { |
129
|
|
|
$output->write(sprintf('%s ', str_pad($line['class'], $pad['class']))); |
130
|
|
|
} |
131
|
|
|
if ($invoke) { |
132
|
|
|
try { |
133
|
|
|
$service = $sm->get($id); |
134
|
|
|
$output->write(sprintf(' <info>OK</info> [%s]', is_object($service) ? get_class($service) : gettype($service))); |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
$output->write(' <error>FAIL</error> [' . $e->getMessage() . ']'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
$output->writeln(''); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.