|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command\Object; |
|
4
|
|
|
|
|
5
|
|
|
use SilverLeague\Console\Command\SilverStripeCommand; |
|
6
|
|
|
use SilverLeague\Console\Framework\Utility\ObjectUtilities; |
|
7
|
|
|
use SilverStripe\Core\ClassInfo; |
|
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* List all child classes of a given class name |
|
15
|
|
|
* |
|
16
|
|
|
* @package silverstripe-console |
|
17
|
|
|
* @author Robbie Averill <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ChildrenCommand extends SilverStripeCommand |
|
20
|
|
|
{ |
|
21
|
|
|
use ObjectUtilities; |
|
22
|
|
|
|
|
23
|
3 |
|
/** |
|
24
|
|
|
* {@inheritDoc} |
|
25
|
|
|
*/ |
|
26
|
3 |
|
protected function configure() |
|
27
|
3 |
|
{ |
|
28
|
3 |
|
$this |
|
29
|
3 |
|
->setName('object:children') |
|
30
|
|
|
->setDescription('List all child classes of a given class, e.g. "Page"') |
|
31
|
|
|
->addArgument('object', InputArgument::REQUIRED, 'The class to find children for'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
/** |
|
35
|
|
|
* {@inheritDoc} |
|
36
|
1 |
|
*/ |
|
37
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$object = $input->getArgument('object'); |
|
40
|
1 |
|
$classes = (array) ClassInfo::subclassesFor($object); |
|
41
|
|
|
// Remove the class itself |
|
42
|
|
|
array_shift($classes); |
|
43
|
|
|
if (!$classes) { |
|
|
|
|
|
|
44
|
1 |
|
$output->writeln('There are no child classes for ' . $object); |
|
45
|
1 |
|
return; |
|
46
|
1 |
|
} |
|
47
|
1 |
|
sort($classes); |
|
48
|
|
|
$rows = array_map(function ($class) { |
|
49
|
1 |
|
return [$class, $this->getModuleName($class)]; |
|
50
|
1 |
|
}, $classes); |
|
51
|
|
|
|
|
52
|
1 |
|
$output->writeln('<info>Child classes for ' . $object . ':</info>'); |
|
53
|
1 |
|
$table = new Table($output); |
|
54
|
1 |
|
$table |
|
55
|
1 |
|
->setHeaders(['Class name', 'Module']) |
|
56
|
|
|
->setRows($rows) |
|
57
|
|
|
->render(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.