You are here

class UnusedModulesCommands in Unused Modules 8

A Drush commandfile.

In addition to this file, you need a drush.services.yml in root of your module, and a composer.json file that provides the name of the services file to use.

See these files for an example of injecting Drupal services:

Hierarchy

Expanded class hierarchy of UnusedModulesCommands

1 string reference to 'UnusedModulesCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses UnusedModulesCommands
unused_modules.commands in ./drush.services.yml
\Drupal\unused_modules\Commands\UnusedModulesCommands

File

src/Commands/UnusedModulesCommands.php, line 20

Namespace

Drupal\unused_modules\Commands
View source
class UnusedModulesCommands extends DrushCommands {

  /**
   * Unused modules helper service.
   *
   * @var \Drupal\unused_modules\UnusedModulesHelperService
   */
  protected $unusedModulesHelper;

  /**
   * {@inheritdoc}
   */
  public function __construct(UnusedModulesHelperService $unusedModulesHelper) {
    parent::__construct();
    $this->unusedModulesHelper = $unusedModulesHelper;
  }

  /**
   * Show unused modules or projects.
   *
   * @param string $type
   *   Options "projects" and "modules". Show modules or projects.
   * @param string $show
   *   Options "all" and "disabled". Show only disabled modules.
   *
   * @usage drush unused-modules projects disabled
   *   Show projects that are unused.
   * @usage drush um
   *   As above, shorthand.
   * @usage drush unused-modules projects disabled
   *   As above, include projects with enabled modules.
   * @usage drush unused-modules modules disabled
   *   Show modules that are unused.
   * @usage drush unused-modules modules all
   *   As above, include enabled modules.
   *
   * @table-style default
   * @field-labels
   *   project: Project
   *   module: Module
   *   enabled: Module enabled
   *   has_modules: Project has Enabled Modules
   *   path: Project Path
   * @default-fields project,module,enabled,has_modules,path
   *
   * @command unused:modules
   * @aliases um,unused_modules,unused-modules
   *
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
   *   Rows with unused project/module information.
   */
  public function modules($type = "projects", $show = "disabled") {

    // Print projects.
    if ($type == 'projects') {
      if ($show == 'all') {
        return $this
          ->showProjects('all');
      }
      elseif ($show == 'disabled') {
        return $this
          ->showProjects('disabled');
      }
      else {
        throw new \Exception("unknown 'show' argument " . $show . ". See drush unused-modules --help");
      }
    }
    elseif ($type == 'modules') {
      if ($show == 'all') {
        return $this
          ->showModules('all');
      }
      elseif ($show == 'disabled') {
        return $this
          ->showModules('disabled');
      }
      else {
        throw new \Exception("unknown 'show' argument " . $show . ". See drush unused-modules --help");
      }
    }
    else {
      throw new \Exception("unknown 'type' argument " . $type . ". See drush unused-modules --help");
    }
  }

  /**
   * Drush callback.
   *
   * Prints a table with orphaned projects.
   *
   * @param string $op
   *   Either 'all' or 'disabled'.
   *
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
   *   Rows with unused project information.
   */
  private function showProjects($op = 'all') {
    $modules = $this->unusedModulesHelper
      ->getModulesByProject();
    $rows = [];
    foreach ($modules as $module) {
      if ($op == 'all') {
        $rows[$module->projectName] = [
          'project' => $module->projectName,
          'has_modules' => $module->projectHasEnabledModules ? dt("Yes") : dt("No"),
          'path' => $module->projectPath,
        ];
      }
      elseif ($op == 'disabled') {
        if (!$module->projectHasEnabledModules) {
          $rows[$module->projectName] = [
            'project' => $module->projectName,
            'has_modules' => $module->projectHasEnabledModules ? dt("Yes") : dt("No"),
            'path' => $module->projectPath,
          ];
        }
      }
    }
    if (!count($rows)) {
      $this
        ->output()
        ->writeln("Hurray, no orphaned projects!");
      return NULL;
    }
    return new RowsOfFields($rows);
  }

  /**
   * Drush callback.
   *
   * Prints a table with orphaned modules.
   *
   * @param string $op
   *   Either 'all' or 'disabled'.
   *
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
   *   Rows with unused module information.
   */
  private function showModules($op = 'all') {
    $modules = $this->unusedModulesHelper
      ->getModulesByProject();
    $rows = [];
    foreach ($modules as $module) {
      if ($op == 'all') {
        $rows[$module
          ->getName()] = [
          'project' => $module->projectName,
          'module' => $module
            ->getName(),
          'enabled' => $module->moduleIsEnabled ? dt("Yes") : dt("No"),
          'has_modules' => $module->projectHasEnabledModules ? dt("Yes") : dt("No"),
          'path' => $module->projectPath,
        ];
      }
      elseif ($op == 'disabled') {
        if (!$module->projectHasEnabledModules) {
          $rows[$module
            ->getName()] = [
            'project' => $module->projectName,
            'module' => $module
              ->getName(),
            'enabled' => $module->moduleIsEnabled ? dt("Yes") : dt("No"),
            'has_modules' => $module->projectHasEnabledModules ? dt("Yes") : dt("No"),
            'path' => $module->projectPath,
          ];
        }
      }
    }
    if (!count($rows)) {
      $this
        ->output()
        ->writeln("Hurray, no orphaned modules!");
      return NULL;
    }
    return new RowsOfFields($rows);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnusedModulesCommands::$unusedModulesHelper protected property Unused modules helper service.
UnusedModulesCommands::modules public function Show unused modules or projects.
UnusedModulesCommands::showModules private function Drush callback.
UnusedModulesCommands::showProjects private function Drush callback.
UnusedModulesCommands::__construct public function