Skip to content

Commit

Permalink
Deployed f425f35 to 13.x with MkDocs 1.4.3 and mike 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 22, 2024
1 parent e181965 commit 72df5f5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 69 deletions.
69 changes: 6 additions & 63 deletions 13.x/dependency-injection/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -704,13 +704,6 @@
</label>
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>

<li class="md-nav__item">
<a href="#types-of-injection" class="md-nav__link">
Types of Injection
</a>

</li>

<li class="md-nav__item">
<a href="#create-method" class="md-nav__link">
create() method
Expand Down Expand Up @@ -5267,26 +5260,11 @@

<h1 id="dependency-injection">Dependency Injection<a class="headerlink" href="#dependency-injection" title="Permanent link">&para;</a></h1>
<p>Drush command files obtain references to the resources they need through a technique called <em>dependency injection</em>. When using this programing paradigm, a class by convention will never use the <code>new</code> operator to instantiate dependencies. Instead, it will store the other objects it needs in class variables, and provide a way for other code to assign an object to that variable.</p>
<h2 id="types-of-injection">Types of Injection<a class="headerlink" href="#types-of-injection" title="Permanent link">&para;</a></h2>
<p>There are two ways that a class can receive its dependencies. One is called “constructor injection”, and the other is called “setter injection”.</p>
<p><em>Example of constructor injection:</em>
<div class="highlight"><pre><span></span><code> <span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">(</span><span class="nx">DependencyType</span> <span class="nv">$service</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">service</span> <span class="o">=</span> <span class="nv">$service</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></p>
<p><em>Example of setter injection:</em>
<div class="highlight"><pre><span></span><code> <span class="k">public</span> <span class="k">function</span> <span class="nf">setService</span><span class="p">(</span><span class="nx">DependencyType</span> <span class="nv">$service</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">service</span> <span class="o">=</span> <span class="nv">$service</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
The code that is responsible for providing the dependencies a class needs is usually an object called the dependency injection container.</p>
<h2 id="create-method">create() method<a class="headerlink" href="#create-method" title="Permanent link">&para;</a></h2>
<p><span class="twemoji"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M7.75 6.5a1.25 1.25 0 1 0 0 2.5 1.25 1.25 0 0 0 0-2.5Z"/><path d="M2.5 1h8.44a1.5 1.5 0 0 1 1.06.44l10.25 10.25a1.5 1.5 0 0 1 0 2.12l-8.44 8.44a1.5 1.5 0 0 1-2.12 0L1.44 12A1.497 1.497 0 0 1 1 10.94V2.5A1.5 1.5 0 0 1 2.5 1Zm0 1.5v8.44l10.25 10.25 8.44-8.44L10.94 2.5Z"/></svg></span> 11.6+</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>Drush 11 and prior required <a href="https://www.drush.org/11.x/dependency-injection/#services-files">dependency injection via a drush.services.yml file</a>. This approach is deprecated in Drush 12 and will be removed in Drush 13.</p>
<p>Drush 11 and prior required <a href="https://www.drush.org/11.x/dependency-injection/#services-files">dependency injection via a drush.services.yml file</a>. This approach is deprecated in Drush 12+ and will be removed in Drush 13.</p>
</div>
<p>Drush command files can inject services by adding a create() method to the commandfile. See <a href="../commands/">creating commands</a> for instructions on how to use the Drupal Code Generator to create a simple command file starter. A create() method and a constructor will look something like this:
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">WootStaticFactoryCommands</span> <span class="k">extends</span> <span class="nx">DrushCommands</span>
Expand Down Expand Up @@ -5318,53 +5296,18 @@ <h2 id="createearly-method">createEarly() method<a class="headerlink" href="#cre
mechanism is only usable by PSR-4 discovered commands packaged with Composer
projects that are <em>not</em> Drupal modules.</p>
<h2 id="inflection">Inflection<a class="headerlink" href="#inflection" title="Permanent link">&para;</a></h2>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>Inflection is deprecated in Drush 12; use <code>create()</code> or <code>createEarly()</code> instead.
Some classes are no longer available for inflection in Drush 12, and more (or potentially all)
may be removed in Drush 13.</p>
</div>
<p>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 <code>use</code> statement to fulfill this requirement.</p>
<p>For example:</p>
<div class="highlight"><pre><span></span><code><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Drupal\my_module\Commands</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Drush\Commands\DrushCommands</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\OutputFormatters\StructuredData\ListDataFromKeys</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\SiteAlias\SiteAliasManagerAwareInterface</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\SiteAlias\SiteAliasManagerAwareTrait</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">MyModuleCommands</span> <span class="k">extends</span> <span class="nx">DrushCommands</span> <span class="k">implements</span> <span class="nx">SiteAliasManagerAwareInterface</span>
<span class="p">{</span>
<span class="k">use</span> <span class="nx">SiteAliasManagerAwareTrait</span><span class="p">;</span>

<span class="sd">/**</span>
<span class="sd"> * Prints the current alias name and info.</span>
<span class="sd"> */</span>
<span class="p">#[</span><span class="nd">CLI\Command</span><span class="p">(</span><span class="nx">name</span><span class="o">:</span> <span class="s1">&#39;mymodule:myAlias&#39;</span><span class="p">)]</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">myAlias</span><span class="p">()</span><span class="o">:</span> <span class="nx">ListDataFromKeys</span>
<span class="p">{</span>
<span class="nv">$selfAlias</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">siteAliasManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getSelf</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">logger</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">success</span><span class="p">(</span><span class="nx">‘The</span> <span class="nb">current</span> <span class="nx">alias</span> <span class="nx">is</span> <span class="p">{</span><span class="nx">name</span><span class="p">}</span><span class="nx"></span><span class="p">,</span> <span class="p">[</span><span class="nx">‘name’</span> <span class="o">=&gt;</span> <span class="nv">$selfAlias</span><span class="p">]);</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">ListDataFromKeys</span><span class="p">(</span><span class="nv">$aliasRecord</span><span class="o">-&gt;</span><span class="na">export</span><span class="p">());</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>All Drush command files extend DrushCommands. DrushCommands implements ConfigAwareInterface, IOAwareInterface, LoggerAwareInterface, which gives access to <code>$this-&gt;getConfig()</code>, <code>$this-&gt;logger()</code> and other ways to do input and output. See the <a href="../io/">IO documentation</a> for more information.</p>
<p>Any additional services that are desired must be injected by implementing the appropriate inflection interface.</p>
<p>Additional Interfaces:</p>
<p>A command class may implement the following interfaces. When doing so, implement the corresponding trait to satisfy the interface.</p>
<ul>
<li>SiteAliasManagerAwareInterface: The site alias manager <a href="../site-alias-manager/">allows alias records to be obtained</a>.</li>
<li>CustomEventAwareInterface: Allows command files to <a href="../hooks/">define and fire custom events</a> that other command files can hook.</li>
<li><a href="https://github.com/consolidation/annotated-command/blob/4.x/src/Events/CustomEventAwareInterface.php">CustomEventAwareInterface</a>: Allows command files to <a href="../hooks/">define and fire custom events</a> that other command files can hook. Example: <a href="https://github.com/drush-ops/drush/blob/13.x/src/Commands/core/CacheCommands.php">CacheCommands</a></li>
<li><a href="https://github.com/consolidation/annotated-command/blob/4.x/src/Input/StdinAwareInterface.php">StdinAwareInterface</a>: Read from standard input. This class contains facilities to redirect stdin to instead read from a file, e.g. in response to an option or argument value. Example: <a href="https://github.com/drush-ops/drush/blob/13.x/src/Commands/core/CacheCommands.php">CacheCommands</a></li>
</ul>
<p>Note that although the autoloader and Drush dependency injection container is available and may be injected into your command file if needed, this should be avoided. Favor using services that can be injected from Drupal or Drush. Some of the objects in the container are not part of the Drush public API, and may not maintain compatibility in minor and patch releases.</p>

<hr>
<div class="md-source-file">
<small>

Last update:
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">September 8, 2023</span>
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">February 22, 2024</span>


</small>
Expand All @@ -5379,7 +5322,7 @@ <h2 id="inflection">Inflection<a class="headerlink" href="#inflection" title="Pe

<div class="md-source-date">
<small>
Authors: <span class='git-page-authors git-authors'><a href='mailto:[email protected]'>Esolitos Marlon</a>, <a href='mailto:[email protected]'>Greg Anderson</a>, <a href='mailto:[email protected]'>Mark Gerarts</a>, <a href='mailto:[email protected]'>Moshe Weitzman</a>, <a href='mailto:[email protected]'>Richard B. Porter</a></span>
Authors: <span class='git-page-authors git-authors'><a href='mailto:[email protected]'>Greg Anderson</a>, <a href='mailto:[email protected]'>Moshe Weitzman</a></span>
</small>
</div>

Expand Down
23 changes: 18 additions & 5 deletions 13.x/examples/SiteAliasAlterCommands.php/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5214,25 +5214,38 @@ <h1>SiteAliasAlterCommands.php</h1>

<span class="k">use</span> <span class="nx">Consolidation\AnnotatedCommand\AnnotationData</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\AnnotatedCommand\Hooks\HookManager</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\SiteAlias\SiteAliasManagerAwareInterface</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\SiteAlias\SiteAliasManagerAwareTrait</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Consolidation\SiteAlias\SiteAliasManagerInterface</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Drush\Attributes</span> <span class="k">as</span> <span class="nx">CLI</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">League\Container\Container</span> <span class="k">as</span> <span class="nx">DrushContainer</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Symfony\Component\Console\Input\InputInterface</span><span class="p">;</span>

<span class="sd">/**</span>
<span class="sd"> * Load this example by using the --include option - e.g. `drush --include=/path/to/drush/examples`</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">SiteAliasAlterCommands</span> <span class="k">extends</span> <span class="nx">DrushCommands</span> <span class="k">implements</span> <span class="nx">SiteAliasManagerAwareInterface</span>
<span class="k">class</span> <span class="nc">SiteAliasAlterCommands</span> <span class="k">extends</span> <span class="nx">DrushCommands</span>
<span class="p">{</span>
<span class="k">use</span> <span class="nx">SiteAliasManagerAwareTrait</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">(</span>
<span class="k">private</span> <span class="nx">readonly</span> <span class="nx">SiteAliasManagerInterface</span> <span class="nv">$siteAliasManager</span>
<span class="p">)</span> <span class="p">{</span>
<span class="k">parent</span><span class="o">::</span><span class="na">__construct</span><span class="p">();</span>
<span class="p">}</span>

<span class="k">public</span> <span class="k">static</span> <span class="k">function</span> <span class="nf">createEarly</span><span class="p">(</span><span class="nx">DrushContainer</span> <span class="nv">$drush_container</span><span class="p">)</span><span class="o">:</span> <span class="nx">self</span>
<span class="p">{</span>
<span class="nv">$commandHandler</span> <span class="o">=</span> <span class="k">new</span> <span class="k">static</span><span class="p">(</span>
<span class="nv">$drush_container</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;site.alias.manager&#39;</span><span class="p">),</span>
<span class="p">);</span>

<span class="k">return</span> <span class="nv">$commandHandler</span><span class="p">;</span>
<span class="p">}</span>

<span class="sd">/**</span>
<span class="sd"> * A few example alterations to site aliases.</span>
<span class="sd"> */</span>
<span class="p">#[</span><span class="nd">CLI\Hook</span><span class="p">(</span><span class="nx">type</span><span class="o">:</span> <span class="nx">HookManager</span><span class="o">::</span><span class="na">PRE_INITIALIZE</span><span class="p">,</span> <span class="nx">target</span><span class="o">:</span> <span class="s1">&#39;*&#39;</span><span class="p">)]</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">alter</span><span class="p">(</span><span class="nx">InputInterface</span> <span class="nv">$input</span><span class="p">,</span> <span class="nx">AnnotationData</span> <span class="nv">$annotationData</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$self</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">siteAliasManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getSelf</span><span class="p">();</span>
<span class="nv">$self</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">siteAliasManager</span><span class="o">-&gt;</span><span class="na">getSelf</span><span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$self</span><span class="o">-&gt;</span><span class="na">isRemote</span><span class="p">())</span> <span class="p">{</span>
<span class="c1">// Always pass along ssh keys.</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$self</span><span class="o">-&gt;</span><span class="na">has</span><span class="p">(</span><span class="s1">&#39;ssh.options&#39;</span><span class="p">))</span> <span class="p">{</span>
Expand Down
2 changes: 1 addition & 1 deletion 13.x/search/search_index.json

Large diffs are not rendered by default.

Binary file modified 13.x/sitemap.xml.gz
Binary file not shown.

0 comments on commit 72df5f5

Please sign in to comment.