The current system
I have a set of services classes that extends the same base class ex:
class NodeHandler extends ContentEntityHandlerBase {}
class ParagraphHandler extends ContentEntityHandlerBase {}
and this base class receives these dependencies:
class ContentEntityHandlerBase extends ControllerBase {
public function __construct(EntityTypeManager $entityTypeManager, RequestStack $requestStack, PermissionsService $permissionsService, LoggerChannelFactoryInterface $loggerChannelFactory) {
$this->entityTypeManager = $entityTypeManager;
$this->request = $requestStack->getCurrentRequest();
$this->permissionsService = $permissionsService;
$this->loggerChannelFactory = $loggerChannelFactory;
}
So for each child class ex:(NodeHandler , ParagraphHandler) I need always to inject the parent class dependencies ex:
services:
sacd_rest_api.node_handler:
class: 'Drupal\sacd_rest_api\service\NodeHandler'
arguments: ['@entity_type.manager','@request_stack','@sacd_user.permissions','@logger.factory']
sacd_rest_api.parapgraph_handler:
class: 'Drupal\sacd_rest_api\service\ParapgrapHandler'
arguments: ['@entity_type.manager','@request_stack','@sacd_user.permissions','@logger.factory']
The problem
I don't want to re-inject the parent dependencies each time I create a service class that extends it unless I want to inject a new dependencies that is not inherited from the Base Class for ex:
services:
sacd_rest_api.node_handler:
class: 'Drupal\sacd_rest_api\service\NodeHandler'
sacd_rest_api.parapgraph_handler:
class: 'Drupal\sacd_rest_api\service\ParapgrapHandler'
So in order to prevent re-injecting the base class dependencies for all baseClass child service ex: (NodeHandler and ParagraphHandler) so I have use the create() factory method but this doesn't work:
class ContentEntityHandlerBase extends ControllerBase {
public function __construct(EntityTypeManager $entityTypeManager, RequestStack $requestStack, PermissionsService $permissionsService, LoggerChannelFactoryInterface $loggerChannelFactory) {
$this->entityTypeManager = $entityTypeManager;
$this->request = $requestStack->getCurrentRequest();
$this->permissionsService = $permissionsService;
$this->loggerChannelFactory = $loggerChannelFactory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('request_stack'),
$container->get('sacd_user.permissions'),
$container->get('logger.factory')
);
}
}
Question ?
is there any way to ineject the Base Class service , when defining a child class service (NodeHnadler , ParagraphHandler) wihout using the arguments: part in the service definiton ?