\phpDocumentor\Plugin\Twig\WriterTwig

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 class entry.

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.

Summary

Methods
Properties
Constants
checkRequirements()
transform()
setRouters()
setTranslator()
No public properties found
No constants found
checkForSpacesInPath()
initializeEnvironment()
addPhpDocumentorExtension()
addExtensionsFromTemplateConfiguration()
getDestinationPath()
getTemplatePath()
$routers
$translator
N/A
No private methods found
No private properties found
N/A

Properties

Methods

checkRequirements()

checkRequirements() : void

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

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.

transform()

transform(\phpDocumentor\Descriptor\ProjectDescriptor  $project, \phpDocumentor\Transformer\Transformation  $transformation) : void

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

Parameters

\phpDocumentor\Descriptor\ProjectDescriptor $project

Document containing the structure.

\phpDocumentor\Transformer\Transformation $transformation

Transformation to execute.

setRouters()

setRouters(\phpDocumentor\Transformer\Router\Queue  $routers) : void

Sets the routers that can be used to determine the path of links.

Parameters

\phpDocumentor\Transformer\Router\Queue $routers

checkForSpacesInPath()

checkForSpacesInPath(string  $path) : void

Checks if there is a space in the path.

Parameters

string $path

Throws

\InvalidArgumentException

if path contains a space.

initializeEnvironment()

initializeEnvironment(\phpDocumentor\Descriptor\ProjectDescriptor  $project, \phpDocumentor\Transformer\Transformation  $transformation, string  $destination) : \Twig_Environment

Initializes the Twig environment with the template, base extension and additionally defined extensions.

Parameters

\phpDocumentor\Descriptor\ProjectDescriptor $project
\phpDocumentor\Transformer\Transformation $transformation
string $destination

Returns

\Twig_Environment

addPhpDocumentorExtension()

addPhpDocumentorExtension(\phpDocumentor\Descriptor\ProjectDescriptor  $project, \phpDocumentor\Transformer\Transformation  $transformation, string  $destination, \Twig_Environment  $twigEnvironment) : void

Adds the phpDocumentor base extension to the Twig Environment.

Parameters

\phpDocumentor\Descriptor\ProjectDescriptor $project
\phpDocumentor\Transformer\Transformation $transformation
string $destination
\Twig_Environment $twigEnvironment

addExtensionsFromTemplateConfiguration()

addExtensionsFromTemplateConfiguration(\phpDocumentor\Transformer\Transformation  $transformation, \phpDocumentor\Descriptor\ProjectDescriptor  $project, \Twig_Environment  $twigEnvironment) : void

Tries to add any custom extensions that have been defined in the template or the transformation's configuration.

This method will read the twig-extension parameter of the transformation (which inherits the template's parameter set) and try to add those extensions to the environment.

Parameters

\phpDocumentor\Transformer\Transformation $transformation
\phpDocumentor\Descriptor\ProjectDescriptor $project
\Twig_Environment $twigEnvironment

Throws

\InvalidArgumentException

if a twig-extension should be loaded but it could not be found.

getDestinationPath()

getDestinationPath(\phpDocumentor\Descriptor\DescriptorAbstract  $node, \phpDocumentor\Transformer\Transformation  $transformation) : string|false

Uses the currently selected node and transformation to assemble the destination path for the file.

The Twig writer accepts the use of a Query to be able to generate output for multiple objects using the same template.

The given node is the result of such a query, or if no query given the selected element, and the transformation contains the destination file.

Since it is important to be able to generate a unique name per element can the user provide a template variable in the name of the file. Such a template variable always resides between double braces and tries to take the node value of a given query string.

Example:

An artifact stating classes/{{name}}.html will try to find the node 'name' as a child of the given $node and use that value instead.

Parameters

\phpDocumentor\Descriptor\DescriptorAbstract $node
\phpDocumentor\Transformer\Transformation $transformation

Throws

\InvalidArgumentException

if no artifact is provided and no routing rule matches.

\UnexpectedValueException

if the provided node does not contain anything.

Returns

string|false —

returns the destination location or false if generation should be aborted.

getTemplatePath()

getTemplatePath(\phpDocumentor\Transformer\Transformation  $transformation) : string

Returns the path belonging to the template.

Parameters

\phpDocumentor\Transformer\Transformation $transformation

Returns

string