About Features Downloads Getting Started Documentation Events Support GitHub

Site Tools


development:code_generators

Code Generators

Starting with VuFind 2.4, command line code generation tools are available to help save you time when performing certain common tasks.

Creating Routes

Adding new entries to the Zend Framework router can involve tedious array creation, so VuFind includes tools for creating certain kinds of routes automatically from the command line.

Static Routes

The most basic type of route simply specifies a controller and an action, mapping a URL path like “/Controller/Action” to the corresponding code.

Usage

cd $VUFIND_HOME
php public/index.php generate staticroute Controller/Action MyModule

Dynamic Routes

Simple dynamic routes are similar to static routes but allow the addition of dynamic parameters after the action name. This is useful for actions that need to, for example, retrieve specific values by ID (like the existing routes that load user favorite lists).

Usage

cd $VUFIND_HOME
php public/index.php generate dynamicroute routename Controller Action/[:placeholders] MyModule

Record Routes

Record routes are a special complex case which handle the creation of multiple route entries for a record home page, dynamic tab URLs, and a set of standard static routes. These do not commonly need to be generated, but they may be needed when creating a new search backend.

Usage

cd $VUFIND_HOME
php public/index.php generate recordroute routename-base Controller MyModule

Creating Themes

Starting with VuFind 4.1, a generator is available to create an example theme for you to customize. The generator not only creates a new theme directory with an example configuration, CSS and template, but it also updates your local config.ini to turn on the new theme and make it a selectable option.

Usage

cd $VUFIND_HOME
php public/index.php generate theme MyTheme

Creating Theme Mix-ins

Starting with VuFind 4.1, a generator is available to create an example theme mix-in for you to customize. The generator creates a new mix-in directory with an example configuration and script; you will need to manually customize this and add it to your theme.config.php's mixins array in order to activate it.

Usage

cd $VUFIND_HOME
php public/index.php generate thememixin MyMixin

Overriding Existing Plugins and Services (VuFind 4.x and earlier)

If you want to extend one of VuFind's built-in plugins or services, you can use the 'extendservice' generator to create a new subclass and automatically configure your local module to use it.

:!: Starting with VuFind 5.0, the 'extendclass' generator is usually a more convenient option. See below for details. You can still use 'extendservice' in 5.0 if you wish, but due to changes in service manager configuration, it is less useful than before.

Usage

cd $VUFIND_HOME
php public/index.php generate extendservice path/to/service MyModule
  • path/to/service is a slash-separated path showing where the service is defined in one of VuFind's existing module.config.php files.
  • MyModule is the name of your local module where you wish to create a new class.

Notes

:!: Be sure that MyModule already exists and is included in your VUFIND_LOCAL_MODULES environment variable, or the generator may not work correctly. If MyModule does not yet exist, you can re-run VuFind's install.php script to create it.

:!: If things don't work right away after generating code, you may need to clear your local/cache directory to get rid of outdated configurations.

:!: Don't forget to escape backslashes in service names within the path/to/service parameter. For example, if you wanted to override the export tool, your path would be “service_manager/factories/VuFind\\Export”.

Example

Suppose you wish to create a custom subclass of VuFind's SolrMarc record driver. You can see in module/VuFind/config/module.config.php that the plugin is defined like this:

