2017 © Pedro Peláez
 

library enuma

Dynamically Create and Edit php classes, interfaces and traits

image

kanel/enuma

Dynamically Create and Edit php classes, interfaces and traits

  • Tuesday, March 6, 2018
  • by elkaadka
  • Repository
  • 0 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Enuma

Package that will help you create and Edit php classes, interfaces and traits dynamically, (*1)

build, (*2)

Create classes, interfaces and traits

Basic usage

  1. 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);
  1. Indentation

Default behaviour is 4 spaces, If you want to change it (only space characters allowed):, (*4)

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setIndentation("\t");
  1. 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 {

}
  1. 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() {

    }
}
  1. 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);
  1. 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);
  1. Array annotation

Default behaviour is short annotation [] If you want to use standard annotation array() :, (*9)

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->useShortArrayNotation(false);
  1. 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)

  1. 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 {

}

  1. 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
{

}

  1. 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
{

}

  1. 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
{

}

  1. 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
{

}

  1. 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)

  1. 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
{

}
  1. 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
{

}
  1. 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;
}
  1. 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;
}
  1. 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)

The Versions

06/03 2018

dev-master

9999999-dev

Dynamically Create and Edit php classes, interfaces and traits

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

by Adil El Kanabi

04/03 2018

1.0.0

1.0.0.0

Dynamically Create and Edit php classes, interfaces and traits

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

by Adil El Kanabi

04/03 2018

0.1.0

0.1.0.0

Dynamically Create and Edit php classes, interfaces and traits

  Sources   Download

The Requires

 

by Adil El Kanabi