Skip to content

Commit a48b9cf

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [Console][Mailer][Security] Added PHP type declarations
2 parents ac05b01 + 94e398d commit a48b9cf

25 files changed

+115
-103
lines changed

console.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ want a command to create a user::
3838
// the name of the command (the part after "bin/console")
3939
protected static $defaultName = 'app:create-user';
4040

41-
protected function configure()
41+
protected function configure(): void
4242
{
4343
// ...
4444
}
4545

46-
protected function execute(InputInterface $input, OutputInterface $output)
46+
protected function execute(InputInterface $input, OutputInterface $output): int
4747
{
4848
// ... put here the code to create the user
4949

@@ -80,7 +80,7 @@ You can optionally define a description, help message and the
8080
:doc:`input options and arguments </console/input>`::
8181

8282
// ...
83-
protected function configure()
83+
protected function configure(): void
8484
{
8585
$this
8686
// the short description shown while running "php bin/console list"
@@ -115,7 +115,7 @@ available in the ``configure()`` method::
115115
parent::__construct();
116116
}
117117

118-
protected function configure()
118+
protected function configure(): void
119119
{
120120
$this
121121
// ...
@@ -151,7 +151,7 @@ The ``execute()`` method has access to the output stream to write messages to
151151
the console::
152152

153153
// ...
154-
protected function execute(InputInterface $input, OutputInterface $output)
154+
protected function execute(InputInterface $input, OutputInterface $output): int
155155
{
156156
// outputs multiple lines to the console (adding "\n" at the end of each line)
157157
$output->writeln([
@@ -204,7 +204,7 @@ method, which returns an instance of
204204

205205
class MyCommand extends Command
206206
{
207-
protected function execute(InputInterface $input, OutputInterface $output)
207+
protected function execute(InputInterface $input, OutputInterface $output): int
208208
{
209209
if (!$output instanceof ConsoleOutputInterface) {
210210
throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
@@ -251,7 +251,7 @@ Use input options or arguments to pass information to the command::
251251
use Symfony\Component\Console\Input\InputArgument;
252252

253253
// ...
254-
protected function configure()
254+
protected function configure(): void
255255
{
256256
$this
257257
// configure an argument
@@ -261,7 +261,7 @@ Use input options or arguments to pass information to the command::
261261
}
262262

263263
// ...
264-
public function execute(InputInterface $input, OutputInterface $output)
264+
public function execute(InputInterface $input, OutputInterface $output): int
265265
{
266266
$output->writeln([
267267
'User Creator',
@@ -315,7 +315,7 @@ as a service, you can use normal dependency injection. Imagine you have a
315315

316316
// ...
317317

318-
protected function execute(InputInterface $input, OutputInterface $output)
318+
protected function execute(InputInterface $input, OutputInterface $output): int
319319
{
320320
// ...
321321

console/calling_commands.rst

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,40 @@ or if you want to create a "meta" command that runs a bunch of other commands
88
changed on the production servers: clearing the cache, generating Doctrine
99
proxies, dumping web assets, ...).
1010

11-
Calling a command from another one is straightforward::
11+
Use the :method:`Symfony\\Component\\Console\\Application::find` method to
12+
find the command you want to run by passing the command name. Then, create a
13+
new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the
14+
arguments and options you want to pass to the command.
1215

16+
Eventually, calling the ``run()`` method actually runs the command and returns
17+
the returned code from the command (return value from command's ``execute()``
18+
method)::
19+
20+
// ...
21+
use Symfony\Component\Console\Command;
1322
use Symfony\Component\Console\Input\ArrayInput;
1423
use Symfony\Component\Console\Input\InputInterface;
1524
use Symfony\Component\Console\Output\OutputInterface;
16-
// ...
1725

18-
protected function execute(InputInterface $input, OutputInterface $output)
26+
class CreateUserCommand extends Command
1927
{
20-
$command = $this->getApplication()->find('demo:greet');
21-
22-
$arguments = [
23-
'name' => 'Fabien',
24-
'--yell' => true,
25-
];
28+
// ...
2629

27-
$greetInput = new ArrayInput($arguments);
28-
$returnCode = $command->run($greetInput, $output);
30+
protected function execute(InputInterface $input, OutputInterface $output): void
31+
{
32+
$command = $this->getApplication()->find('demo:greet');
2933

30-
// ...
31-
}
34+
$arguments = [
35+
'name' => 'Fabien',
36+
'--yell' => true,
37+
];
3238

33-
First, you :method:`Symfony\\Component\\Console\\Application::find` the
34-
command you want to run by passing the command name. Then, you need to create
35-
a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments
36-
and options you want to pass to the command.
39+
$greetInput = new ArrayInput($arguments);
40+
$returnCode = $command->run($greetInput, $output);
3741

38-
Eventually, calling the ``run()`` method actually runs the command and returns
39-
the returned code from the command (return value from command's ``execute()``
40-
method).
42+
// ...
43+
}
44+
}
4145

4246
.. tip::
4347

console/command_in_controller.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Run this command from inside your controller via::
3636

3737
class SpoolController extends AbstractController
3838
{
39-
public function sendSpool($messages = 10, KernelInterface $kernel)
39+
public function sendSpool(int $messages = 10, KernelInterface $kernel): Response
4040
{
4141
$application = new Application($kernel);
4242
$application->setAutoExit(false);
@@ -87,7 +87,7 @@ Now, use it in your controller::
8787

8888
class SpoolController extends AbstractController
8989
{
90-
public function sendSpool($messages = 10)
90+
public function sendSpool(int $messages = 10): Response
9191
{
9292
// ...
9393
$output = new BufferedOutput(

console/commands_as_services.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ For example, suppose you want to log something from within your command::
3535
parent::__construct();
3636
}
3737

38-
protected function configure()
38+
protected function configure(): void
3939
{
4040
$this
4141
->setDescription('Good morning!');
4242
}
4343

44-
protected function execute(InputInterface $input, OutputInterface $output)
44+
protected function execute(InputInterface $input, OutputInterface $output): int
4545
{
4646
$this->logger->info('Waking up the sun');
4747
// ...

console/hide_commands.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In those cases, you can define the command as **hidden** by setting the
2020
{
2121
protected static $defaultName = 'app:legacy';
2222

23-
protected function configure()
23+
protected function configure(): void
2424
{
2525
$this
2626
->setHidden(true)

console/input.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ and make the ``name`` argument required::
2121
{
2222
// ...
2323

24-
protected function configure()
24+
protected function configure(): void
2525
{
2626
$this
2727
// ...
@@ -42,7 +42,7 @@ You now have access to a ``last_name`` argument in your command::
4242
{
4343
// ...
4444

45-
protected function execute(InputInterface $input, OutputInterface $output)
45+
protected function execute(InputInterface $input, OutputInterface $output): int
4646
{
4747
$text = 'Hi '.$input->getArgument('name');
4848

console/lockable_trait.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ that adds two convenient methods to lock and release commands::
2222

2323
// ...
2424

25-
protected function execute(InputInterface $input, OutputInterface $output)
25+
protected function execute(InputInterface $input, OutputInterface $output): int
2626
{
2727
if (!$this->lock()) {
2828
$output->writeln('The command is already running in another process.');

console/style.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Consider for example the code used to display the title of the following command
2121
{
2222
// ...
2323

24-
protected function execute(InputInterface $input, OutputInterface $output)
24+
protected function execute(InputInterface $input, OutputInterface $output): int
2525
{
2626
$output->writeln([
2727
'<info>Lorem Ipsum Dolor Sit Amet</>',
@@ -62,7 +62,7 @@ title of the command::
6262
{
6363
// ...
6464

65-
protected function execute(InputInterface $input, OutputInterface $output)
65+
protected function execute(InputInterface $input, OutputInterface $output): int
6666
{
6767
$io = new SymfonyStyle($input, $output);
6868
$io->title('Lorem Ipsum Dolor Sit Amet');
@@ -412,7 +412,7 @@ of your commands to change their appearance::
412412
{
413413
// ...
414414

415-
protected function execute(InputInterface $input, OutputInterface $output)
415+
protected function execute(InputInterface $input, OutputInterface $output): int
416416
{
417417
// Before
418418
$io = new SymfonyStyle($input, $output);

console/verbosity.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ level. For example::
4949
{
5050
// ...
5151

52-
public function execute(InputInterface $input, OutputInterface $output)
52+
public function execute(InputInterface $input, OutputInterface $output): int
5353
{
5454
$user = new User(...);
5555

@@ -68,6 +68,8 @@ level. For example::
6868
'Will only be printed in verbose mode or higher',
6969
OutputInterface::VERBOSITY_VERBOSE
7070
);
71+
72+
return 0;
7173
}
7274
}
7375

mailer.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ and create an :class:`Symfony\\Component\\Mime\\Email` object::
264264
namespace App\Controller;
265265

266266
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
267+
use Symfony\Component\HttpFoundation\Response;
267268
use Symfony\Component\Mailer\MailerInterface;
268269
use Symfony\Component\Mime\Email;
269270

@@ -272,7 +273,7 @@ and create an :class:`Symfony\\Component\\Mime\\Email` object::
272273
/**
273274
* @Route("/email")
274275
*/
275-
public function sendEmail(MailerInterface $mailer)
276+
public function sendEmail(MailerInterface $mailer): Response
276277
{
277278
$email = (new Email())
278279

security.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ You can deny access from inside a controller::
950950
// src/Controller/AdminController.php
951951
// ...
952952

953-
public function adminDashboard()
953+
public function adminDashboard(): Response
954954
{
955955
$this->denyAccessUnlessGranted('ROLE_ADMIN');
956956

@@ -994,7 +994,7 @@ using annotations:
994994
+ *
995995
+ * @IsGranted("ROLE_ADMIN")
996996
+ */
997-
public function adminDashboard()
997+
public function adminDashboard(): Response
998998
{
999999
// ...
10001000
}
@@ -1041,7 +1041,7 @@ role::
10411041

10421042
// ...
10431043

1044-
public function adminDashboard()
1044+
public function adminDashboard(): Response
10451045
{
10461046
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
10471047

@@ -1091,7 +1091,7 @@ like this:
10911091
After authentication, the ``User`` object of the current user can be accessed
10921092
via the ``getUser()`` shortcut::
10931093

1094-
public function index()
1094+
public function index(): Response
10951095
{
10961096
// usually you'll want to make sure the user is authenticated first
10971097
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
@@ -1132,6 +1132,8 @@ If you need to get the logged in user from a service, use the
11321132
{
11331133
// returns User object or null if not authenticated
11341134
$user = $this->security->getUser();
1135+
1136+
// ...
11351137
}
11361138
}
11371139

@@ -1224,7 +1226,7 @@ Next, you'll need to create a route for this URL (but not a controller):
12241226
/**
12251227
* @Route("/logout", name="app_logout", methods={"GET"})
12261228
*/
1227-
public function logout()
1229+
public function logout(): void
12281230
{
12291231
// controller can be blank: it will never be executed!
12301232
throw new \Exception('Don\'t forget to activate logout in security.yaml');

security/access_denied_handler.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ response)::
120120

121121
class AccessDeniedHandler implements AccessDeniedHandlerInterface
122122
{
123-
public function handle(Request $request, AccessDeniedException $accessDeniedException)
123+
public function handle(Request $request, AccessDeniedException $accessDeniedException): ?Response
124124
{
125125
// ...
126126

security/csrf.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ this can be customized on a form-by-form basis::
9494
{
9595
// ...
9696

97-
public function configureOptions(OptionsResolver $resolver)
97+
public function configureOptions(OptionsResolver $resolver): void
9898
{
9999
$resolver->setDefaults([
100100
'data_class' => Task::class,
@@ -150,9 +150,10 @@ Then, get the value of the CSRF token in the controller action and use the
150150
to check its validity::
151151

152152
use Symfony\Component\HttpFoundation\Request;
153+
use Symfony\Component\HttpFoundation\Response;
153154
// ...
154155

155-
public function delete(Request $request)
156+
public function delete(Request $request): Response
156157
{
157158
$submittedToken = $request->request->get('token');
158159

0 commit comments

Comments
 (0)