I had this question in mind along all the last week: When I should create a service or an utility function?
In the Drupal Core we have both Services and Utility functions, but I can't find the distinction between them (when I need to create a service or when I need to create an utility function).
I will take as example the Modules Weight module where I have the InternalFunctions class.
<?php
namespace Drupal\modules_weight\Utility;
class InternalFunctions {
public static function prepareDelta($weight) {
$delta = 100;
$weight = (int) $weight;
if ($weight > $delta) {
return $weight;
}
if ($weight < -100) {
return $weight * -1;
}
return $delta;
}
public static function modulesList($force = FALSE) {
$modules = [];
$installed_modules = system_get_info('module');
$config_factory = \Drupal::service('config.factory');
if ($force) {
$show_system_modules = TRUE;
}
else {
modules.
$show_system_modules = $config_factory->get('modules_weight.settings')->get('show_system_modules');
}
$modules_weight = $config_factory->get('core.extension')->get('module');
foreach ($installed_modules as $filename => $module_info) {
if (!isset($module_info['hidden']) && ($show_system_modules || $module_info['package'] != 'Core')) {
$modules[$filename]['name'] = $module_info['name'];
$modules[$filename]['description'] = $module_info['description'];
$modules[$filename]['weight'] = $modules_weight[$filename];
$modules[$filename]['package'] = $module_info['package'];
}
}
uasort($modules, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
return $modules;
}
}
In this class I have two static functions but they are both utility functions or prepareDelta()
is an utility function and modulesList()
should be in another class and have a service?
The only difference that I found at this time is that inside the namespace Drupal\Component\Utility (where you will see a lot of utility fucntions) none of them use inside a service and usually a service use another services inside (I don't have review all the services to validate this).
So, when I should create a service or an utility function?
Unicode
class in core - that's a static utility class, not a service, because it doesn't have any dependencies, and doesn't need to maintain any state. If it required a service dependency, the DI pattern would require it to be converted to a service, and you'd use the singleton (or factory-generated) instance from the container when you needed it. Otherwise you can justuse
the static class it when it makes sense.it doesn't really need to be.
One could argue it's the "Drupal way". Also, making it a service makes it easier for other devs to find what you can overwrite.Unicode
is a Drupal class that contains only static methods! The fact that the core devs chose to implement it as a static class, rather than a service, probably means something don't you think? A utility class doesn't really need to be overwritten, by its nature - it does some things, if those things aren't what you want, you write your own class instead. Remember the sort of things that traditionally live in utility classes are one-shot, "I do this and nothing else" types of methods, that don't need input except a set of parameters