Skip to content

Commit 2464cec

Browse files
Esolitos Marlongreg-1-anderson
authored andcommitted
Fix documentation example for DependencyInjection. Ref #3950 (#3967)
1 parent a9a165c commit 2464cec

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

docs/dependency-injection.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,34 @@ Inflection
4444
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.
4545

4646
For example:
47+
4748
```
4849
<?php
4950
namespace Drupal\my_module\Commands;
5051
5152
use Drush\Commands\DrushCommands;
53+
use Consolidation\OutputFormatters\StructuredData\ListDataFromKeys;
5254
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
53-
use Consolidation\SiteAlias\SiteSliasManagerAwareTrait;
55+
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
5456
55-
class MyModuleiCommands extends DrushCommands implements SiteAliasManagerAwareInterface
57+
class MyModuleCommands extends DrushCommands implements SiteAliasManagerAwareInterface
5658
{
5759
use SiteAliasManagerAwareTrait;
58-
public function commandName($)
59-
{
60-
$selfAlias = $this->aliasManager()->getSelf();
61-
$this->logger()->success(‘The current alias is {name}’, [‘name’ => $selfAlias]);
62-
}
60+
/**
61+
* Prints the currenbt alias name and info.
62+
*
63+
* @command mymodule:myAlias
64+
* @return \Consolidation\OutputFormatters\StructuredData\ListDataFromKeys
65+
*/
66+
public function myAlias()
67+
{
68+
$selfAlias = $this->siteAliasManager()->getSelf();
69+
$this->logger()->success(‘The current alias is {name}’, [‘name’ => $selfAlias]);
70+
return new ListDataFromKeys($aliasRecord->export());
71+
}
6372
}
6473
```
74+
6575
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.
6676

6777
Any additional services that are desired must be injected by implementing the appropriate inflection interface.

0 commit comments

Comments
 (0)