Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to implement SiteAliasManagerAwareInterface #3394

Closed
himerus opened this issue Feb 18, 2018 · 7 comments
Closed

Unable to implement SiteAliasManagerAwareInterface #3394

himerus opened this issue Feb 18, 2018 · 7 comments

Comments

@himerus
Copy link

himerus commented Feb 18, 2018

Issue Summary

The basics are this: I'm attempting to write a Drush command that uses the site aliases available to an environment in order to handle creating some custom sync/deploy commands between environments. In order to do so I need to implement the SiteAliasManagerAwareInterface and have access to the available aliases in drush/sites/mysite.yml.

I've used the drush @myalias generate command to build out the sample module and Drush command file. Then I replace the contents (and edit appropriately) from any of the Drush commands I found to implements SiteAliasManagerAwareInterface.

Replicate issue

I've created a sample module to demonstrate what I'm trying to accomplish, the issue I've noted, and to avoid having to post the contents of everything from the module/command file in this issue directly.

Sample module demonstrating issue:
sam_test

From SamTestCommands.php

<?php

namespace Drupal\sam_test\Commands;

use Drush\Commands\DrushCommands;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\SiteAlias\SiteAliasManagerAwareTrait;

class SamTestCommands extends DrushCommands implements SiteAliasManagerAwareInterface {
  use SiteAliasManagerAwareTrait;

  /**
   * Test to run $this->siteAliasManager()->getSelf();
   *
   * @command sam:test
   * @aliases sam-test
   * @usage drush sam:test
   *   Test that `implements SiteAliasManagerAwareInterface` is working correctly.
   * @handle-remote-commands true
   */
  public function example() {
    $alias = $this->siteAliasManager()->getSelf();
  }

  /**
   * Test to run $this->siteAliasManager()->searchLocations();
   *
   * @command sam:test2
   * @aliases sam-test2
   * @usage drush sam:test2
   *   Test that `implements SiteAliasManagerAwareInterface` is working correctly.
   * @handle-remote-commands true
   */
  public function example_two() {
    $alias = $this->siteAliasManager()->searchLocations();
  }
}

Resulting Error:

drush @mysite-alias sam:test:

Error: Call to a member function getSelf() on null in Drupal\sam_test\Commands\SamTestCommands->example() (line 22 of modules/custom/sam_test/src/Commands/SamTestCommands.php).

Conclusion

Either I'm doing something really wrong, and have wasted a day or two for nothing, or there's something wrong with implementing the SiteAliasManagerAwareInterface outside of the default Drush commands in Drush 9.

@weitzman
Copy link
Member

Hi @himerus!

Thanks for the example module and clear bug report. I can confirm the ill behavior. The issue is that the inflectors at

// Add inflectors
$container->inflector(\Drush\Boot\AutoloaderAwareInterface::class)
->invokeMethod('setAutoloader', ['loader']);
$container->inflector(\Drush\SiteAlias\SiteAliasManagerAwareInterface::class)
->invokeMethod('setSiteAliasManager', ['site.alias.manager']);
only run for Drush core commands. Perhaps @greg-1-anderson has an idea on how to address this for Drupal commands which use the Drupal container, not the League container like Drush. I think this need has come up before.

@weitzman
Copy link
Member

Until we have a dependency injection solution, your code should use Drush::aliasManager(). I think the docs dont make this clear at all.

@himerus
Copy link
Author

himerus commented Feb 18, 2018

Thanks @weitzman!

Yeah, the docs at http://docs.drush.org/en/master/site-alias-manager/ weren't super helpful, and until I wrap my head around some of the underpinnings, I was spinning my wheels a bit.

Drush::aliasManager() definitely gets me access to the alias data.

greg-1-anderson added a commit that referenced this issue Feb 20, 2018
@greg-1-anderson
Copy link
Member

See #3406

@Ambient-Impact
Copy link

Hi there. I'm still seeing this problem with Drush 9.7.1 despite seemingly following all the instructions in the docs and in this thread. I'm implementing the interface and using the trait, but I still end up with $this->siteAliasManager() returning null. Has this not been fixed, or is it possible I'm doing something wrong?

@bob-hinrichs
Copy link

bob-hinrichs commented Dec 15, 2020

I am finding Drush::aliasManager() still works in Drupal 9.1 + Drush 10, but is deprecated; but the suggested siteAliasManager() on the same service returns null. I have spent two days attempting to find any working guidance on the correct way to do it. Is this a bug? All I need to do is simply execute a drush command from a module's post-install hook.

FWIW, I eliminated the error by instantiating the SiteAliasManager and setting it into the service.

$command = \Drupal::service('my_service_implementing_siteawarenterface');
$command->setSiteAliasManager(new SiteAliasManager());
print "\r\nRun silly drush command devel entity updates...\r\n";
Drush::drush($command->siteAliasManager()->getSelf(), 'devel-entity-updates')->run();

@Ambient-Impact
Copy link

Ambient-Impact commented Aug 13, 2021

I am finding Drush::aliasManager() still works in Drupal 9.1 + Drush 10, but is deprecated; but the suggested siteAliasManager() on the same service returns null. I have spent two days attempting to find any working guidance on the correct way to do it. Is this a bug? All I need to do is simply execute a drush command from a module's post-install hook.

FWIW, I eliminated the error by instantiating the SiteAliasManager and setting it into the service.

$command = \Drupal::service('my_service_implementing_siteawarenterface');
$command->setSiteAliasManager(new SiteAliasManager());
print "\r\nRun silly drush command devel entity updates...\r\n";
Drush::drush($command->siteAliasManager()->getSelf(), 'devel-entity-updates')->run();

I think this has been fixed at some point since I posted my comment. On Drush 10.5, it seems to work if you try accessing $this->siteAliasManager() in a command method. It doesn't work in the command class' __construct() for whatever reason, but that's not usually crucial.

Also, I would strongly recommend using proper dependency injection when possible and not the \Drupal static class. Let me know if you want some more info on that or you're already familiar with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants