diff --git a/src/Command/UserListCommand.php b/src/Command/UserListCommand.php index 40fc5c7..ac964b9 100644 --- a/src/Command/UserListCommand.php +++ b/src/Command/UserListCommand.php @@ -25,6 +25,10 @@ class UserListCommand extends Command * @var EntityManagerInterface */ private $em; + /** + * @var int + */ + private $colWidth; public function __construct( string $userClassName, @@ -41,12 +45,15 @@ protected function configure() $this ->setDescription(self::$defaultDescription) ->addArgument('page', InputArgument::OPTIONAL, 'Page', 1) - ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit rows on single page', 10); + ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit rows on single page', 10) + ->addOption('col-width', 'w', InputOption::VALUE_REQUIRED, 'Set maximum width for one column', 64); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); + /** @var int $colWidth */ + $this->colWidth = $input->getOption('col-width'); $userClass = new $this->userClassName(); if (!$userClass instanceof UserInterface) { @@ -71,14 +78,19 @@ private function draw(SymfonyStyle $io, int $page, int $limit) ->getRepository($this->userClassName) ->findBy([], [], $limit, $limit * ($page - 1)); - $table = new ObjectToTable( + $objectToTable = new ObjectToTable( $userList, $io, $limit ); - $table = $table->makeTable(); + $table = $objectToTable->makeTable(); $table->setFooterTitle("Page $page / " . ceil($counetr / $limit)); + + for ($i = 0; $i < count($objectToTable->getUserGetters(new $this->userClassName)); $i++) { + $table->setColumnMaxWidth($i, $this->colWidth); + } + $table->render(); $io->writeln('To exit type "q" and pres '); diff --git a/src/Services/ObjectToTable.php b/src/Services/ObjectToTable.php index a7b1705..9580978 100644 --- a/src/Services/ObjectToTable.php +++ b/src/Services/ObjectToTable.php @@ -2,18 +2,10 @@ namespace Fabricio872\RegisterCommand\Services; -use Doctrine\Common\Annotations\Reader; -use Fabricio872\RegisterCommand\Annotations\RegisterCommand; use Fabricio872\RegisterCommand\Serializer\UserEntityNormalizer; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Security\Core\User\UserInterface; -use Symfony\Component\Serializer\Encoder\JsonEncoder; -use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; -use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; -use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; -use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; class ObjectToTable @@ -51,12 +43,9 @@ public function makeTable(): Table }, $this->getUserGetters($this->users[0])) ); $table->setRows( - array_merge( - array_map(function ($user) { - return $this->makeCols($user); - }, $this->users), - $this->emptyRows() - ) + array_map(function ($user) { + return $this->makeCols($user); + }, $this->users) ); $table->setStyle('box'); // $table->; @@ -72,19 +61,7 @@ private function makeCols($user) /** * @return array */ - private function emptyRows(): array - { - $rows = []; - for ($i = count($this->users); $i < $this->maxRows; $i++) { - $rows[] = ['']; - } - return $rows; - } - - /** - * @return array - */ - private function getUserGetters(UserInterface $user): array + public function getUserGetters(UserInterface $user): array { return array_filter(get_class_methods($user), function ($var) { return substr($var, 0, 3) == 'get';