diff --git a/console.yml b/console.yml index 1d4a678..2c8fcbb 100644 --- a/console.yml +++ b/console.yml @@ -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 diff --git a/src/Command/Object/ExtensionsCommand.php b/src/Command/Object/ExtensionsCommand.php new file mode 100644 index 0000000..3537c78 --- /dev/null +++ b/src/Command/Object/ExtensionsCommand.php @@ -0,0 +1,95 @@ + + */ +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('Extensions for ' . $object . ':'); + $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; + } +} diff --git a/src/Command/Object/LookupCommand.php b/src/Command/Object/LookupCommand.php index d4e93d1..2da4e59 100644 --- a/src/Command/Object/LookupCommand.php +++ b/src/Command/Object/LookupCommand.php @@ -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 @@ -23,8 +23,8 @@ 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'); } /** @@ -32,9 +32,9 @@ protected function configure() */ 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('' . $class . ' resolves to ' . $resolvedTo . ''); + $output->writeln('' . $object . ' resolves to ' . $resolvedTo . ''); } }