Enuma
Package that will help you create and Edit php classes, interfaces and traits dynamically, (*1)
, (*2)
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 :, (*3)
Create an interface
```php
$phpInterface = new PhpInterface('Foo');
$enuma = new Enuma();
$enuma->save($phpInterface, 'path/to/a/file);
```
will create or rewrite the file with :
```php
Create a trait
```php
$phpTrait = new PhpTrait('Foo');
$enuma = new Enuma();
$enuma->save($phpTrait, 'path/to/a/file);
```
will create or rewrite the file with :
```php
setEncoding('your encoding');
```
2. Php Closing Tag
Default behaviour is not having the ?> closing tag
If you want to have it :
```php
$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setUsePhpClosingTag(true);
- Indentation
Default behaviour is 4 spaces,
If you want to change it (only space characters allowed):, (*4)
$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:, (*5)
$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:, (*6)
$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:, (*7)
$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:, (*8)
$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->useWindowsNewLine(true);
- Array annotation
Default behaviour is short annotation []
If you want to use standard annotation array() :, (*9)
$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:, (*10)
$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setAutoComments(false);
Class creation
you can define many things when creating a class/interface/trait :, (*11)
- Coding style:
$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setClassBracesInNewLine(false);
$phpClass = new PhpClass('Foo', $customCodingStyle);
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*12)
<?php
class Foo {
}
- Namespace
Works for : PhpClass, PhpInterface and PhpTrait, (*13)
$phpClass = new PhpClass('Foo');
$phpClass->namespace('My\\Name\\Space');
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*14)
<?php
namespace My\Name\Space;
class Foo
{
}
- Use classes
Works for : PhpClass, PhpInterface and PhpTrait, (*15)
$phpClass = new PhpClass('Foo');
$phpClass->use('My\\Package\\Class1');
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*16)
<?php
use My\Package\Class1;
class Foo
{
}
- Class comment
Works for : PhpClass, PhpInterface and PhpTrait, (*17)
$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:, (*18)
<?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, (*19)
$phpClass = new PhpClass('Foo');
$phpClass
->namespace('My\\Name\\Space')
->abstract(true);
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*20)
<?php
namespace My\Name\Space;
abstract class Foo
{
}
- Make class Final
Only Works for PhpClass, (*21)
$phpClass = new PhpClass('Foo');
$phpClass
->namespace('My\\Name\\Space')
->final(true);
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*22)
<?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., (*23)
- Extend a class
Only Works for PhpClass, (*24)
$phpClass = new PhpClass('Foo');
$phpClass
->namespace('My\\Name\\Space')
->extends('My\\OtherPackage\\Class2');
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*25)
<?php
namespace My\Name\Space;
use My\OtherPackage\Class2;
final class Foo extends Class2
{
}
- Implement interfaces
Only Works for PhpClass, (*26)
$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:, (*27)
<?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, (*28)
$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:, (*29)
<?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, (*30)
$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:, (*31)
<?php
namespace My\Name\Space;
class Foo
{
const My_CONST = true;
const MY_OTHER_CONST = [1, 2, 3];
}
10.1. add a Property, (*32)
Works for PhpClass and PhpTrait, (*33)
$phpClass = new PhpClass('Foo');
$phpClass
->namespace('My\\Name\\Space')
->addProperty(new Property('property1'));
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*34)
<?php
namespace My\Name\Space;
class Foo
{
public $property1;
}
10.2. Set a visibility for a property, (*35)
You can specify a visibility for the property using the Hint class constants:, (*36)
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:, (*37)
<?php
namespace My\Name\Space;
class Foo
{
protected $property1;
}
10.3. Set a property as static, (*38)
$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:, (*39)
<?php
namespace My\Name\Space;
class Foo
{
protected static $property1;
}
10.4. Set a property's default value, (*40)
$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:, (*41)
<?php
namespace My\Name\Space;
class Foo
{
protected static $property1 = null;
}
- add a Method
Works for : PhpClass, PhpInterface and PhpTrait, (*42)
$phpClass = new PhpClass('Foo');
$phpClass
->namespace('My\\Name\\Space')
->addMethod(new Method('bar'));
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*43)
<?php
namespace My\Name\Space;
class Foo
{
/**
* @return mixed
*/
public function bar()
{
}
}
11.1. add a Method Visibility, (*44)
For interfaces, visibility is always public, (*45)
$phpClass = new PhpClass('Foo');
$phpClass
->namespace('My\\Name\\Space')
->addMethod(new Method('bar', Visibility::PROTECTED));
$enuma = new Enuma();
echo $enuma->stringify($phpClass);
will output:, (*46)
<?php
namespace My\Name\Space;
class Foo
{
/**
* @return mixed
*/
protected function bar()
{
}
}
11.2. Set Method as static, (*47)
$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:, (*48)
<?php
namespace My\Name\Space;
class Foo
{
/**
* @return mixed
*/
protected static function bar()
{
}
}
11.3. Set Method as abstract (and the class implicitly), (*49)
$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:, (*50)
<?php
namespace My\Name\Space;
abstract class Foo
{
/**
* @return mixed
*/
abstract protected function bar();
}
11.4. Set Method as final, (*51)
$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:, (*52)
<?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., (*53)
11.5. add Method comment, (*54)
$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:, (*55)
<?php
namespace My\Name\Space;
class Foo
{
/**
* This is my function
* @return mixed
*/
protected function bar()
{
}
}
11.6. add Method Return type, (*56)
$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:, (*57)
<?php
namespace My\Name\Space;
class Foo
{
/**
* This is my function
* @return bool
*/
protected function bar(): bool
{
}
}
11.6. add Method parameters, (*58)
$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:, (*59)
<?php
namespace My\Name\Space;
class Foo
{
/**
* @param bool $test
* @return mixed
*/
protected function bar(bool $test = true)
{
}
}
Edit classes
Coming soon, (*60)