Skip to content

Commit

Permalink
Use interface instead of classe, if it is possible (#3777)
Browse files Browse the repository at this point in the history
  • Loading branch information
LOBsTerr authored and jmolivas committed Feb 17, 2018
1 parent 12ef535 commit 0c47f84
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Command/Shared/ServicesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function buildServices($services)
if (!empty($services)) {
foreach ($services as $service) {
$class = get_class($this->container->get($service));
$class = $this->getInterface($class);
$shortClass = explode('\\', $class);
$machineName = str_replace('.', '_', $service);
$buildServices[$service] = [
Expand All @@ -73,4 +74,27 @@ public function buildServices($services)

return $buildServices;
}

/**
* Gets class interface.
*
* @param string $class
* Class name.
*
* @return string
* Interface
*/
private function getInterface($class) {
$interfaceName = $class;
$interfaces = class_implements($class);
if (!empty($interfaces)) {
if (count($interfaces) == 1) {
$interfaceName = array_shift($interfaces);
} elseif ($key = array_search($class . 'Interface', $interfaces)) {
$interfaceName = $interfaces[$key];
}
}

return $interfaceName;
}
}
2 changes: 1 addition & 1 deletion templates/module/src/TwigExtension/twig-extension.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class {{ class }} extends \Twig_Extension {% endblock %}
/**
* {{ service.class }} definition.
*
* @var \{{ service.class }}
* @var {{ service.short }}
*/
protected ${{service.camel_case_name}};
{% endfor %}
Expand Down

0 comments on commit 0c47f84

Please sign in to comment.