js-php-generator
v0.1.0
Published
Generate PHP code programmatically from JavaScript. A port of nette/php-generator.
Downloads
13
Maintainers
Readme
JavaScript PHP Generator
A JavaScript/TypeScript library for generating PHP code programmatically. This is a port of the popular nette/php-generator package.
Features
✅ Generate PHP classes, interfaces, traits, and enums
✅ Support for PHP 8.5 features including property hooks, enums, attributes, etc.
✅ Modify existing classes programmatically
✅ PSR-12 / PER coding style compliant output
✅ TypeScript support with full type definitions
✅ Node.js and browser compatibility
Installation
npm install js-php-generatorQuick Start
import { ClassType, PhpNamespace, PhpFile } from 'js-php-generator';
// Create a simple class
const class_ = new ClassType('Demo');
class_
.setFinal()
.setExtends('ParentClass')
.addImplement('Countable')
.addComment('Class description.\nSecond line\n')
.addComment('@property-read Nette\\Forms\\Form $form');
// Generate PHP code
console.log(class_.toString());Examples
Basic Class Generation
import { ClassType } from 'js-php-generator';
const class_ = new ClassType('User');
class_
.setFinal()
.addProperty('name')
.setType('string')
.setVisibility('private')
.setValue('John Doe');
class_
.addProperty('age')
.setType('int')
.setVisibility('private')
.setValue(30);
class_
.addMethod('getName')
.setVisibility('public')
.setReturnType('string')
.setBody('return $this->name;');
console.log(class_.toString());Namespace Support
import { PhpNamespace, ClassType } from 'js-php-generator';
const namespace_ = new PhpNamespace('App\\Models');
const userClass = namespace_.addClass('User');
userClass
.addProperty('id')
.setType('int')
.setVisibility('private');
const orderClass = namespace_.addClass('Order');
orderClass
.addProperty('userId')
.setType('int')
.setVisibility('private');
console.log(namespace_.toString());PHP File Generation
import { PhpFile, PhpNamespace, ClassType } from 'js-php-generator';
const file = new PhpFile();
file.addComment('This file is auto-generated.');
file.setStrictTypes();
const namespace_ = file.addNamespace('App\\Controllers');
const controller = namespace_.addClass('UserController');
controller
.addMethod('index')
.setVisibility('public')
.setReturnType('array')
.setBody('return [];');
console.log(file.toString());Advanced Features
import { ClassType } from 'js-php-generator';
const class_ = new ClassType('AdvancedDemo');
// Add attributes (PHP 8.0+)
class_
.addAttribute('Route', ['/api/users'])
.addAttribute('ApiResource');
// Add constructor
const constructor = class_.addMethod('__construct');
constructor
.addParameter('name')
.setType('string')
.setRequired(true);
constructor
.addParameter('age')
.setType('int')
.setDefaultValue(18);
// Add typed properties
class_
.addProperty('items')
.setType('array')
.setVisibility('private')
.setValue([]);
console.log(class_.toString());API Reference
Core Classes
ClassType- Represents a PHP classInterfaceType- Represents a PHP interfaceTraitType- Represents a PHP traitEnumType- Represents a PHP enumPhpNamespace- Represents a PHP namespacePhpFile- Represents a complete PHP fileMethod- Represents a PHP methodProperty- Represents a PHP propertyParameter- Represents a method parameterAttribute- Represents a PHP attribute
Utility Classes
Printer- Base printer for generating PHP codePsrPrinter- PSR-12 compliant printerDumper- Variable dumper for generating PHP codeClassManipulator- Tools for manipulating existing classes
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This project is a JavaScript port of nette/php-generator by Nette Framework.
