public function buildIndex($basedir, OutputInterface $output, array $options = array())
    {
        $target = $this->ensureBuildDir($basedir, $output);

        $built_iterator = new DirectoryIterator($target);
        $index = $target . DIRECTORY_SEPARATOR . 'index.rst';

        $title = str_replace('\\', '\\\\', $this->reflection->getName());
        if (isset($options['title'])) {
            $title = $options['title'];
        }

        $depth = substr_count($this->reflection->getName(), '\\');

        $template = str_repeat($this->titles[$depth], strlen($title)) . "\n";
        $template .= $title . "\n";
        $template .= str_repeat($this->titles[$depth], strlen($title)) . "\n\n";
        $template .= $this->getNamespaceElement();

        $template .= ".. toctree::\n\n";

        foreach ($built_iterator as $file) {
            if ($file->isDot()) continue;
            if ($file->isFile() && !$file->getExtension() == 'rst') continue;
            if ($file->isFile() && substr($file->getBaseName(), 0, 1) == '.') continue;
            if ($file->getBaseName() == 'index.rst') continue;

            $template .= '   ' . pathinfo($file->getPathName(), PATHINFO_FILENAME);

            if ($file->isDir()) {
                $template .= '/index';
            }

            $template .= "\n";
        }

        file_put_contents($index, $template);
    }