1. Go to this page and download the library: Download murtukov/php-code-generator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
murtukov / php-code-generator example snippets
use Murtukov\PHPCodeGenerator\PhpFile;
$file = PhpFile::new()->setNamespace('App\Generator');
$class = $file->createClass('MyClass');
$class->setExtends('App\My\BaseClass')
->addImplements(Traversable::class, JsonSerializable::class)
->setFinal()
->addDocBlock("This file was generated and shouldn't be modified")
->addConstructor();
echo $file;
namespace App\Generator;
use App\My\BaseClass;
/**
* This file was generated and shouldn't be modified
*/
final class MyClass extends BaseClass implements Traversable, JsonSerializable
{
public function __construct()
{
}
}
use Murtukov\PHPCodeGenerator\Comment;
use Murtukov\PHPCodeGenerator\Literal;
use Murtukov\PHPCodeGenerator\Method;
use Murtukov\PHPCodeGenerator\Modifier;
use Murtukov\PHPCodeGenerator\PhpClass;
$class = PhpClass::new('Stringifier')
->addConst('KNOWN_TYPES', ['DYNAMIC', 'STATIC'], Modifier::PRIVATE)
->addProperty('errors', Modifier::PRIVATE, '', [])
->addImplements(JsonSerializable::class, ArrayAccess::class)
->setExtends(Exception::class)
->setFinal()
->addDocBlock('This is just a test class.');
$class->emptyLine();
$class->createConstructor()
->append('parent::__construct(...func_get_args())');
# Create a method separately
$method = Method::new('getErrors', Modifier::PUBLIC, 'array')
->append(Comment::slash('Add here your content...'))
->append('return ', new Literal('[]'))
;
$class->append($method);
echo $class;
/**
* This is just a test class.
*/
final class Stringifier extends Exception implements JsonSerializable, ArrayAccess
{
private const KNOWN_TYPES = ['DYNAMIC', 'STATIC'];
private $errors = [];
public function __construct()
{
parent::__construct(...func_get_args());
}
public function getErrors(): array
{
// Add here your content...
return [];
}
}
use Murtukov\PHPCodeGenerator\BlockInterface;
use Murtukov\PHPCodeGenerator\ConverterInterface;
use Murtukov\PHPCodeGenerator\Method;
use Murtukov\PHPCodeGenerator\Modifier;
use Murtukov\PHPCodeGenerator\PhpInterface;
$interface = PhpInterface::new('StringifierInterface')
->addExtends(BlockInterface::class, ConverterInterface::class)
->addConst('NAME', 'MyInterface')
->addConst('TYPE', 'Component')
->emptyLine()
->addSignature('parse', 'string');
$interface->emptyLine();
$signature = $interface->createSignature('stringify', 'string');
$signature->addArgument('escapeSlashes', 'bool', false);
$signature->addDocBlock('Convert value to string.');
$interface->emptyLine();
$dumpMethod = Method::new('dump', Modifier::PUBLIC, 'string');
$interface->addSignatureFromMethod($dumpMethod);
echo $interface;
interface StringifierInterface extends BlockInterface, ConverterInterface
{
public const NAME = 'MyInterface';
public const TYPE = 'Component';
public function parse(): string;
/**
* Convert value to string.
*/
public function stringify(bool $escapeSlashes = false): string;
public function dump(): string;
}
use Murtukov\PHPCodeGenerator\Comment;
use Murtukov\PHPCodeGenerator\Literal;
use Murtukov\PHPCodeGenerator\Method;
use Murtukov\PHPCodeGenerator\Modifier;
use Murtukov\PHPCodeGenerator\PhpTrait;
$trait = PhpTrait::new('Stringifier')
->addProperty('cache', Modifier::PRIVATE, 'array', [])
->addProperty('heap', Modifier::PROTECTED, SplHeap::class, null)
->addDocBlock('This is just a test trait.')
->emptyLine();
$constructor = Method::new('__construct')
->append('parent::__construct(...func_get_args())');
$method = Method::new('getErrors', Modifier::PUBLIC, 'array')
->append(Comment::hash('Add your content here...'))
->append('return ', new Literal('[]'));
$trait->append($constructor);
$trait->emptyLine();
$trait->append($method);
echo $trait;
/**
* This is just a test class.
*/
trait Stringifier
{
private array $cache = [];
protected ?SplHeap $heap = null;
public function __construct()
{
parent::__construct(...func_get_args());
}
public function getErrors(): array
{
# Add your content here...
return [];
}
}
use Murtukov\PHPCodeGenerator\Argument;
use Murtukov\PHPCodeGenerator\Instance;
use Murtukov\PHPCodeGenerator\Func;
$func = Func::new('myMethod', 'void');
# Crete argument and return it
$func->createArgument('arg1', SplHeap::class, null)->setNullable();
# Create argument and return function
$func->addArgument('arg2', 'string', '');
# Adding argument from object
$func->add(Argument::new('arg3'));
# Add content
$func->append('$object = ', Instance::new(stdClass::class));
echo $func;
function myMethod(?SplHeap $arg1 = null, string $arg2 = '', $arg3): void
{
$object = new stdClass();
}
use Murtukov\PHPCodeGenerator\Argument;
use Murtukov\PHPCodeGenerator\Instance;
use Murtukov\PHPCodeGenerator\Method;
use Murtukov\PHPCodeGenerator\Modifier;
$method = Method::new('myMethod', Modifier::PRIVATE, 'void');
# Crete argument and return it
$method->createArgument('arg1', SplHeap::class, null)->setNullable();
# Create argument and return function
$method->addArgument('arg2', 'string', '');
# Adding argument from object
$method->add(Argument::new('arg3'));
# Add content
$method->append('$object = ', Instance::new(stdClass::class));
echo $method;
private function myMethod(?SplHeap $arg1 = null, string $arg2 = '', $arg3): void
{
$object = new stdClass();
}
use Murtukov\PHPCodeGenerator\Instance;
# Create an instance with a single argument
$instance = Instance::new('App\Entity\DateTime', '2000-01-01');
# Add a second argument
$instance->addArgument(null);
echo $instance;
$literal = Literal::new(
'$foo = %s; sprintf("This value should not be quoted %%s.", %s);',
Literal::new('"bar"'),
Literal::new('$foo')
);
echo $literal;
$foo = "bar"; sprintf("This value should not be quoted %s.", $foo);
use Murtukov\PHPCodeGenerator\Config;
Config::$indent = ' ';
use Murtukov\PHPCodeGenerator\Config;
Config::$shortenQualifiers = false;
use Murtukov\PHPCodeGenerator\Config;
Config::$suppressSymbol = '%';
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.