{"id":20674631,"url":"https://github.com/slawomirkania/class-generator-bundle","last_synced_at":"2025-04-19T20:26:47.102Z","repository":{"id":57053152,"uuid":"49527810","full_name":"slawomirkania/class-generator-bundle","owner":"slawomirkania","description":"Generate classes, interfaces and PHPUnit test classes from YAML schema","archived":false,"fork":false,"pushed_at":"2017-04-24T11:35:29.000Z","size":205,"stargazers_count":7,"open_issues_count":6,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-30T18:41:36.383Z","etag":null,"topics":["class","generator","phpunit","symfony","symfony-bundle","yaml"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slawomirkania.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-12T20:45:33.000Z","updated_at":"2017-11-23T15:25:56.000Z","dependencies_parsed_at":"2022-08-24T04:11:34.411Z","dependency_job_id":null,"html_url":"https://github.com/slawomirkania/class-generator-bundle","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slawomirkania%2Fclass-generator-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slawomirkania%2Fclass-generator-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slawomirkania%2Fclass-generator-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slawomirkania%2Fclass-generator-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slawomirkania","download_url":"https://codeload.github.com/slawomirkania/class-generator-bundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224851630,"owners_count":17380388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["class","generator","phpunit","symfony","symfony-bundle","yaml"],"created_at":"2024-11-16T21:06:29.624Z","updated_at":"2024-11-16T21:06:30.399Z","avatar_url":"https://github.com/slawomirkania.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://api.travis-ci.org/slawomirkania/class-generator-bundle.png?branch=master)](https://travis-ci.org/slawomirkania/class-generator-bundle)\n[![Coverage Status](https://coveralls.io/repos/github/slawomirkania/class-generator-bundle/badge.svg?branch=master)](https://coveralls.io/github/slawomirkania/class-generator-bundle?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/slawomirkania/class-generator-bundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/slawomirkania/class-generator-bundle/?branch=master)\n\n# About\n\nClass Generator Bundle\n- Generate classes, interfaces and PHPUnit test classes from the YAML schema.\n- Generator does not overwrite existing methods or properties, only renders new elements.\n- Generated entity class is compatible with JMS Serializer, each property has an annotation @Type based on a property type.\n- Also each property has an annotation @SerializedName which can be adjusted as in the example below (user_setting).\n- Generator allows to add Symfony constraints to a property.\n- Generator allows to handle inheritance.\n\n# Installation\n\n```bash\n$ php composer.phar require slawomirkania/class-generator-bundle dev-master\n```\n\n### Symfony configuration\n\n app/config/config.yml\n```yml\n framework:\n validation: { enable_annotations: true }\n```\napp/AppKernel.php\n```php\npublic function registerBundles()\n{\n    if (in_array($this-\u003egetEnvironment(), array('dev', 'test'), true)) {\n        //...\n            $bundles[] = new HelloWordPl\\SimpleEntityGeneratorBundle\\HelloWordPlSimpleEntityGeneratorBundle();\n        //...\n    }\n    //...\n}\n```\n\n# Usage\n\n### Create YAML structure as below\n\n```yml\n-\n  namespace: \\AppBundle\\Api\\Command\\UserUpdate\n  extends: \\AppBundle\\Api\\Command\\Request\n  configuration:\n    no_interface: true\n    no_phpunit_class: true\n  comment: \"Update user data command\"\n  # in multiline comment you can put whatever you want eg. annotations, poems etc.\n  multiline_comment:\n    - 'lorem ipsum'\n    - 'second row'\n    - '@\\Doctrine\\Common\\Annotations\\Entity()'\n  # you can overwrite default template for specific class, constructor, interface and test class\n  class_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnClassManagerTemplate.txt'\n  class_constructor_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnClassConstructorManagerTemplate.txt'\n  interface_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnInterfaceManagerTemplate.txt'\n  test_class_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnTestClassManagerTemplate.txt'\n  properties:\n    -\n      name: username\n      type: string\n      comment: \"Username for login\"\n      constraints:\n        - NotBlank(message = \"Login can not be empty\")\n        - NotNull(message = \"Login can not be null\")\n      # you can overwrite default template for specific property, setter, getter etc.\n      property_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnPropertyManagerTemplate.txt'\n      test_class_method_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnTestClassManagerTemplate.txt'\n      method_getter_boolean_interface_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnMethodGetterBooleanInterfaceManagerTemplate.txt'\n      method_getter_boolean_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnMethodGetterBooleanManagerTemplate.txt'\n      method_getter_interface_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnMethodGetterInterfaceManagerTemplate.txt'\n      method_getter_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnMethodGetterManagerTemplate.txt'\n    -\n      name: email\n      type: string\n      comment: \"User email\"\n      multiline_comment:\n        - '@\\Doctrine\\Common\\Annotations\\Column()'\n        - 'lorem ipsum'\n        - 'third row'\n      constraints:\n        - NotBlank()\n        - Email(message = \"Invalid email\")\n    -\n      name: active\n      type: boolean\n      comment: \"Wether user is active or not\"\n      constraints:\n        - Type(type = \"boolean\")\n      # you can overwrite default template for specific property, setter, getter etc.\n      method_setter_interface_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnMethodSetterInterfaceManagerTemplate.txt'\n      method_setter_manager_template_path: 'YourOwnBundle/Resources/templates/YourOwnMethodSetterManagerTemplate.txt'\n    -\n      name: groups\n      type: Doctrine\\Common\\Collections\\ArrayCollection\u003cAppBundle\\Api\\Param\\Group\u003e\n      comment: User groups\n    -\n      # default comment\n      name: user_setting\n      serialized_name: userSetting\n      type: AppBundle\\Api\\Param\\Setting\n      # Setter allows 'null' value. Default 'false'.\n      optional: true\n-\n  namespace: \\AppBundle\\Api\\Param\\Group\n  # no comment\n  properties:\n    -\n      name: name\n      type: string\n      comment: \"Group name\"\n      constraints:\n        - NotBlank()\n    -\n      name: description\n      type: string\n      comment: \"Group description\"\n-\n  namespace: \\AppBundle\\Api\\Param\\Setting\n  # no comment\n  # no properties\n```\n\nPut {file_name_with_extension} YAML file into {bundle_name}\\Resources\\config\\\n\nRun Symfony command\n\n```sh\n$ ./bin/console class_generator:generate {bundle_name} {file_name_with_extension}\n```\nOptions (optional)\n\n--no-interfaces - Switches off interfaces generating\n\n--no-phpunit-classes - Switches off PHPUnit classes generating\n\n--only-simulate-file - Simulation of generating classes from file and show summary\n\nYou can also define configuration for specific class, see \\AppBundle\\Api\\Command\\UserUpdate in YAML definition above.\n\n```sh\n$ ./bin/console class_generator:generate {bundle_name} {file_name_with_extension} --no-interfaces --no-phpunit-classes\n```\nAfter processing generated tests (if test classes generated), by command e.g.\n\n```sh\nphpunit {base_url}/src/AppBundle/Tests/\n```\n\nyou should see an error because class \\AppBundle\\Api\\Command\\Request does not exist.\nAppend the following minimalistic code to the YAML structure {bundle_name}\\Resources\\config\\{file_name_with_extension}\n\n```yml\n-\n  namespace: \\AppBundle\\Api\\Command\\Request\n  comment: \"Base class for commands\"\n  # no properties\n```\nRemove code generated before, because when some class has syntax error, generator can not work properly.\nRun Symfony command again.\nExecute tests again and then you should see something like this:\n\n```sh\n...IIIIIIIIIII.IIII.\n\nTime: 124 ms, Memory: 6.75Mb\n```\nNow you can implement missing tests cases.\nTip: For better code format quality use PHP-CS-Fixer on generated files\n\n### Generated files namespaces:\n- AppBundle\\Api\\Command\\Request\n- AppBundle\\Api\\Command\\RequestInterface (when no --no-interfaces option)\n- AppBundle\\Tests\\Api\\Command\\RequestTest (when no --no-phpunit-classes option)\n\n- AppBundle\\Api\\Command\\UserUpdate\n- AppBundle\\Api\\Command\\UserUpdateInterface (when no --no-interfaces option)\n- AppBundle\\Tests\\Api\\Command\\UserUpdateTest (when no --no-phpunit-classes option)\n\n- AppBundle\\Api\\Param\\Group\n- AppBundle\\Api\\Param\\GroupInterface (when no --no-interfaces option)\n- AppBundle\\Tests\\Api\\Param\\GroupTest (when no --no-phpunit-classes option)\n\n- AppBundle\\Api\\Param\\Setting\n- AppBundle\\Api\\Param\\SettingInterface (when no --no-interfaces option)\n- AppBundle\\Tests\\Api\\Param\\SettingTest (when no --no-phpunit-classes option)\n\nYou can check how deserialization of generated classes works.\n```json\n{\n  \"username\": \"slawomir\",\n  \"email\": \"slawomir.kania1@gmail.com\",\n  \"active\": true,\n  \"groups\": [\n    {\n      \"name\": \"admin\",\n      \"description\": \"admin group\"\n    },\n    {\n      \"name\": \"guest\",\n      \"description\": \"guest group\"\n    }\n  ],\n  \"userSetting\": {\n\n  }\n}\n```\n\n```php\n    ...\n    $json = \"{...}\";\n    $serializer = \\JMS\\Serializer\\SerializerBuilder::create()-\u003ebuild();\n    $userUpdateDeserialized = $serializer-\u003edeserialize($json, \"AppBundle\\Api\\Command\\UserUpdate\", \"json\");\n    var_dump($userUpdateDeserialized);\n    ...\n```\n\nYou can override default template for each element in generator.\nTemplates are placed in src/HelloWordPl/SimpleEntityGeneratorBundle/Resources/templates/ directory.\n\nClassConstructorManagerTemplatePath - Constructor template\nClassManagerTemplatePath - Class template\nInitPropertyManagerTemplatePath - Initialization ArrayCollection property in constructor template\nInterfaceManagerTemplatePath - Interface template\nMethodGetterBooleanInterfaceManagerTemplatePath - Interface boolean getter template\nMethodGetterBooleanManagerTemplatePath - Class boolean getter template\nMethodGetterInterfaceManagerTemplatePath - Interface common getter template\nMethodGetterManagerTemplatePath - Class getter template\nMethodSetterInterfaceManagerTemplatePath - Interface boolean setter template\nMethodSetterManagerTemplatePath - Class setter template\nPropertyManagerTemplatePath - Class property template\nTestClassManagerTemplatePath - PHPUnit class template\nTestMethodManagerTemplatePath - PHPUnit class method template\n\nIf you want to replace default item template you have to create your own template (take a look at methods getTemplateTags in each Item Manager src/HelloWordPl/SimpleEntityGeneratorBundle/Lib/Items/*.php, for tags which you can use in your own template)...\n\n```php\n/**\n * My own template for interface getter\n *\n * \u003ccomment\u003e\n * @return \u003cproperty_type\u003e\n */\nfinal public function \u003cmethod_name\u003eMyImagination();\n```\n\n...and set proper template directory (in parameters.yml) which consists of bundle name and path inside this bundle, e.g.\n\n```yml\nparameters:\n    ...\n    MethodGetterInterfaceManagerTemplatePath: 'YourOwnBundle/Resources/templates/YourOwnMethodGetterInterfaceTemplate.txt'\n    ...\n```\n\nYou can also overwrite all methods in Renderer, by creating new Renderer class which extends default renderer class.\nAll you need to do is set new Renderer class namespace in parameters.yml\n\n```yml\nparameters:\n    ...\n    SegRendererClass: \"YourOwnBundle\\\\Lib\\\\OwnRendererClass\"\n    ...\n```\n\n# Changelog\n- Ability to disable/enable interface and/or test class generating per class \n- Item template overwriting supported\n- Multiline comments for class and property supported\n- Optional parameter for setter\n\nLicense\n----\n\nThe MIT License (MIT)\n\nCopyright (c) 2016 Sławomir Kania\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslawomirkania%2Fclass-generator-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslawomirkania%2Fclass-generator-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslawomirkania%2Fclass-generator-bundle/lists"}