A Yeoman generator for PHP with the ability to resolve namespaces from a Composer file.
First make sure you have installed Yeoman. If not, install Yeoman using:
npm install -g yo
Next install the generator using:
npm install -g generator-phab
To see a list of all the available generators use the following:
yo phab
Specific generators can also be called directly. E.g.:
yo phab:class
Available generators are listed below. These will by default ask the least amount of questions; to access advanced options use the --advanced
option.
-
Creates an empty class.
The following command will create the class
Model\MyModel
relative to the working directory:yo phab:class Model/MyModel
-
Creates an empty interface.
The following command will create the interface
Api\MyInterface
relative to the working directory:yo phab:interface Api/MyInterface
-
Creates a callable class using the
__invoke()
function.The following command will create a callable class
Controller\MyController
relative to the working directory:yo phab:callable Controller/MyController
-
Creates an empty document.
The following command will create
document.php
relative to the working directory:yo phab:document
It is possible to open a generated file using the option -o
. This requires the environment variable EDITOR
to point to the default editor. E.g.:
EDITOR=$(which code) yo phab:document -o
If a Composer file is found in any of the parent directories generators will use the autoload configuration from this file to resolve the namespace for the location where the command is called and prepend that to a class or interface.
Default values for author and license will also be read from the Composer file.
These generators are set up to use options which can also be passed in from other generators using this.composeWith()
.
The following example will use the document generator to create a document that prints 'Hello, World!'.
this.composeWith(
require.resolve('generator-phab/generators/document'),
{
contextRoot: this.contextRoot,
document: {
body: "echo 'Hello, World!';"
}
}
);
When composing generators always pass in the context root as it will be reset to the project root by the generator's constructor.
For a complete example of generator composition see the callable generator.
More information about generator composition can be found in the Yeoman docs.