About Features Downloads Getting Started Documentation Events Support GitHub

Love VuFind®? Consider becoming a financial supporter. Your support helps build a better VuFind®!

Site Tools


Warning: This page has not been updated in over over a year and may be outdated or deprecated.
development:code_generators

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:code_generators [2023/11/09 21:14] demiankatzdevelopment:code_generators [2023/11/09 21:19] (current) – [extendservice] demiankatz
Line 1: Line 1:
 ====== 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.+Command line code generation tools are available to help save you time when performing certain common tasks.
  
 ===== Creating Routes ===== ===== Creating Routes =====
Line 59: Line 59:
 </code> </code>
  
-===== Overriding Existing Plugins and Services (VuFind® 4.x and earlier) =====+===== Overriding Existing Plugins and Services =====
  
-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.+==== extendclass ====
  
-:!: Starting with VuFind® 5.0, the 'extendclass' generator is usually 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.+If you want to extend one of VuFind®'s built-in plugins or servicesyou can use the 'extendclass' generator to create new subclass and automatically configure your local module to use it.
  
-==== Usage ====+=== Usage ===
  
 <code bash> <code bash>
 cd $VUFIND_HOME cd $VUFIND_HOME
-php public/index.php generate extendservice path/to/service MyModule+php public/index.php generate extendclass [--extendfactory] Fully\\Qualified\\ClassName MyModule
 </code> </code>
  
-  * path/to/service is a slash-separated path showing where the service is defined in one of VuFind®'s existing module.config.php files.+  * --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.   * MyModule is the name of your local module where you wish to create a new class.
  
-==== Notes ====+=== 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. :!: 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. :!: 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 ===
  
-==== 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:
- +
-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: +
- +
-<code php> +
-$config = array( +
-    'vufind' => array( +
-        ... +
-        'plugin_managers' => array( +
-            ... +
-            'recorddriver' => array( +
-                ... +
-                'factories' => array( +
-                    ... +
-                    'solrmarc' => 'VuFind\RecordDriver\Factory::getSolrMarc', +
-</code> +
- +
-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:+
  
 <code bash> <code bash>
 cd $VUFIND_HOME cd $VUFIND_HOME
-php public/index.php generate extendservice vufind/plugin_managers/recorddriver/factories/solrmarc MyModule+php public/index.php generate extendclass --extendfactory VuFind\\RecordDriver\\SolrMarc MyModule
 </code> </code>
  
Line 109: Line 93:
  
 <code> <code>
-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/SolrMarc.php
 +Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.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 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 Successfully updated /.../module/MyModule/config/module.config.php
 </code> </code>
  
-===== Overriding Existing Plugins and Services (VuFind® 5.0 and later) =====+==== extendservice ====
  
-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.+You can also use the 'extendservice' generator to create a new subclass and automatically configure your local module to use it. It is recommended that you use 'extendclass' instead, because 'extendservice' is a legacy tool that became less useful when 'extendclass' was introduced in release 5.0. However, 'extendservice' remains available as an alternative approach.
  
-==== Usage ====+=== Usage ===
  
 <code bash> <code bash>
 cd $VUFIND_HOME cd $VUFIND_HOME
-php public/index.php generate extendclass [--extendfactory] Fully\\Qualified\\ClassName MyModule+php public/index.php generate extendservice path/to/service MyModule
 </code> </code>
  
-  * --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. +  * path/to/service is a slash-separated path showing where the service is defined in one of VuFind®'s existing module.config.php files.
-  * 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.   * MyModule is the name of your local module where you wish to create a new class.
  
-==== Notes ====+=== 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. :!: 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. :!: 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 ====+:!: 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".
  
-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:+=== 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: 
 + 
 +<code php> 
 +$config = array( 
 +    'vufind' => array( 
 +        ... 
 +        'plugin_managers' => array( 
 +            ... 
 +            'recorddriver' => array( 
 +                ... 
 +                'factories' => array( 
 +                    ... 
 +                    'solrmarc' => 'VuFind\RecordDriver\Factory::getSolrMarc', 
 +</code> 
 + 
 +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:
  
 <code bash> <code bash>
 cd $VUFIND_HOME cd $VUFIND_HOME
-php public/index.php generate extendclass --extendfactory VuFind\\RecordDriver\\SolrMarc MyModule+php public/index.php generate extendservice vufind/plugin_managers/recorddriver/factories/solrmarc MyModule
 </code> </code>
  
Line 148: Line 149:
  
 <code> <code>
 +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/SolrMarc.php
-Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.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 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 Successfully updated /.../module/MyModule/config/module.config.php
 </code> </code>
development/code_generators.1699564483.txt.gz · Last modified: 2023/11/09 21:14 by demiankatz