8000 Updated the application to Symfony 3.3.0 by javiereguiluz · Pull Request #562 · symfony/demo · GitHub
[go: up one dir, main page]

Skip to content

Updated the application to Symfony 3.3.0 #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use DI features for commands too
  • Loading branch information
javiereguiluz committed May 22, 2017
commit faf65d3d86e8b4c2f5702c8e045627a0a8fcde27
4 changes: 4 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ services:

# Autowiring can't guess the constructor arguments that are not type-hinted with
# classes (e.g. container parameters) so you must define those arguments explicitly
AppBundle\Command\ListUsersCommand:
arguments:
$emailSender: '%app.notifications.email_sender%'

AppBundle\Twig\AppExtension:
arguments:
$locales: '%app_locales%'
Expand Down
35 changes: 14 additions & 21 deletions src/AppBundle/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
namespace AppBundle\Command;

use AppBundle\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;

/**
* A command console that creates users and stores them in the database.
Expand All @@ -39,14 +40,20 @@
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class AddUserCommand extends ContainerAwareCommand
class AddUserCommand extends Command
{
const MAX_ATTEMPTS = 5;

/**
* @var ObjectManager
*/
private $entityManager;
private $passwordEncoder;

public function __construct(EntityManagerInterface $em, UserPasswordEncoder $encoder)
{
parent::__construct();

$this->entityManager = $em;
$this->passwordEncoder = $encoder;
}

/**
* {@inheritdoc}
Expand All @@ -68,19 +75,6 @@ protected function configure()
;
}

/**
* This method is executed before the interact() and the execute() methods.
* It's main purpose is to initialize the variables used in the rest of the
* command methods.
*
* Beware that the input options and arguments are validated after executing
* the interact() method, so you can't blindly trust their values in this method.
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->entityManager = $this->getContainer()->get('doctrine')->getManager();
}

/**
* This method is executed after initialize() and before execute(). Its purpose
* is to check if some of the options/arguments are missing and interactively
Expand Down Expand Up @@ -206,8 +200,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);

// See https://symfony.com/doc/current/book/security.html#security-encoding-password
$encoder = $this->getContainer()->get('security.password_encoder');
$encodedPassword = $encoder->encodePassword($user, $plainPassword);
$encodedPassword = $this->passwordEncoder->encodePassword($user, $plainPassword);
$user->setPassword($encodedPassword);

$this->entityManager->persist($user);
Expand Down
21 changes: 10 additions & 11 deletions src/AppBundle/Command/DeleteUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace AppBundle\Command;

use AppBundle\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -36,15 +36,19 @@
*
* @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
*/
class DeleteUserCommand extends ContainerAwareCommand
class DeleteUserCommand extends Command
{
const MAX_ATTEMPTS = 5;

/**
* @var ObjectManager
*/
private $entityManager;

public function __construct(EntityManagerInterface $em)
{
parent::__construct();

$this->entityManager = $em;
}

/**
* {@inheritdoc}
*/
Expand All @@ -67,11 +71,6 @@ protected function configure()
);
}

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->entityManager = $this->getContainer()->get('doctrine')->getManager();
}

protected function interact(InputInterface $input, OutputInterface $output)
{
if (null !== $input->getArgument('username')) {
Expand Down
36 changes: 16 additions & 20 deletions src/AppBundle/Command/ListUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace AppBundle\Command;

use AppBundle\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -34,12 +34,19 @@
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class ListUsersCommand extends ContainerAwareCommand
class ListUsersCommand extends Command
{
/**
* @var ObjectManager
*/
private $entityManager;
private $mailer;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing $emailSender property


public function __construct(EntityManagerInterface $em, \Swift_Mailer $mailer, $emailSender)
{
parent::__construct();

$this->entityManager = $em;
$this->mailer = $mailer;
$this->emailSender = $emailSender;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -74,15 +81,6 @@ protected function configure()
;
}

/**
* This method is executed before the the execute() method. It's main purpose
* is to initialize the variables used in the rest of the command methods.
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->entityManager = $this->getContainer()->get('doctrine')->getManager();
}

/**
* This method is executed after initialize(). It usually contains the logic
* to execute to complete this command task.
Expand Down Expand Up @@ -133,15 +131,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
private function sendReport($contents, $recipient)
{
// See https://symfony.com/doc/current/cookbook/email/email.html
$mailer = $this->getContainer()->get('mailer');

$message = $mailer->createMessage()
$message = $this->mailer->createMessage()
->setSubject(sprintf('app:list-users report (%s)', date('Y-m-d H:i:s')))
->setFrom($this->getContainer()->getParameter('app.notifications.email_sender'))
->setFrom($this->emailSender)
->setTo($recipient)
->setBody($contents, 'text/plain')
;

$mailer->send($message);
$this->mailer->send($message);
}
}
0