Skip to content

Commit

Permalink
API Add object:children command to list concrete extensions of a class
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Jan 3, 2017
1 parent 6e7d180 commit 915229f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Commands:
- SilverLeague\Console\Command\Member\ChangeGroupsCommand
- SilverLeague\Console\Command\Member\ChangePasswordCommand
- SilverLeague\Console\Command\Member\CreateCommand
- SilverLeague\Console\Command\Object\ChildrenCommand
- SilverLeague\Console\Command\Object\ExtensionsCommand
- SilverLeague\Console\Command\Object\LookupCommand
56 changes: 56 additions & 0 deletions src/Command/Object/ChildrenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace SilverLeague\Console\Command\Object;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\Core\ClassInfo;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* List all child classes of a given class name
*
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class ChildrenCommand extends SilverStripeCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('object:children')
->setDescription('List all child classes of a given class, e.g. "Page"')
->addArgument('object', InputArgument::REQUIRED, 'The class to find children for');
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$object = $input->getArgument('object');
$classes = (array) ClassInfo::subclassesFor($object);
// Remove the class itself
array_shift($classes);
if (!$classes) {
$output->writeln('There are no child classes for ' . $object);
return;
}
sort($classes);
$rows = array_map(function ($class) {
return [$class];
}, $classes);

$output->writeln('<info>Child classes for ' . $object . ':</info>');
$table = new Table($output);
$table
->setHeaders(['Class name'])
->setRows($rows)
->render();
}
}
8 changes: 5 additions & 3 deletions src/Command/Object/ExtensionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Object;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,7 +26,7 @@ protected function configure()
$this
->setName('object:extensions')
->setDescription('List all Extensions of a given Object, e.g. "Page"')
->addArgument('object', InputArgument::REQUIRED, 'The Object to look up');
->addArgument('object', InputArgument::REQUIRED, 'The Object to find Extensions for');
}

/**
Expand All @@ -34,14 +35,15 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$object = $input->getArgument('object');
$extensions = \SilverStripe\Core\Object::get_extensions($object);
$extensions = Object::get_extensions($object);
if (!$extensions) {
$output->writeln('There are no Extensions registered for ' . $object);
return;
}
sort($extensions);

$isCmsClass = singleton($object) instanceof \SilverStripe\CMS\Model\SiteTree;
$isCmsClass = (class_exists('SilverStripe\\CMS\\Model\\SiteTree')
&& singleton($object) instanceof \SilverStripe\CMS\Model\SiteTree);

$output->writeln('<info>Extensions for ' . $object . ':</info>');
$table = new Table($output);
Expand Down

0 comments on commit 915229f

Please sign in to comment.