8000 Merge branch '4.4' into 5.2 · SirRFI/symfony-docs@94e398d · GitHub
[go: up one dir, main page]

Skip to content

Commit 94e398d

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: [Console][Mailer][Security] Added PHP type declarations
2 parents 37b0816 + 0bddffd commit 94e398d

25 files changed

+116
-104
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

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

7474
// ...
75-
protected function configure()
75+
protected function configure(): void
7676
{
7777
$this
7878
// the short description shown while running "php bin/console list"
@@ -107,7 +107,7 @@ available in the ``configure()`` method::
107107
parent::__construct();
108108
}
109109

110-
protected function configure()
110+
protected function configure(): void
111111
{
112112
$this
113113
// ...
@@ -143,7 +143,7 @@ The ``execute()`` method has access to the output stream to write messages to
143143
the console::
144144

145145
// ...
146-
protected function execute(InputInterface $input, OutputInterface $output)
146+
protected function execute(InputInterface $input, OutputInterface $output): int
147147
{
148148
// outputs multiple lines to the console (adding "\n" at the end of each line)
149149
$output->writeln([
@@ -196,7 +196,7 @@ method, which returns an instance of
196196

197197
class MyCommand extends Command
198198
{
199-
protected function execute(InputInterface $input, OutputInterface $output)
199+
protected function execute(InputInterface $input, OutputInterface $output): int
200200
{
201201
if (!$output instanceof ConsoleOutputInterface) {
202202
throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
@@ -243,7 +243,7 @@ Use input options or arguments to pass information to the command::
243243
use Symfony\Component\Console\Input\InputArgument;
244244

245245
// ...
246-
protected function configure()
246+
protected function configure(): void
247247
{
248248
$this
249249
// configure an argument
@@ -253,7 +253,7 @@ Use input options or arguments to pass information to the command::
253253
}
254254

255255
// ...
256-
public function execute(InputInterface $input, OutputInterface $output)
256+
public function execute(InputInterface $input, OutputInterface $output): int
257257
{
258258
$output->writeln([
259259
'User Creator',
@@ -307,7 +307,7 @@ as a service, you can use normal dependency injection. Imagine you have a
307307

308308
// ...
309309

310-
protected function execute(InputInterface $input, OutputInterface $output)
310+
protected function execute(InputInterface $input, OutputInterface $output): int
311311
{
312312
// ...
313313

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
->from('hello@example.com')

security.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ You can deny access from inside a controller::
933933
// src/Controller/AdminController.php
934934
// ...
935935

936-
public function adminDashboard()
936+
public function adminDashboard(): Response
937937
{
938938
$this->denyAccessUnlessGranted('ROLE_ADMIN');
939939

@@ -977,7 +977,7 @@ using annotations:
977977
+ *
978978
+ * @IsGranted("ROLE_ADMIN")
979979
+ */
980-
public function adminDashboard()
980+
public function adminDashboard(): Response
981981
{
982982
// ...
983983
}
@@ -1024,7 +1024,7 @@ role::
10241024

10251025
// ...
10261026

1027-
public function adminDashboard()
1027+
public function adminDashboard(): Response
10281028
{
10291029
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
10301030

@@ -1074,7 +1074,7 @@ like this:
10741074
After authentication, the ``User`` object of the current user can be accessed
10751075
via the ``getUser()`` shortcut::
10761076

1077-
public function index()
1077+
public function index(): Response
10781078
{
10791079
// usually you'll want to make sure the user is authenticated first
10801080
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
@@ -1115,6 +1115,8 @@ If you need to get the logged in user from a service, use the
11151115
{
11161116
// returns User object or null if not authenticated
11171117
$user = $this->security->getUser();
1118+
1119+
// ...
11181120
}
11191121
}
11201122

@@ -1207,7 +1209,7 @@ Next, you'll need to create a route for this URL (but not a controller):
12071209
/**
12081210
* @Route("/logout", name="app_logout", methods={"GET"})
12091211
*/
1210-
public function logout()
1212+
public function logout(): void
12111213
{
12121214
// controller can be blank: it will never be executed!
12131215
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
@@ -123,7 +123,7 @@ response)::
123123

124124
class AccessDeniedHandler implements AccessDeniedHandlerInterface
125125
{
126-
public function handle(Request $request, AccessDeniedException $accessDeniedException)
126+
public function handle(Request $request, AccessDeniedException $accessDeniedException): ?Response
127127
{
128128
// ...
129129

security/csrf.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ this can be customized on a form-by-form basis::
8585

8686
// src/Form/TaskType.php
8787
namespace App\Form;
88-
88+
8989
// ...
9090
use App\Entity\Task;
9191
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -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)
0