Skip to content

Commit

Permalink
WV-4745: Add mudule_hander service trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lio Novelli committed Nov 17, 2021
1 parent 97169ab commit 5d7fe43
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Core/Extension/ModuleHandlerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace drunomics\ServiceUtils\Core\Extension;

use Drupal\Core\Extension\ModuleHandlerInterface;

/**
* Allows setter injection and simple usage of the service.
*/
trait ModuleHandlerTrait {

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Sets the module handler.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*
* @return $this
*/
public function setModuleHandler(ModuleHandlerInterface $moduleHandler) {
$this->moduleHandler = $moduleHandler;
return $this;
}

/**
* Gets the module handler.
*
* @return \Drupal\Core\Extension\ModuleHandlerInterface
* The module handler.
*/
public function getModuleHandler() {
if (empty($this->moduleHandler)) {
$this->moduleHandler = \Drupal::service('module_handler');
}
return $this->moduleHandler;
}

}

0 comments on commit 5d7fe43

Please sign in to comment.