$config = array(
    'vufind' => array(
        ...
        'plugin_managers' => array(
            ...
            'recorddriver' => array(
                ...
                'factories' => array(
                    ...
                    'solrmarc' => 'VuFind\RecordDriver\Factory::getSolrMarc',

The path to this service would be vufind/plugin_managers/recorddriver/factories/solrmarc (following the nested arrays down from the top). So the command to generate a custom extension of the default class is:

cd $VUFIND_HOME
php public/index.php generate extendservice vufind/plugin_managers/recorddriver/factories/solrmarc MyModule

The output should look something like this:

Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.php
Saved file: /.../module/MyModule/src/MyModule/RecordDriver/SolrMarc.php
Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.php
Created backup: /.../module/MyModule/config/module.config.php.1423850855.bak
Successfully updated /.../module/MyModule/config/module.config.php

Overriding Existing Plugins and Services (VuFind 5.0 and later)

If you want to extend one of VuFind's built-in plugins or services, you can use the 'extendclass' generator to create a new subclass and automatically configure your local module to use it.

Usage

cd $VUFIND_HOME
php public/index.php generate extendclass [--extendfactory] Fully\\Qualified\\ClassName MyModule
  • –extendfactory is a switch which will cause the existing factory method to be cloned into your local module. If you omit the switch, the existing factory will be used. :!: Using the existing factory only makes sense for factories that dynamically determine the name of the constructed object using the service name; however, a significant number of VuFind services use this type of factory starting in release 5.0.
  • Fully\\Qualified\\ClassName is the name of the class you wish to extend/override – note the need to escape backslashes on the command line.
  • MyModule is the name of your local module where you wish to create a new class.

Notes

:!: Be sure that MyModule already exists and is included in your VUFIND_LOCAL_MODULES environment variable, or the generator may not work correctly. If MyModule does not yet exist, you can re-run VuFind's install.php script to create it.

:!: If things don't work right away after generating code, you may need to clear your local/cache directory to get rid of outdated configurations.

Example

Suppose you wish to create a custom subclass of VuFind's SolrMarc record driver. The command to generate a custom extension of the default class is:

cd $VUFIND_HOME
php public/index.php generate extendclass --extendfactory VuFind\\RecordDriver\\SolrMarc MyModule

The output should look something like this:

Saved file: /.../module/MyModule/src/MyModule/RecordDriver/SolrMarc.php
Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.php
Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.php
Created backup: /.../module/MyModule/config/module.config.php.1423850855.bak
Successfully updated /.../module/MyModule/config/module.config.php
Successfully updated /.../module/MyModule/config/module.config.php

Creating New Plugins

:!: This feature was introduced in VuFind 5.1.

If you want to create a new plugin for VuFind, you can use the 'plugin' generator to create a stub class and (if necessary) factory and automatically update the relevant module configuration.

Usage

cd $VUFIND_HOME
php public/index.php generate plugin MyModule\\PluginType\\PluginName [optional factory name]
  • MyModule\\PluginType\\PluginName is the name of the class you wish to create. The generator will use the first part of the class name to determine which module it belongs in (since modules correspond to namespaces); it will use the last part of the class name to name the class being defined; it will use the middle part to identify what type of plugin is being created and to set up configuration accordingly. Obviously, if you specify a class name that does not correspond with a standard VuFind plugin type, the generator will fail.
  • [optional factory name] may be included to specify the name of an existing factory that should be used to construct your plugin. For example, if your class will not have any dependencies, you can use the simple Zend\\ServiceManager\\Factory\\InvokableFactory to construct the object in a simple fashion. If you do not specify a factory name, the generator will create a factory automatically, naming it by taking your class name and appending “Factory” on the end.

Notes

:!: Be sure that MyModule already exists and is included in your VUFIND_LOCAL_MODULES environment variable, or the generator may not work correctly. If MyModule does not yet exist, you can re-run VuFind's install.php script to create it.

:!: If things don't work right away after generating code, you may need to clear your local/cache directory to get rid of outdated configurations.

Example

Suppose you want to create a new ILS driver for your home-grown system. You could begin like this:

cd $VUFIND_HOME
php public/index.php generate plugin MyModule\\ILS\\Driver\\MyLocalSystem

The output should look something like this:

Saved file: /home/dkatz/vufind3/module/MyModule/src/MyModule/ILS/Driver/MyLocalSystem.php
Saved file: /home/dkatz/vufind3/module/MyModule/src/MyModule/ILS/Driver/MyLocalSystemFactory.php
Created backup: /home/dkatz/vufind3/module/MyModule/config/module.config.php.1540394754.bak
Successfully updated /home/dkatz/vufind3/module/MyModule/config/module.config.php
Successfully updated /home/dkatz/vufind3/module/MyModule/config/module.config.php
development/code_generators.txt · Last modified: 2018/10/24 11:27 by demiankatz