PHP code example of lucid / writer

1. Go to this page and download the library: Download lucid/writer 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/ */

    

lucid / writer example snippets




use Lucid\Writer\Writer;

$writer = new Writer;

$writer
    ->writeln('foo')
    ->writeln('bar');

echo $writer->dump();  //"foo\nbar"



use Lucid\Writer\Writer;

$writer = new Writer;
$writer->allowTrailingSpace(true); // will now preserve trailing space chars for each line.




use Lucid\Writer\Writer;

$writer = new Writer(2);

$writer
    ->writeln('foo')
    ->indent()
    ->writeln('bar');

echo $writer->dump(); //"foo\n  bar"



use Lucid\Writer\Writer;

$writer = new Writer;
$writer->useTabs(true);

// …


$alias = $cg->getImportResolver()->getAlias('Acme\MyClass') // e.g. AcmeMyClassAlias;



use Lucid\Writer\Object\ClassWriter;

$iw = new InterfaceWriter('Foo', 'Acme', '\Acme\Parent');

file_put_contents('Acme/Foo.php', $iw->generate());




/*
 * This file was generated at 2014-07-08 12:23:22.
 */

namespace Acme;

/**
 * @interface Foo
 * @see Acme\Parent
 */
interface Foo extends Parent
{
}



use Lucid\Writer\Object\ClassWriter;

$cg = new ClassWriter('Foo', 'Acme');

file_put_contents('Acme/Foo.php', $cg->generate());




/*
 * This file was generated at 2014-07-08 12:23:22.
 */

namespace Acme;

/**
 * @class Foo
 */
class Foo
{
}



use Lucid\Writer\Writer;
use Lucid\Writer\Object\Constant;
use Lucid\Writer\Object\Argument;
use Lucid\Writer\Object\Method;
use Lucid\Writer\Object\Property;
use Lucid\Writer\Object\ClassGenerator;

$cg = new ClassGenerator('Foo', 'Acme');

$cg->setParent('Acme\Lib\Bar');
$cg->addProperty(new Property('foo', Property::IS_PRIVATE, 'string'));
$cg->addConstant(new Constant('T_ASW', '42', 'int'));
$cg->addMethod($method = new Method('__construct', Method::IS_PUBLIC, Method::T_VOID));

// declare method:
$method->setDescription('Constructor.')
$method->addArgument(new Argument('foo', Method::T_STRING, 'null'));
$method->setBody('$this->foo = $foo;');

// Add traits:
$cg->addTrait($foo = 'Acme\Lib\Traits\FooTrait');
$cg->addTrait($bar = 'Acme\Lib\Traits\BarTrait');
// resolve trait conflicts:
$cg->useTraitMethodAs($foo, 'getFoo', 'getFooStr', Method::IS_PRIVATE);
$cg->replaceTraitConflict($bar, $foo, 'getBar');

// modify the class doc.
$cg->getObjDoc()
    ->setDescription('Some class.')
    ->setLongDescription("Some more info on the class.\nSome more lines.")
    ->addAnnotation('author', 'Thomas Appel <[email protected]>');

echo $cg->generate();




/*
 * This file was generated at 2014-07-09 02:07:42.
 */

namespace Acme;

use Acme\Lib\Bar;
use Acme\Lib\Traits\BarTrait;
use Acme\Lib\Traits\FooTrait;

/**
 * Some class.
 *
 * Some more info on the class.
 * Some more lines.
 *
 * @class Foo
 * @see Acme\Lib\Bar
 * @author Thomas Appel <[email protected]>
 */
class Foo extends Bar
{
    use FooTrait,
        BarTrait {
        FooTrait::getFoo as private getFooStr;
        BarTrait::getBar insteadof FooTrait;
    }

    /** @var int */
    const T_ASW = 42;

    /** @var string */
    private $foo;

    /**
     * Constructor.
     *
     * @param string $foo
     */
    public function __construct($foo = null)
    {
        $this->foo = $foo;
    }
}



// $cl beeing the object writer instance.

$body = 'return new '.$cl->getImportResolver()->getAlias('Acme\MyClass').';';
$method->setBody($body);