Version 1
[yaffs-website] / vendor / drupal / console-core / src / Helper / DescriptorHelper.php
diff --git a/vendor/drupal/console-core/src/Helper/DescriptorHelper.php b/vendor/drupal/console-core/src/Helper/DescriptorHelper.php
new file mode 100644 (file)
index 0000000..40e7f0c
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/*
+ * This file is part of the Drupal Console.
+ */
+namespace Drupal\Console\Core\Helper;
+
+use Symfony\Component\Console\Helper\Helper as BaseHelper;
+use Symfony\Component\Console\Descriptor\DescriptorInterface;
+use Symfony\Component\Console\Descriptor\JsonDescriptor;
+use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
+use Drupal\Console\Core\Descriptor\TextDescriptor;
+use Symfony\Component\Console\Descriptor\XmlDescriptor;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * This class adds helper method to describe objects in various formats.
+ *
+ * @author Jean-François Simon <contact@jfsimon.fr>
+ */
+class DescriptorHelper extends BaseHelper
+{
+    /**
+     * @var DescriptorInterface[]
+     */
+    private $descriptors = array();
+    /**
+     * Constructor.
+     */
+    public function __construct()
+    {
+        $this
+            ->register('txt', new TextDescriptor())
+            ->register('xml', new XmlDescriptor())
+            ->register('json', new JsonDescriptor())
+            ->register('md', new MarkdownDescriptor());
+    }
+    /**
+     * Describes an object if supported.
+     *
+     * Available options are:
+     * * format: string, the output format name
+     * * raw_text: boolean, sets output type as raw
+     *
+     * @param OutputInterface $output
+     * @param object          $object
+     * @param array           $options
+     *
+     * @throws \InvalidArgumentException when the given format is not supported
+     */
+    public function describe(OutputInterface $output, $object, array $options = array())
+    {
+        $options = array_merge(
+            array(
+            'raw_text' => false,
+            'format' => 'txt',
+            ), $options
+        );
+        if (!isset($this->descriptors[$options['format']])) {
+            throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
+        }
+        $descriptor = $this->descriptors[$options['format']];
+        $descriptor->describe($output, $object, $options);
+    }
+    /**
+     * Registers a descriptor.
+     *
+     * @param string              $format
+     * @param DescriptorInterface $descriptor
+     *
+     * @return DescriptorHelper
+     */
+    public function register($format, DescriptorInterface $descriptor)
+    {
+        $this->descriptors[$format] = $descriptor;
+        return $this;
+    }
+    /**
+     * {@inheritdoc}
+     */
+    public function getName()
+    {
+        return 'descriptor';
+    }
+}