Skip to content

Commit

Permalink
API Add object:extensions command to list Extensions of an Object
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Jan 3, 2017
1 parent 79c365d commit 08179b8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
3 changes: 2 additions & 1 deletion console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#

Commands:
- SilverLeague\Console\Command\Object\LookupCommand
- SilverLeague\Console\Command\Dev\BuildCommand
- SilverLeague\Console\Command\Member\ChangeGroupsCommand
- SilverLeague\Console\Command\Member\ChangePasswordCommand
- SilverLeague\Console\Command\Member\CreateCommand
- SilverLeague\Console\Command\Object\ExtensionsCommand
- SilverLeague\Console\Command\Object\LookupCommand
95 changes: 95 additions & 0 deletions src/Command/Object/ExtensionsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace SilverLeague\Console\Command\Object;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\Core\Config\Config;
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 extensions of a given Object, e.g. "Page"
*
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class ExtensionsCommand extends SilverStripeCommand
{
/**
* {@inheritDoc}
*/
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');
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$object = $input->getArgument('object');
$extensions = \SilverStripe\Core\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;

$output->writeln('<info>Extensions for ' . $object . ':</info>');
$table = new Table($output);
$table
->setHeaders($this->getHeaders($isCmsClass))
->setRows($this->getRows($isCmsClass, $extensions))
->render();
}

/**
* Return the header cells for the output table. CMS classes have an extra column.
*
* @param bool $isCmsClass
* @return string[]
*/
protected function getHeaders($isCmsClass)
{
$headers = ['Class name', 'Added DB fields'];
if ($isCmsClass) {
$headers[] = 'Updates CMS fields';
}

return $headers;
}

/**
* Return the rows for the output table containing extension statistics. CMS classes have an extra column.
*
* @param bool $isCmsClass
* @return array[]
*/
protected function getRows($isCmsClass, $extensions)
{
$tableRows = [];
foreach ($extensions as $extensionClass) {
$row = [
$extensionClass,
// Add the number of DB fields that the class adds
count((array) Config::inst()->get($extensionClass, 'db', Config::UNINHERITED))
];

if ($isCmsClass) {
// Add whether or not the extension updates CMS fields
$row[] = method_exists(singleton($extensionClass), 'updateCMSFields') ? 'Yes' : 'No';
}

$tableRows[] = $row;
}
return $tableRows;
}
}
12 changes: 6 additions & 6 deletions src/Command/Object/LookupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Shows which class/objet is returned from the Injector
* Shows which Object is returned from the Injector
*
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
Expand All @@ -23,18 +23,18 @@ protected function configure()
{
$this
->setName('object:lookup')
->setDescription('Shows which class is returned from the Injector')
->addArgument('class', InputArgument::REQUIRED, 'The class to look up');
->setDescription('Shows which Object is returned from the Injector')
->addArgument('object', InputArgument::REQUIRED, 'The Object to look up');
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$class = $input->getArgument('class');
$resolvedTo = get_class(Injector::inst()->get($class));
$object = $input->getArgument('object');
$resolvedTo = get_class(Injector::inst()->get($object));

$output->writeln('<comment>' . $class . ' resolves to <info>' . $resolvedTo . '</info></comment>');
$output->writeln('<comment>' . $object . '</comment> resolves to <info>' . $resolvedTo . '</info>');
}
}

0 comments on commit 08179b8

Please sign in to comment.