c40560887b2e0d6edd3aebfcff0ee6e3f04ef4e1
[yaffs-website] / vendor / drush / drush / docs / commands.md
1 Creating Custom Drush Commands
2 ==============================
3
4 Creating a new Drush command or porting a legacy command is easy. Follow the steps below.
5
6 1. Run `drush generate drush-command-file`.
7 1. Drush will prompt for the machine name of the module that should "own" the file.
8     1. (optional) Drush will also prompt for the path to a legacy command file to port. See [tips on porting command to Drush 9](https://weitzman.github.io/blog/port-to-drush9)
9     1. The module selected must already exist and be enabled. Use `drush generate module-standard` to create a new module.
10 1. Drush will then report that it created a commandfile, a drush.services.yml file and a composer.json file. Edit those files as needed.
11 1. Use the classes for the core Drush commands at [/src/Drupal/Commands](https://github.com/drush-ops/drush/tree/master/src/Drupal/Commands) as inspiration and documentation.
12 1. See the [dependency injection docs](dependency-injection.md) for interfaces you can implement to gain access to Drush config, Drupal site aliases, etc.
13 1. Once your two files are ready, run `drush cr` to get your command recognized by the Drupal container.
14
15 Specifying the Services File
16 ================================
17
18 A module's composer.json file stipulates the filename where the Drush services (e.g. the Drush command files) are defined. The default services file is `drush.services.yml`, which is defined in the extra section of the composer.json file as follows:
19 ```
20   "extra": {
21     "drush": {
22       "services": {
23         "drush.services.yml": "^9"
24       }
25     }
26   }
27 ```
28 If for some reason you need to load different services for different versions of Drush, simply define multiple services files in the `services` section. The first one found will be used. For example:
29 ```
30   "extra": {
31     "drush": {
32       "services": {
33         "drush-9-99.services.yml": "^9.99",
34         "drush.services.yml": "^9"
35       }
36     }
37   }
38 ```
39 In this example, the file `drush-9-99.services.yml` loads commandfile classes that require features only available in Drush 9.99 and later, and drush.services.yml loads an older commandfile implementation for earlier versions of Drush.
40
41 It is also possible to use [version ranges](https://getcomposer.org/doc/articles/versions.md#version-range) to exactly specify which version of Drush the services file should be used with (e.g. `"drush.services.yml": ">=9 <9.99"`).
42
43 In Drush 9, the default services file, `drush.services.yml`, will be used in instances where there is no `services` section in the Drush extras of the project's composer.json file. In Drush 10, however, the services section must exist, and must name the services file to be used. If a future Drush extension is written such that it only works with Drush 10 and later, then its entry would read `"drush.services.yml": "^10"`, and Drush 9 would not load the extension's commands. It is all the same recommended that Drush 9 extensions explicitly declare their services file with an appropriate version constraint.
44
45 Altering Drush Command Info
46 ===========================
47
48 Drush command info (annotations) can be altered from other modules. This is done by creating and registering 'command info alterers'. Alterers are class services that are able to intercept and manipulate an existing command annotation.
49
50 In order to alter an existing command info, follow the steps below:
51
52 1. In the module that wants to alter a command info, add a service class that implements the `\Consolidation\AnnotatedCommand\CommandInfoAltererInterface`.
53 1. In the module `drush.services.yml` declare a service pointing to this class and tag the service with the `drush.command_info_alterer` tag.
54 1. In that class, implement the alteration logic in the `alterCommandInfo()` method.
55 1. Along with the alter code, it's strongly recommended to log a debug message explaining what exactly was altered. This makes things easier on others who may need to debug the interaction of the alter code with other modules. Also it's a good practice to inject the the logger in the class constructor.
56
57 For an example, see the alterer class provided by the testing 'woot' module: `tests/resources/modules/d8/woot/src/WootCommandInfoAlterer.php`.
58
59 Site-Wide Drush Commands
60 ==============================
61
62 Commandfiles that are installed in a Drupal site and are not bundled inside a Drupal module are called 'site-wide' commandfiles. Site-wide commands may either be added directly to the Drupal site's repository (e.g. for site-specific policy files), or via `composer require`. See the [examples/Commands](/examples/Commands) folder for examples. In general, it's better to use modules to carry your Drush commands, as module-based commands may [participate in Drupal's dependency injection via the drush.services.yml](#specifying-the-services-file). 
63
64 If you still prefer using site-wide commandfiles, here are some examples of valid commandfile names and namespaces:
65
66 1. Simple
67      - Filename: $PROJECT_ROOT/drush/Commands/ExampleCommands.php
68      - Namespace: Drush\Commands
69 1. Nested in a subdirectory committed to the site's repository
70      - Filename: $PROJECT_ROOT/drush/Commands/example/ExampleCommands.php
71      - Namespace: Drush\Commands\example
72 1. Nested in a subdirectory installed via a Composer package
73     - Filename: $PROJECT_ROOT/drush/Commands/contrib/dev_modules/ExampleCommands.php
74     - Namespace: Drush\Commands\dev_modules
75
76 Installing commands as part of a Composer project requires that the project's type be `drupal-drush`, and that the `installer-paths` in the Drupal site's composer.json file contains `"drush/Commands/contrib/{$name}": ["type:drupal-drush"]`. It is also possible to commit projects with a similar layout using a directory named `custom` in place of `contrib`; if this is done, then the directory `custom` will not be considered to be part of the commandfile's namespace.
77
78 If a site-wide commandfile is added via a Composer package, then it may declare any dependencies that it may need in its composer.json file. Site-wide commandfiles that are committed directly to a site's repository only have access to the dependencies already available in the site. Site-wide commandfiles should declare their Drush version compatibility via a `conflict` directive. For example, a Composer-managed site-wide command that works with both Drush 8 and Drush 9 might contain something similar to the following in its composer.json file:
79 ```
80     "conflict": {
81         "drush/drush": "<8.1 || >=9.0 <9.5 || >=10.0",
82     }
83 ```
84 Using `require` in place of `conflict` is not recommended.
85
86 Global Drush Commands
87 ==============================
88
89 Commandfiles that are not part of any Drupal site are called 'global' commandfiles. Global commandfiles are not supported by default; in order to enable them, you must configure your `drush.yml` configuration file to add an `include` search location.
90
91 For example:
92
93 drush:
94   paths:
95     include:
96       - '${env.home}/.drush/commands'
97       
98 With this configuration in place, global commands may be placed as described in the Site-Wide Drush Commands section above. Global commandfiles may not declare any dependencies of their own; they may only use those dependencies already available via the autoloader.
99
100 ##### Tips
101 1. The filename must be have a name like Commands/ExampleCommands.php
102     1. The prefix `Example` can be whatever string you want.
103     1. The file must end in `Commands.php`
104 1. The directory above `Commands` must be one of: 
105     1.  A Folder listed in the 'include' option. Include may be provided via [config](#global-drush-commands) or via CLI.
106     1.  ../drush, /drush or /sites/all/drush. These paths are relative to Drupal root.