namespace Drupal\Console\Command\Shared;
-use Drupal\Console\Core\Style\DrupalStyle;
-
trait ServicesTrait
{
/**
- * @param DrupalStyle $io
- *
* @return mixed
*/
- public function servicesQuestion(DrupalStyle $io)
+ public function servicesQuestion()
{
- if ($io->confirm(
+ if ($this->getIo()->confirm(
$this->trans('commands.common.questions.services.confirm'),
false
)
) {
$service_collection = [];
- $io->writeln($this->trans('commands.common.questions.services.message'));
+ $this->getIo()->writeln($this->trans('commands.common.questions.services.message'));
$services = $this->container->getServiceIds();
+
while (true) {
- $service = $io->choiceNoList(
+ $service = $this->getIo()->choiceNoList(
$this->trans('commands.common.questions.services.name'),
$services,
- null,
+ '',
true
);
$service = trim($service);
- if (empty($service)) {
+ if (empty($service) || is_numeric($service)) {
break;
}
*/
public function buildServices($services)
{
+ $buildServices = [];
if (!empty($services)) {
- $buildServices = [];
foreach ($services as $service) {
$class = get_class($this->container->get($service));
+ $class = $this->getInterface($class);
$shortClass = explode('\\', $class);
$machineName = str_replace('.', '_', $service);
$buildServices[$service] = [
'short' => end($shortClass),
];
}
+ }
+
+ return $buildServices;
+ }
- 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 [];
+ return $interfaceName;
}
}