kanel / enuma
Dynamically Create and Edit php classes, interfaces and traits
Requires
- php: >=7.0
Requires (Dev)
- phpspec/phpspec: ^4.3
This package is not auto-updated.
Last update: 2024-12-17 22:22:09 UTC
README
Package that will help you create and Edit php classes, interfaces and traits dynamically
Create classes, interfaces and traits
Basic usage
- Create a class
$phpClass = new PhpClass('Foo'); $enuma = new Enuma(); $enuma->save($phpClass, 'path/to/a/file);
will create or rewrite the file with :
<?php class Foo { }
- Create an interface
$phpInterface = new PhpInterface('Foo'); $enuma = new Enuma(); $enuma->save($phpInterface, 'path/to/a/file);
will create or rewrite the file with :
<?php interface Foo { }
- Create a trait
$phpTrait = new PhpTrait('Foo'); $enuma = new Enuma(); $enuma->save($phpTrait, 'path/to/a/file);
will create or rewrite the file with :
<?php trait Foo { }
Advanced usage
Coding style
Default coding style is PSR2. If you need to customize it you can :
- Encoding
Default PSR2 encoding is UTF-8, if you want to use yours :
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setEncoding('your encoding');
- Php Closing Tag
Default behaviour is not having the ?> closing tag If you want to have it :
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setUsePhpClosingTag(true);
- Indentation
Default behaviour is 4 spaces, If you want to change it (only space characters allowed):
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setIndentation("\t");
- Class braces
Default behaviour is class opening braces in new line, If you want to have them on same line as the class:
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setClassBracesInNewLine(false); //will generate : class A { }
- Method braces
Default behaviour is method's opening braces in new line, If you want to have them on same line as the method:
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setMethodBracesInNewLine(false); //will generate : class A { public function aaa() { } }
- Unix line feed
Default behaviour is having an extra new line after class braces closing, If you don't want to have it:
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setUnixLineFeedEnding(false);
- Windows new line
Default behaviour is Unix new line \n Ifw you want to have windows new line \r\n:
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->useWindowsNewLine(true);
- Array annotation
Default behaviour is short annotation [] If you want to use standard annotation array() :
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->useShortArrayNotation(false);
- Auto comments
Default behaviour is adding automatic @param and @return comments for methods If you don't want to have them:
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setAutoComments(false);
Class creation
you can define many things when creating a class/interface/trait :
- Coding style:
$customCodingStyle = new CustomCodingStyle(); $customCodingStyle->setClassBracesInNewLine(false); $phpClass = new PhpClass('Foo', $customCodingStyle); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php class Foo { }
- Namespace
Works for : PhpClass, PhpInterface and PhpTrait
$phpClass = new PhpClass('Foo'); $phpClass->namespace('My\\Name\\Space'); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { }
- Use classes
Works for : PhpClass, PhpInterface and PhpTrait
$phpClass = new PhpClass('Foo'); $phpClass->use('My\\Package\\Class1'); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php use My\Package\Class1; class Foo { }
- Class comment
Works for : PhpClass, PhpInterface and PhpTrait
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->use('My\\Package\\Class1') ->addComment("This is my class\n@package My\\Name\\Space"); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; use My\Package\Class1; /** * This is my class * @package My\Name\Space */ class Foo { }
- Make class Abstract
Only Works for PhpClass
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->abstract(true); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; abstract class Foo { }
- Make class Final
Only Works for PhpClass
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->final(true); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; final class Foo { }
Since a class can either be final or abstract, setting one to true automatically sets the other to false.
- Extend a class
Only Works for PhpClass
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->extends('My\\OtherPackage\\Class2'); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; use My\OtherPackage\Class2; final class Foo extends Class2 { }
- Implement interfaces
Only Works for PhpClass
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->implements('My\\OtherPackage\\Interface1') ->implements('My\\OtherPackage\\Interface2'); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; use My\OtherPackage\Interface1; use My\OtherPackage\Interface2; class Foo implements Interface1, Interface2 { }
- Use a trait
Works for PhpClass and PhpTrait
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->useTrait('My\\OtherPackage\\Trait1') ->useTrait('My\\OtherPackage\\Trait2'); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; use My\OtherPackage\Trait1; use My\OtherPackage\Trait2; class Foo { use Trait1; use Trait2; }
- add a Constant
Works for PhpClass and PhpInterface
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addConst(new Constant('MY_CONST', true)); ->addConst(new Constant('MY_OTHER_CONST', array(1, 2, 3)); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { const My_CONST = true; const MY_OTHER_CONST = [1, 2, 3]; }
10.1. add a Property
Works for PhpClass and PhpTrait
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addProperty(new Property('property1')); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { public $property1; }
10.2. Set a visibility for a property
You can specify a visibility for the property using the Hint class constants:
Kanel\Enuma\Hint\Visibility::PROTECTED;
Kanel\Enuma\Hint\Visibility::PUBLIC; <-- default
Kanel\Enuma\Hint\Visibility::PRIVATE;
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addProperty(new Property('property1', Visibility::PROTECTED)); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { protected $property1; }
10.3. Set a property as static
$property = new Property('property1', Visibility::PROTECTED); $property->setIsStatic(true); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addProperty($property); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { protected static $property1; }
10.4. Set a property's default value
$property = new Property('property1', Visibility::PROTECTED); $property->setValue(null); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addProperty($property); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { protected static $property1 = null; }
- add a Method
Works for : PhpClass, PhpInterface and PhpTrait
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod(new Method('bar')); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * @return mixed */ public function bar() { } }
11.1. add a Method Visibility
For interfaces, visibility is always public
$phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod(new Method('bar', Visibility::PROTECTED)); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * @return mixed */ protected function bar() { } }
11.2. Set Method as static
$method = new Method('bar', Visibility::PROTECTED); $method->setIsStatic(true); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod($method); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * @return mixed */ protected static function bar() { } }
11.3. Set Method as abstract (and the class implicitly)
$method = new Method('bar', Visibility::PROTECTED); $method->setIsAbstract(true); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod($method); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; abstract class Foo { /** * @return mixed */ abstract protected function bar(); }
11.4. Set Method as final
$method = new Method('bar', Visibility::PROTECTED); $method->setIsFinal(true); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod($method); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * @return mixed */ final protected function bar() { } }
Since a method can either be final or abstract, setting one to true automatically sets the other to false.
11.5. add Method comment
$method = new Method('bar', Visibility::PROTECTED); $method->setComment('This is my function'); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod($method); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * This is my function * @return mixed */ protected function bar() { } }
11.6. add Method Return type
$method = new Method('bar', Visibility::PROTECTED); $method->setReturnType('bool'); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod($method); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * This is my function * @return bool */ protected function bar(): bool { } }
11.6. add Method parameters
$method = new Method('bar', Visibility::PROTECTED); $parameter = new Parameter("test"); $parameter->setValue(true); $parameter->setType('bool'); $method->addParameter($parameter); $phpClass = new PhpClass('Foo'); $phpClass ->namespace('My\\Name\\Space') ->addMethod($method); $enuma = new Enuma(); echo $enuma->stringify($phpClass);
will output:
<?php namespace My\Name\Space; class Foo { /** * @param bool $test * @return mixed */ protected function bar(bool $test = true) { } }
Edit classes
Coming soon