Download the PHP package openclassrooms/code-generator without Composer
On this page you can find all versions of the php package openclassrooms/code-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package code-generator
CodeGenerator
CodeGenerator is a library which generates classes in a Clean Architecture context.
From any use case response, developers have the possibility to generate:
- Generic use case architecture
- Entity use case Get architecture
- Entities use case Get architecture
- Create Entity use case architecture
- Delete Entity use case architecture
- ViewModel architecture
- Controller and models classes
- Unit tests for each class generated
Installation
The easiest way to install CodeGenerator is via composer.
Create the following composer.json
file or run the php composer.phar install
command to install it.
or
Setup post-install and post-update hooks in your composer.json
's script section,
for the oc_code_generator.yml
default configuration to be generated.
This file is mandatory for the code generator to work.
The script creates a file named oc_code_generator.yml
at the root of the project, and will ask you interactively for parameters
which are missing in the parameters file, using the values of the dist file as default values, as follows:
You may need to tweak these values depending on the project you are using the code generator in.
Usage
Basic execution
To list all commands:
To generate a view model architecture:
To generate a generic use case architecture:
To generate an entity CRUD use case architecture:
Extensions
To generate without tests:
To generate view model architecture tests only if view model classes already exist:
To dump preview for view model classes:
Using the code generator in PHPStorm
You can set up External Tools entries in PHPStorm to be able to run some code generator command from your IDE by right-clicking on classes in the project tree (e.g., an entity class).
For the generic use case generation, add an external tool entry like this:
Program: /usr/local/bin/php Arguments: bin/code-generator code-generator:generic-use-case $Prompt$ Working Directory: $ProjectFileDir$
For a get entity use case generation, add an external tool entry like this:
Program: /usr/local/bin/php Arguments: bin/code-generator code-generator:get-entity-use-case $FilePath$ $Prompt$ Working Directory: $ProjectFileDir$
Note that this will only work if the code generator is correctly
setup in your project's composer.json
. You may need to tweak this for your local
PHP binary path.
How to create a new generator
To know
- A generated file is described by a skeleton, in the skeleton directory.
- A generator grabs data and sends it to the skeleton (just like a web page).
- A generator MUST generate only one file.
- A mediator is responsible for calling different generators.
- A mediator can call many other mediators.
- The command MUST call a mediator.
- Each class MUST have an interface and its implementation.
- FileObject contains all needed class information to generate a file.
- Factories are used to create FileObject from Domain and Entity name.
- Entity name and Domain are getting from ClassNameUtility trait.
- If you need another class information in your generator, use factories to create the needed FileObject.
- Some utility classes are used to generate stubs, constants and others things.
- In view model command, if the use case response class name contains base namespace, the generator uses it when needed.
Methodology
1) Write the file templates you want to generate
First, you have to create twig templates for the expected files after generation.
2) Create generator RequestDTO and generator RequestBuilder
Create class with the entity name suffixed by RequestBuilder
, this is used as parameter in generator main method.
3) Start Generator Implementation
The main goal of the generator class is to generate the expected FileObject, but to succeed, it is necessary to build other FileObject from factories and use available Utilities.
4) Create GeneratorTest
Create class with the entity name suffixed by GeneratorTest
and the related stub class (entity name suffixed by FileObjectStub1
). The stub MUST contain expected values to compare with the actual object.
5) Create skeletonModel and SkeletonModelAssembler
Create two classes both prefixed by the entity name:
- Firstly, an abstract class
SkeletonModel
, - Secondly
SkeletonModelAssembler
, Both are used to create the object which will be used in the template.6) Create mediator or add generator in existing mediator
The command uses a mediator pattern to generate classes by functional group. Add the new generator in the concerned group and update the mediator config.
7) Create config files and update services.xml
Create a new
.xml
file for the generator and add it insrc/Resources/config/service.xml
.8) Create the command if it does not exist
Create your command in
src/Commands
and call mediatormediate
function in theexecute
method.
See an example
For a practical example, check how src/Generator/ViewModelGenerator.php
is built.
All versions of code-generator with dependencies
composer/composer Version ~1.8
doctrine/annotations Version ~1.6
doctrine/inflector Version ~1.3
fzaninotto/faker Version ^1.8
incenteev/composer-parameter-handler Version ~2.0
nesbot/carbon Version ~1.22 || ~2.1
symfony/config Version ~3.3 || ~4.0
symfony/console Version ~3.3 || ~4.0
symfony/dependency-injection Version ~3.3 || ~4.0
symfony/filesystem Version ~3.3 || ~4.0
symfony/finder Version ~3.3 || ~4.0
symfony/yaml Version ~3.3 || ~4.0
twig/twig Version ^1.38 || ^2.7
openclassrooms/cache Version 1.0.x@dev
openclassrooms/use-case Version 1.0.x@dev