You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/dependency-injection.md
+17-7Lines changed: 17 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,24 +44,34 @@ Inflection
44
44
Drush will also inject dependencies that it provides using a technique called inflection. Inflection is a kind of dependency injection that works by way of a set of provided inflection interfaces, one for each available service. Each of these interfaces will define one or more setter methods (usually only one); these will automatically be called by Drush when the commandfile object is instantiated. The command only needs to implement this method and save the provided object in a class field. There is usually a corresponding trait that may be included via a `use` statement to fulfill this requirement.
45
45
46
46
For example:
47
+
47
48
```
48
49
<?php
49
50
namespace Drupal\my_module\Commands;
50
51
51
52
use Drush\Commands\DrushCommands;
53
+
use Consolidation\OutputFormatters\StructuredData\ListDataFromKeys;
52
54
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
53
-
use Consolidation\SiteAlias\SiteSliasManagerAwareTrait;
55
+
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
54
56
55
-
class MyModuleiCommands extends DrushCommands implements SiteAliasManagerAwareInterface
57
+
class MyModuleCommands extends DrushCommands implements SiteAliasManagerAwareInterface
56
58
{
57
59
use SiteAliasManagerAwareTrait;
58
-
public function commandName($)
59
-
{
60
-
$selfAlias = $this->aliasManager()->getSelf();
61
-
$this->logger()->success(‘The current alias is {name}’, [‘name’ => $selfAlias]);
$this->logger()->success(‘The current alias is {name}’, [‘name’ => $selfAlias]);
70
+
return new ListDataFromKeys($aliasRecord->export());
71
+
}
63
72
}
64
73
```
74
+
65
75
All Drush command files extend DrushCommands. DrushCommands implements ConfigAwareInterface, IOAwareInterface, LoggerAwareInterface, which gives access to `$this->getConfig()`, `$this->logger()` and other ways to do input and output. See the [IO documentation](io.md) for more information.
66
76
67
77
Any additional services that are desired must be injected by implementing the appropriate inflection interface.
0 commit comments