|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command\Object; |
|
4
|
|
|
|
|
5
|
|
|
use SilverLeague\Console\Command\SilverStripeCommand; |
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* List all extensions of a given Object, e.g. "Page" |
|
14
|
|
|
* |
|
15
|
|
|
* @package silverstripe-console |
|
16
|
|
|
* @author Robbie Averill <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ExtensionsCommand extends SilverStripeCommand |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritDoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function configure() |
|
24
|
|
|
{ |
|
25
|
|
|
$this |
|
26
|
|
|
->setName('object:extensions') |
|
27
|
|
|
->setDescription('List all Extensions of a given Object, e.g. "Page"') |
|
28
|
|
|
->addArgument('object', InputArgument::REQUIRED, 'The Object to look up'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
35
|
|
|
{ |
|
36
|
|
|
$object = $input->getArgument('object'); |
|
37
|
|
|
$extensions = \SilverStripe\Core\Object::get_extensions($object); |
|
38
|
|
|
if (!$extensions) { |
|
39
|
|
|
$output->writeln('There are no Extensions registered for ' . $object); |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
sort($extensions); |
|
43
|
|
|
|
|
44
|
|
|
$isCmsClass = singleton($object) instanceof \SilverStripe\CMS\Model\SiteTree; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$output->writeln('<info>Extensions for ' . $object . ':</info>'); |
|
47
|
|
|
$table = new Table($output); |
|
48
|
|
|
$table |
|
49
|
|
|
->setHeaders($this->getHeaders($isCmsClass)) |
|
50
|
|
|
->setRows($this->getRows($isCmsClass, $extensions)) |
|
51
|
|
|
->render(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return the header cells for the output table. CMS classes have an extra column. |
|
56
|
|
|
* |
|
57
|
|
|
* @param bool $isCmsClass |
|
58
|
|
|
* @return string[] |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getHeaders($isCmsClass) |
|
61
|
|
|
{ |
|
62
|
|
|
$headers = ['Class name', 'Added DB fields']; |
|
63
|
|
|
if ($isCmsClass) { |
|
64
|
|
|
$headers[] = 'Updates CMS fields'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $headers; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return the rows for the output table containing extension statistics. CMS classes have an extra column. |
|
72
|
|
|
* |
|
73
|
|
|
* @param bool $isCmsClass |
|
74
|
|
|
* @return array[] |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getRows($isCmsClass, $extensions) |
|
77
|
|
|
{ |
|
78
|
|
|
$tableRows = []; |
|
79
|
|
|
foreach ($extensions as $extensionClass) { |
|
80
|
|
|
$row = [ |
|
81
|
|
|
$extensionClass, |
|
82
|
|
|
// Add the number of DB fields that the class adds |
|
83
|
|
|
count((array) Config::inst()->get($extensionClass, 'db', Config::UNINHERITED)) |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
if ($isCmsClass) { |
|
87
|
|
|
// Add whether or not the extension updates CMS fields |
|
88
|
|
|
$row[] = method_exists(singleton($extensionClass), 'updateCMSFields') ? 'Yes' : 'No'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$tableRows[] = $row; |
|
92
|
|
|
} |
|
93
|
|
|
return $tableRows; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.