|
| 1 | +SymfonyMakerBundle |
| 2 | +================== |
| 3 | + |
| 4 | +Symfony Maker helps you creating empty commands, controllers, form classes, |
| 5 | +tests and more so you can forget about the required boilerplate code. This |
| 6 | +bundle is an alternative to `SensioGeneratorBundle`_ for modern Symfony |
| 7 | +applications and requires using Symfony 3.4 or newer and `Symfony Flex`_. |
| 8 | + |
| 9 | +Installation |
| 10 | +------------ |
| 11 | + |
| 12 | +Run this command to install and enable this bundle in your application: |
| 13 | + |
| 14 | +.. code-block:: terminal |
| 15 | +
|
| 16 | + $ composer require maker |
| 17 | +
|
| 18 | +Usage |
| 19 | +----- |
| 20 | + |
| 21 | +This bundle provides several commands under the ``maker:`` namespace. List them |
| 22 | +all executing this command: |
| 23 | + |
| 24 | +.. code-block:: terminal |
| 25 | +
|
| 26 | + $ php bin/console list make |
| 27 | +
|
| 28 | + make:command Creates a new console command class |
| 29 | + make:controller Creates a new controller class |
| 30 | + make:entity Creates a new Doctrine entity class |
| 31 | +
|
| 32 | + [...] |
| 33 | +
|
| 34 | + make:validator Creates a new validator and constraint class |
| 35 | + make:voter Creates a new security voter class |
| 36 | +
|
| 37 | +The names of the commands are self-explanatory, but some of them include |
| 38 | +optional arguments and options. Check them out with the ``--help`` option: |
| 39 | + |
| 40 | +.. code-block:: terminal |
| 41 | +
|
| 42 | + $ php bin/console make:controller --help |
| 43 | +
|
| 44 | +Creating your Own Makers |
| 45 | +------------------------ |
| 46 | + |
| 47 | +In case your applications need to generate custom boilerplate code, you can |
| 48 | +create your own ``make:...`` command reusing the tools provided by this bundle. |
| 49 | +Imagine that you need to create a ``make:report`` command. First, create an |
| 50 | +empty command: |
| 51 | + |
| 52 | +.. code-block:: terminal |
| 53 | +
|
| 54 | + $ cd your-project/ |
| 55 | + $ php bin/console make:command 'make:report' |
| 56 | +
|
| 57 | +Then, change the generated command to extend from |
| 58 | +:class:`Symfony\\Bundle\\MakerBundle\\Command\\AbstractCommand`, which is the |
| 59 | +base command used by all ``make:`` commands: |
| 60 | + |
| 61 | +.. code-block:: diff |
| 62 | +
|
| 63 | + // ... |
| 64 | + -use Symfony\Component\Console\Command\Command; |
| 65 | + +use Symfony\Bundle\MakerBundle\Command\AbstractCommand; |
| 66 | +
|
| 67 | + -class MakeReportCommand extends Command |
| 68 | + +class MakeReportCommand extends AbstractCommand |
| 69 | + { |
| 70 | + protected static $defaultName = 'make:report'; |
| 71 | +
|
| 72 | + // ... |
| 73 | + } |
| 74 | +
|
| 75 | +Finally, implement the methods required by the ``AbstractCommand`` class:: |
| 76 | + |
| 77 | + // ... |
| 78 | + use Symfony\Bundle\MakerBundle\Command\AbstractCommand; |
| 79 | + use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 80 | + use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 81 | + |
| 82 | + class MakeReportCommand extends AbstractCommand |
| 83 | + { |
| 84 | + protected static $defaultName = 'make:report'; |
| 85 | + |
| 86 | + // ... |
| 87 | + |
| 88 | + // Returns pairs of name-value parameters used to fill in the |
| 89 | + // skeleton files of the generated code and the success/error messages |
| 90 | + protected function getParameters(): array |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'filename' => sprintf('report-%s.txt', date('YmdHis')), |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + // Returns pairs of skeleton files (absolute paths) and their corresponding |
| 98 | + // generated files (with paths relative to the app) |
| 99 | + protected function getFiles(array $params): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + __DIR__.'/../Resources/skeleton/report.txt' => 'reports/'.$params['filename']; |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + // Optionally, display some message after the generation of code |
| 107 | + protected function writeNextStepsMessage(array $params, ConsoleStyle $io) |
| 108 | + { |
| 109 | + $io->text(sprintf('A new report was generated in the %s file.', $params['filename'])); |
| 110 | + } |
| 111 | + |
| 112 | + // Optionally, define which classes must exist in the application to make |
| 113 | + // this command work (useful to ensure that needed dependencies are installed) |
| 114 | + protected function configureDependencies(DependencyBuilder $dependencies) |
| 115 | + { |
| 116 | + $dependencies->addClassDependency(PdfGenerator::class, ['acme-pdf-generator'], true); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +.. _`SensioGeneratorBundle`: https://github.com/sensiolabs/SensioGeneratorBundle |
| 121 | +.. _`Symfony Flex`: https://symfony.com/doc/current/setup/flex.html |
0 commit comments