-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Add object:extensions command to list Extensions of an Object
- Loading branch information
1 parent
79c365d
commit 08179b8
Showing
3 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters