lucid / writer
Requires
- php: >=5.6
- lucid/common: dev-master
Requires (Dev)
- phpunit/phpunit: ^5.2
This package is not auto-updated.
Last update: 2024-09-25 23:08:58 UTC
README
安装
> composer require lucid/writer
Writer
导出字符串
写入两行文本块
<?php use Lucid\Writer\Writer; $writer = new Writer; $writer ->writeln('foo') ->writeln('bar'); echo $writer->dump(); //"foo\nbar"
行为
默认情况下,Writer 将删除每行末尾的空格。
您可以通过调用 allowTrailingSpace() 方法来覆盖此行为。
<?php use Lucid\Writer\Writer; $writer = new Writer; $writer->allowTrailingSpace(true); // will now preserve trailing space chars for each line.
缩进
默认缩进级别为 4 个空格。
如果您需要使用空格进行不同级别的缩进,您必须在构造函数中指定此级别。
<?php use Lucid\Writer\Writer; $writer = new Writer(2); $writer ->writeln('foo') ->indent() ->writeln('bar'); echo $writer->dump(); //"foo\n bar"
您也可以使用 useTabs() 方法将空格更改为制表符。
<?php use Lucid\Writer\Writer; $writer = new Writer; $writer->useTabs(true); // …
输出缩进
输出缩进将整个块缩进,并在字符串被导出之前应用。传递给 setOutputIndentation(int $level) 的值作为乘数。
API
流畅方法
Lucid\Writer\Writerwriteln(string|null $str)
添加一行。
Lucid\Writer\Writerindent(void)
添加缩进。
Lucid\Writer\Writerreplaceln( string $str, int $index)
替换指定索引的一行。
Lucid\Writer\Writerremoveln(int $index)
删除指定索引的一行。
Lucid\Writer\Writerpopln (void)
删除最后一行。
Lucid\Writer\Writerappendln (string $str)
将字符串追加到最后一行。
非流畅方法
-
voidignoreNull(bool $ignore) 如果Writer::writeln()中的$str为null,则不添加行。默认开启。 -
voidallowTrailingSpace(bool $space) 允许/禁止尾随空格字符。默认关闭。 -
voiduseTabs(void) 使用制表符进行缩进而不是空格。 -
voidsetOutputIndentation(int $level) 设置整个文本块的输出缩进级别。级别值增加一个缩进,例如0表示没有额外缩进,1表示一个缩进,等等。默认为0。 -
intgetOutputIndentation(void) 获取输出缩进级别。(见Writer::setOutputIndentation());
类、接口和特质编写器
导出符合 PSR-2 的 PHP 源代码。
有三个对象生成器,分别是 InterfaceWriter、ClassWriter 和 TraitWriter。所有对象生成器共享一个公共 API。
共享 API
- setParent(
string $parent)
这是一个一次性操作。一旦设置了父类,就不能更改它。$parent 名称必须是父接口或类的全称。
- addUseStatement(
string $use)
将使用语句添加到 PHP 文档中。命名冲突将自动解决,但是您可以声明导入来设置自己的别名,例如 \Acme\Foo as FooAlias。默认情况下,Acme\Lib\Foo 将成为 LibFoo 或 AcmeLibFoo 或 AcmeLibFooAlias 等。请注意,使用语句被视为全称。
- getImportResolver( )
将返回一个 Lucid\Writer\Object\ImportResolver 实例。如果您需要知道导入字符串(接口、特质、父类或使用语句)的别名,这很有用。
<?php $alias = $cg->getImportResolver()->getAlias('Acme\MyClass') // e.g. AcmeMyClassAlias;
voidaddConstant(Lucid\Writer\Object\Constant $constant)
将常量添加到接口。
voidaddMethod(Lucid\Writer\Object\MethodInterface $method)
接收一个类型为 Lucid\Writer\Object\MethodInterface 的对象并将其添加到对象声明中。
Lucid\Writer\Object\DocBlockgetDoc(void)
返回表示文档级别docblock的 Lucid\Writer\Object\DocBlock 实例。
Lucid\Writer\Object\DocBlockgetObjDoc(void)
返回表示对象级别docblock的 Lucid\Writer\Object\DocBlock 实例。
voidnoAutoGenerateTag( void )
默认情况下,对象写入器将时间戳添加到文档级别docblock。如果您想禁用此行为,请使用此方法。
InterfaceWriter
用于自动生成PHP接口。
<?php use Lucid\Writer\Object\ClassWriter; $iw = new InterfaceWriter('Foo', 'Acme', '\Acme\Parent'); file_put_contents('Acme/Foo.php', $iw->generate());
结果为
<?php /* * This file was generated at 2014-07-08 12:23:22. */ namespace Acme; /** * @interface Foo * @see Acme\Parent */ interface Foo extends Parent { }
API
- addMethod(
Lucid\Writer\Object\MethodInterface $method)
接收一个 Lucid\Writer\Object\InterfaceMethod 类型的对象,并将其添加到接口中。
ClassWriter
用于生成PHP类。
<?php use Lucid\Writer\Object\ClassWriter; $cg = new ClassWriter('Foo', 'Acme'); file_put_contents('Acme/Foo.php', $cg->generate());
结果为
<?php /* * This file was generated at 2014-07-08 12:23:22. */ namespace Acme; /** * @class Foo */ class Foo { }
API
除了 InterfaceWriter 之外
-
voidaddTrait(string $trait) 接收一个特质的完全限定名称,并将其添加为特质。特质将自动添加到 use 语句列表中,除非它们属于与类完全相同的命名空间。 -
voidaddInterface(string $interface) 添加一个接口。它将自动添加到类导入中。 -
voidsetAbstract(boolean $abstract) 切换此类的抽象状态。 -
voidaddMethod(MethodInterface $method) 接收一个Method类型的对象,并将其添加到类中。 -
voidsetProperties(array $properties) 设置类属性。$properties必须是一个包含Lucid\Writer\Object\Property实例的数组。 -
voidaddProperty(Lucid\Writer\Object\Property $property) 接收一个Lucid\Writer\Object\Property类型的对象,并将其添加为类属性。 -
voiduseTraitMethodAs(string $trait,string $method,string $replacement,[string $visibility]) 替换特质和类之间方法命名的冲突。默认可见性为public。 -
voidreplaceTraitConflict(string $trait,string $conflict,string $method) 替换两个特质之间方法的冲突。
示例
生成包含常量、方法、属性和特质的类。
<?php 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 <mail@thomas-appel.com>'); echo $cg->generate();
结果为
<?php /* * 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 <mail@thomas-appel.com> */ 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; } }
TraitWriter
类似于 ClassWriter,但没有任何常量和接口。
注意
设置方法体由您自己决定。然而,如果您依赖于已导入的类基名称,则可以使用导入解析器来确定对象写入器实际使用的简短名称。
另请参阅共享API部分。
<?php // $cl beeing the object writer instance. $body = 'return new '.$cl->getImportResolver()->getAlias('Acme\MyClass').';'; $method->setBody($body);