Twig

in package
extends WriterAbstract Uses IoTrait

A specialized writer which uses the Twig templating engine to convert templates to HTML output.

This writer support the Query attribute of a Transformation to generate multiple templates in one transformation.

The Query attribute supports a simplified version of Twig queries and will use each individual result as the 'node' global variable in the Twig template.

Example:

Suppose a Query indexes.classes is given then this writer will be invoked as many times as there are classes in the project and the 'node' global variable in twig will be filled with each individual

When using the Query attribute in the transformation it is important to use a variable in the Artifact attribute as well (otherwise the same file would be overwritten several times).

A simple example transformation line could be:

<transformation
    writer="twig"
    source="templates/twig/index.twig"
    artifact="index.html"
/>

This example transformation would use this writer to transform the index.twig template file in the twig template folder into index.html at the destination location. Since no Query is provided the 'node' global variable will contain the Project Descriptor of the Object Graph.

A complex example transformation line could be:

<transformation
    query="indexes.classes"
    writer="twig"
    source="templates/twig/class.twig"
    artifact="{{name}}.html"/>

This example transformation would use this writer to transform the class.twig template file in the twig template folder into a file with the 'name' property for an individual class inside the Object Graph. Since a Query is provided will the 'node' global variable contain a specific instance of a class applicable to the current iteration.

Interfaces, Classes & Used Traits

IoTrait
see
self::getDestinationPath()

for more information about variables in the Artifact attribute.

Properties

private$environmentFactory  : \phpDocumentor\Transformer\Writer\Twig\EnvironmentFactory                

private$pathGenerator  : \phpDocumentor\Transformer\Writer\PathGenerator                

Methods

This method verifies whether PHP has all requirements needed to run this writer.

publiccheckRequirements() : void

If one of the requirements is missing for this Writer then an exception of type RequirementMissing should be thrown; this indicates to the calling process that this writer will not function.

Throws

\phpDocumentor\Transformer\Writer\Exception\RequirementMissing

When a requirements is missing stating which one.

Inherited from
\phpDocumentor\Transformer\Writer\WriterAbstract
codeCoverageIgnore

This method combines the ProjectDescriptor and the given target template and creates a static html page at the artifact location.

publictransform(ProjectDescriptor $project, Transformation $transformation) : void

Parameters

$project : ProjectDescriptor

Document containing the structure.

$transformation : Transformation

Transformation to execute.

Throws

\Twig\Error\LoaderError
\Twig\Error\RuntimeError
\Twig\Error\SyntaxError

public__toString() : string

Returns

string

public__construct(EnvironmentFactory $environmentFactory, PathGenerator $pathGenerator) : mixed

Parameters

$environmentFactory : EnvironmentFactory
$pathGenerator : PathGenerator

Returns

mixed

protectedcopy(Transformation $transformation, string $path, string $destination) : void

Parameters

$transformation : Transformation
$path : string
$destination : string

protectedreadSourceFile(Transformation $transformation, string $path) : string

Parameters

$transformation : Transformation
$path : string

Returns

string

protectedpersistTo(Transformation $transformation, string $path, string $contents) : void

Parameters

$transformation : Transformation
$path : string
$contents : string

privatecopyDirectory(Transformation $transformation, string $path, string $destination) : void

Parameters

$transformation : Transformation
$path : string
$destination : string

privatestripFirstPartOfPath(string $path) : string

Parameters

$path : string

Returns

string

privateisGlobalTemplateReference(string $path) : bool

Parameters

$path : string

Returns

bool

privatenormalizeSourcePath(string $path) : string

Parameters

$path : string

Returns

string

privatenormalizeDestination(string $destination) : string

Parameters

$destination : string

Returns

string

Returns the path belonging to the template.

privategetTemplatePath(Transformation $transformation) : string

Parameters

$transformation : Transformation

Returns

string