PHPackages                             openclassrooms/code-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. openclassrooms/code-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

openclassrooms/code-generator
=============================

Library

v1.2.3(6y ago)763.7k2proprietaryPHPPHP &gt;=7.1

Since Feb 4Pushed 4y ago20 watchersCompare

[ Source](https://github.com/OpenClassrooms/CodeGenerator)[ Packagist](https://packagist.org/packages/openclassrooms/code-generator)[ RSS](/packages/openclassrooms-code-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (18)Versions (15)Used By (0)

CodeGenerator
=============

[](#codegenerator)

[![Build Status](https://github.com/OpenClassrooms/CodeGenerator/workflows/CodeGenerator/badge.svg)](https://github.com/OpenClassrooms/CodeGenerator/)[![SensioLabsInsight](https://camo.githubusercontent.com/5160507726be4297fa2d80a177a761fb14522ac465bdc614f314a8a28857a0a1/68747470733a2f2f696e73696768742e73796d666f6e792e636f6d2f70726f6a656374732f65393164363564382d353565322d346236362d383634392d3162666166373962363764382f6d696e692e737667)](https://insight.symfony.com/account/widget?project=e91d65d8-55e2-4b66-8649-1bfaf79b67d8)[![Coverage](../coverage/coverage.svg)](../coverage/coverage.svg)

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
------------

[](#installation)

The easiest way to install CodeGenerator is via [composer](http://getcomposer.org/).

Create the following `composer.json` file or run the `php composer.phar install` command to install it.

```
composer require --dev openclassrooms/code-generator *

```

or

```
{
    "require": {
        "openclassrooms/code-generator": "*"
    }
}
```

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.

```
{
  "scripts": {
    "post-install-cmd": [
      "OpenClassrooms\\CodeGenerator\\Composer\\ParameterHandler::createGeneratorFileParameters"

    ],
    "post-update-cmd": [
      "OpenClassrooms\\CodeGenerator\\Composer\\ParameterHandler::createGeneratorFileParameters"
    ]
  }
}
```

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:

```
parameters:
    api_dir: 'ApiBundle\'
    app_dir: 'AppBundle\'
    base_namespace: 'OC\'
    stub_namespace: 'Doubles\OC\'
    tests_base_namespace: 'OC\'

    entity_util_classname: 'OC\Util\EntityUtil'

    security_classname: 'OpenClassrooms\UseCase\Application\Annotations\Security'
    transaction_classname: 'OpenClassrooms\UseCase\Application\Annotations\Transaction'
    use_case_classname: 'OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase'
    use_case_request_classname: 'OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest'

    paginated_collection_builder_impl: 'OpenClassrooms\UseCase\Application\Entity\PaginatedCollectionBuilderImpl'
    paginated_collection_classname: 'OC\BusinessRules\Entities\PaginatedCollection'
    paginated_collection_factory: 'OC\AppBundle\Repository\PaginatedCollectionFactory'
    paginated_use_case_response_classname: 'OC\BusinessRules\Responders\PaginatedUseCaseResponse'
    paginated_use_case_response_builder_classname: 'OC\BusinessRules\Responders\PaginatedUseCaseResponseBuilder'
    use_case_response_classname: 'OC\BusinessRules\Responders\UseCaseResponse'
    pagination_classname: 'OC\BusinessRules\Gateways\Pagination'

    abstract_controller : 'OC\ApiBundle\Framework\FrameworkBundle\Controller\AbstractApiController'
    collection_information : 'OC\ApiBundle\ParamConverter\CollectionInformation'
```

You may need to tweak these values depending on the project you are using the code generator in.

Usage
-----

[](#usage)

### Basic execution

[](#basic-execution)

To list all commands:

```
php bin/code-generator

```

To generate a view model architecture:

```
php bin/code-generator code-generator:view-models useCaseResponseClassName

```

To generate a generic use case architecture:

```
php bin/code-generator code-generator:generic-use-case
or
php bin/code-generator code-generator:generic-use-case Domain\\SubDomain UseCaseName

```

To generate an entity CRUD use case architecture:

```
# Create:
php bin/code-generator code-generator:create-entity-use-case EntityClassName
# Delete:
php bin/code-generator code-generator:delete-entity-use-case EntityClassName
# Edit:
php bin/code-generator code-generator:edit-entity-use-case EntityClassName
# Get all:
php bin/code-generator code-generator:get-entities-use-case EntityClassName
# Get:
php bin/code-generator code-generator:get-entity-use-case EntityClassName
```

### Extensions

[](#extensions)

To generate without tests:

```
php bin/code-generator code-generator:view-models useCaseResponseClassName --no-test

```

To generate view model architecture tests only if view model classes already exist:

```
php bin/code-generator code-generator:view-models useCaseResponseClassName --tests-only

```

To dump preview for view model classes:

```
php bin/code-generator code-generator:view-models useCaseResponseClassName --dump

```

### Using the code generator in PHPStorm

[](#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
-----------------------------

[](#how-to-create-a-new-generator)

### To know

[](#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

[](#methodology)

#### 1) Write the file templates you want to generate

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#7-create-config-files-and-update-servicesxml)

Create a new `.xml` file for the generator and add it in `src/Resources/config/service.xml`.

#### 8) Create the command if it does not exist

[](#8-create-the-command-if-it-does-not-exist)

Create your command in `src/Commands` and call mediator `mediate` function in the `execute` method.

### See an example

[](#see-an-example)

For a practical example, check how `src/Generator/ViewModelGenerator.php` is built.

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community23

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 81.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~97 days

Recently: every ~186 days

Total

10

Last Release

1776d ago

Major Versions

v1.2.3 → v2.0.0-alpha2021-06-28

PHP version history (2 changes)1.0PHP &gt;=7.1

v2.0.0-alphaPHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/d82b0e2dcc1728eacb2b83c259311b5ea7c3253bd08f1341d45df79bdd9ba77a?d=identicon)[openclassrooms-admin](/maintainers/openclassrooms-admin)

![](https://www.gravatar.com/avatar/a4eaa1e2bd9272e248e28561739200cbcfd705cc4d3fec9d3e4d595f3dd12165?d=identicon)[romainkuzniak](/maintainers/romainkuzniak)

---

Top Contributors

[![samokiss](https://avatars.githubusercontent.com/u/8625072?v=4)](https://github.com/samokiss "samokiss (398 commits)")[![arnaud-23](https://avatars.githubusercontent.com/u/2587655?v=4)](https://github.com/arnaud-23 "arnaud-23 (62 commits)")[![fredericjaume-oc](https://avatars.githubusercontent.com/u/57905621?v=4)](https://github.com/fredericjaume-oc "fredericjaume-oc (11 commits)")[![oc-clem](https://avatars.githubusercontent.com/u/78789422?v=4)](https://github.com/oc-clem "oc-clem (5 commits)")[![sidux](https://avatars.githubusercontent.com/u/1242225?v=4)](https://github.com/sidux "sidux (5 commits)")[![kletord](https://avatars.githubusercontent.com/u/392556?v=4)](https://github.com/kletord "kletord (4 commits)")[![yassavic](https://avatars.githubusercontent.com/u/66954816?v=4)](https://github.com/yassavic "yassavic (1 commits)")[![killianherbunot](https://avatars.githubusercontent.com/u/10085296?v=4)](https://github.com/killianherbunot "killianherbunot (1 commits)")[![prollandoc](https://avatars.githubusercontent.com/u/70900670?v=4)](https://github.com/prollandoc "prollandoc (1 commits)")[![romainkuzniak](https://avatars.githubusercontent.com/u/1823301?v=4)](https://github.com/romainkuzniak "romainkuzniak (1 commits)")[![samnela](https://avatars.githubusercontent.com/u/1852108?v=4)](https://github.com/samnela "samnela (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/openclassrooms-code-generator/health.svg)

```
[![Health](https://phpackages.com/badges/openclassrooms-code-generator/health.svg)](https://phpackages.com/packages/openclassrooms-code-generator)
```

###  Alternatives

[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M647](/packages/sylius-sylius)[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M565](/packages/symfony-maker-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
