Download the PHP package lucid/writer without Composer
On this page you can find all versions of the php package lucid/writer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package writer
Dumping strings with elegance
Installation
The Writer
Dumping strings
Write a 2 line text block:
Behavior
By defatault, the Writer will remove trailing spaces at the end of each line.
You may override this behavior by calling the allowTrailingSpace()
method.
Indentation
The default indentation level is 4 spaces.
If you require a different level using spaces, you'll have to specify this on the. constructor:
You may also change spaces to tabs using the useTabs() method.
Output indentation
Output indentation indents the whole block and is applied just before the
string is being dumped. The value passed to setOutputIndentation(int $level)
acts as a multiplyer.
API
Fluent methods:
Lucid\Writer\Writerwriteln(string|null $str)
Adds a line.
Lucid\Writer\Writerindent(void)
Adds an indentation.
Lucid\Writer\Writerreplaceln( string $str, int $index)
Replaces a line at a line index.
Lucid\Writer\Writerremoveln(int $index)
Removes a line at a line index.
Lucid\Writer\Writerpopln (void)
Removes the last line.
Lucid\Writer\Writerappendln (string $str)
Appends a string to the last line.
None fluent methods:
-
voidignoreNull(bool $ignore) Don't add a line if$strinWriter::writeln()isnull. Default is on. -
voidallowTrailingSpace(bool $space) Allow/Disallow traling space chars. Default is off. -
voiduseTabs(void) Use Tabs for indentation instead of spaces. -
voidsetOutputIndentation(int $level) Sets the output indentation level of the whole text block. The level value increments the indentation by one indent, e.g.0is no additional indentation,1is one indent, etc. Default is0. intgetOutputIndentation(void) Gets the output indentation level. (seeWriter::setOutputIndentation());
Class, Interface, and Trait Writers
Dump PSR-2 compliant php source code.
There're three object generators, InterfaceWriter, ClassWriter, and TraitWriter.
All object generators share a common API.
Shared API
- setParent(
string $parent)
This is a one time operation. Once the parent is set, you cannot change it.
$parent name must be the FQN of the parent interface or class.
- addUseStatement(
string $use)
Adds a use statement to the php document. Naming conflicts will automatically
be resolved, however you can set your own alias by declating the import like
this \Acme\Foo as FooAlias. By default Acme\Lib\Foo will become LibFoo,
or AcmeLibFoo, or AcmeLibFooAlias, and so on.
Note that the use statement is considered to be the FQN;
- getImportResolver( )
Will return an instance of Lucid\Writer\Object\ImportResolver.
This is useful if you need to know the aliases name of a imported string
(interface, trait, parent class or usestatement), e.g.
voidaddConstant(Lucid\Writer\Object\Constant $constant)
Adds a constant to the interface.
voidaddMethod(Lucid\Writer\Object\MethodInterface $method)
Takes an object of type Lucid\Writer\Object\MethodInterface and adds it to
the object declaration.
Lucid\Writer\Object\DocBlockgetDoc(void)
Returns an instance of Lucid\Writer\Object\DocBlock that represents the
document level docblock.
Lucid\Writer\Object\DocBlockgetObjDoc(void)
Returns an instance of Lucid\Writer\Object\DocBlock that represents the
object level docblock.
voidnoAutoGenerateTag( void )
By default, the objectwriter will add a timestamp to the document level docblock. Use this if you wan't to deactivate this behavior.
InterfaceWriter
Use this for autogenerating php interfaces.
Results in:
API
- addMethod(
Lucid\Writer\Object\MethodInterface $method)
Takes an object of type Lucid\Writer\Object\InterfaceMethod and adds it to
the interface.
ClassWriter
Use this to generate php classes.
Results in:
API
In addition to the InterfaceWriter:
-
voidaddTrait(string $trait) Takes a FQN of a trait and adds it as a trait. Traits will be automatically added to the use statements list, except they're belong to exact same namespace of the class. -
voidaddInterface(string $interface) Adds an interface. Will be automatically added to the class imports. -
voidsetAbstract(boolean $abstract) Toggle this class abstract. -
voidaddMethod(MethodInterface $method) Takes an object of typeMethodand adds it to the class. -
voidsetProperties(array $properties) Set the class properties.$propertiesmust be an array ofLucid\Writer\Object\Propertyinstances. -
voidaddProperty(Lucid\Writer\Object\Property $property) Takes an object of typeLucid\Writer\Object\Propertyand adds it as a class property. -
voiduseTraitMethodAs(string $trait,string $method,string $replacement,[string $visibility]) Replaces a method naming conflict between a trait an a class. Default visiblity ispublic. voidreplaceTraitConflict(string $trait,string $conflict,string $method) Replaces a method conflict between two traits.
Example
Generating a class with constants, methods, properties, and traits.
Results in
TraitWriter
Behaves like the ClassWriter except there're no constants and interfaces.
Notes
Setting method bodies is up to you. However, if you rely on class base names that have been imported you can utilize the import resolver to determine the actual shortname that's used on the object writer.
Also see the Shared API section.