|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace AppBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 15 | +use Symfony\Component\Console\Helper\Table; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 20 | + |
| 21 | +/** |
| 22 | + * A command console that lists all the existing users. To use this command, open |
| 23 | + * a terminal window, enter into your project directory and execute the following: |
| 24 | + * |
| 25 | + * $ php app/console app:list-users |
| 26 | + * |
| 27 | + * See http://symfony.com/doc/current/cookbook/console/console_command.html |
| 28 | + * |
| 29 | + * @author Javier Eguiluz <[email protected]> |
| 30 | + */ |
| 31 | +class ListUsersCommand extends ContainerAwareCommand |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var ObjectManager |
| 35 | + */ |
| 36 | + private $em; |
| 37 | + |
| 38 | + protected function configure() |
| 39 | + { |
| 40 | + $this |
| 41 | + // a good practice is to use the 'app:' prefix to group all your custom application commands |
| 42 | + ->setName('app:list-users') |
| 43 | + ->setDescription('Lists all the existing users') |
| 44 | + ->setHelp(<<<HELP |
| 45 | +The <info>%command.name%</info> command lists all the users registered in the application: |
| 46 | +
|
| 47 | + <info>php %command.full_name%</info> |
| 48 | +
|
| 49 | +By default the command only displays the 50 most recent users. Set the number of |
| 50 | +results to display with the <comment>--max-results</comment> option: |
| 51 | +
|
| 52 | + <info>php %command.full_name%</info> <comment>--max-results=2000</comment> |
| 53 | +
|
| 54 | +In addition to displaying the user list, you can also send this information to |
| 55 | +the email address specified in the <comment>--send-to</comment> option: |
| 56 | +
|
| 57 | + <info>php %command.full_name%</info> <comment>[email protected]</comment> |
| 58 | +
|
| 59 | +HELP |
| 60 | + ) |
| 61 | + // commands can optionally define arguments and/or options (mandatory and optional) |
| 62 | + // see http://symfony.com/doc/current/components/console/console_arguments.html |
| 63 | + ->addOption('max-results', null, InputOption::VALUE_OPTIONAL, 'Limits the number of users listed', 50) |
| 64 | + ->addOption('send-to', null, InputOption::VALUE_OPTIONAL, 'If set, the result is sent to the given email address') |
| 65 | + ; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * This method is executed before the the execute() method. It's main purpose |
| 70 | + * is to initialize the variables used in the rest of the command methods. |
| 71 | + */ |
| 72 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 73 | + { |
| 74 | + $this->em = $this->getContainer()->get('doctrine')->getManager(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * This method is executed after initialize(). It usually contains the logic |
| 79 | + * to execute to complete this command task. |
| 80 | + */ |
| 81 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 82 | + { |
| 83 | + $maxResults = $input->getOption('max-results'); |
| 84 | + // Use ->findBy() instead of ->findAll() to allow result sorting and limiting |
| 85 | + $users = $this->em->getRepository('AppBundle:User')->findBy(array(), array('id' => 'DESC'), $maxResults); |
| 86 | + |
| 87 | + // Doctrine query returns an array of objects and we need an array of plain arrays |
| 88 | + $usersAsPlainArrays = array_map(function ($user) { |
| 89 | + return array($user->getId(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles())); |
| 90 | + }, $users); |
| 91 | + |
| 92 | + // In your console commands you should always use the regular output type, |
| 93 | + // which outputs contents directly in the console window. However, this |
| 94 | + // particular command uses the BufferedOutput type instead. |
| 95 | + // The reason is that the table displaying the list of users can be sent |
| 96 | + // via email if the '--send-to' option is provided. Instead of complicating |
| 97 | + // things, the BufferedOutput allows to get the command output and store |
| 98 | + // it in a variable before displaying it. |
| 99 | + $bufferedOutput = new BufferedOutput(); |
| 100 | + |
| 101 | + $table = new Table($bufferedOutput); |
| 102 | + $table |
| 103 | + ->setHeaders(array('ID', 'Username', 'Email', 'Roles')) |
| 104 | + ->setRows($usersAsPlainArrays) |
| 105 | + ; |
| 106 | + $table->render(); |
| 107 | + |
| 108 | + // instead of displaying the table of users, store it in a variable |
| 109 | + $tableContents = $bufferedOutput->fetch(); |
| 110 | + |
| 111 | + if (null !== $email = $input->getOption('send-to')) { |
| 112 | + $this->sendReport($tableContents, $email); |
| 113 | + } |
| 114 | + |
| 115 | + $output->writeln($tableContents); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Sends the given $contents to the $recipient email address. |
| 120 | + * |
| 121 | + * @param string $contents |
| 122 | + * @param string $recipient |
| 123 | + */ |
| 124 | + private function sendReport($contents, $recipient) |
| 125 | + { |
| 126 | + // See http://symfony.com/doc/current/cookbook/email/email.html |
| 127 | + $mailer = $this->getContainer()->get('mailer'); |
| 128 | + |
| 129 | + $message = $mailer->createMessage() |
| 130 | + ->setSubject(sprintf('app:list-users report (%s)', date('Y-m-d H:i:s'))) |
| 131 | + ->setFrom($this->getContainer()->getParameter('app.notifications.email_sender')) |
| 132 | + ->setTo($recipient) |
| 133 | + ->setBody($contents, 'text/plain') |
| 134 | + ; |
| 135 | + |
| 136 | + $mailer->send($message); |
| 137 | + } |
| 138 | +} |
0 commit comments