<commit_before><?php

/*
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ML\HydraBundle;

use Doctrine\Common\Annotations\Reader;
use ML\HydraBundle\Mapping\Operation;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * The DocumentationGenerator
 */
class DocumentationGenerator
{
    const OPERATION_ANNOTATION = 'ML\\HydraBundle\\Mapping\\Operation';

    const COLLECTION_ANNOTATION = 'ML\\HydraBundle\\Mapping\\Collection';

    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    /**
     * @var \Symfony\Component\Routing\RouterInterface
     */
    private $router;

    /**
     * @var \Doctrine\Common\Annotations\Reader
     */
    private $reader;

    /**
     * Constructor
     *
     * @param ContainerInterface $container The service container.
     * @param RouterInterface    $router    The router.
     * @param Reader             $reader    The annotation reader.
     */
    public function __construct(ContainerInterface $container, RouterInterface $router, Reader $reader)
    {
        $this->container = $container;
        $this->router    = $router;
        $this->reader    = $reader;
    }

    public function isOperation(\ReflectionMethod $method)
    {
        if ($this->getAnnotation($method, self::OPERATION_ANNOTATION)) {
            return true;
        }

        return false;
    }

    /**
     * Gets the documentation
     *
     * @return array
     */
    public function getDocumentation()
    {
        $array = array();
        $resources = array();
        $documentation = array();
        $documentation['rels'] = array();
        $documentation['types2document'] = array();
        $documentation['vocab_base'] = $this->router->generate('hydra_vocab');
        $documentation['vocab_base_abs'] = $this->router->generate('hydra_vocab', array(), true);

        $this->documentType($documentation, 'ML\HydraBundle\Collection\Collection');

        foreach ($this->router->getRouteCollection()->all() as $name => $route) {
            if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
                if ($annotation = $this->getAnnotation($method, self::OPERATION_ANNOTATION)) {
                    $this->documentRoute($documentation, $name, $route, $annotation, $method);
                }
            }
        }
        while (count($documentation['types2document']) > 0)
        {
            $type = array_pop($documentation['types2document']);
            $this->documentType($documentation, $type);
        }

        unset($documentation['types2document']);

        return $documentation;
    }

    public function getVocabulary()
    {
        $documentation = $this->getDocumentation();

        $vocab = array();
        $vocabPrefix = $this->router->generate('hydra_vocab', array(), true) . '#';

        foreach ($documentation['types'] as $type => $definition) {
            $vocab[] = array(
                '@id' => $vocabPrefix . $type,
                '@type' => 'rdfs:Class',
                'short_name' => $type,
                'label' => $definition['title'],
                'description' => $definition['description'],
                'operations' => $this->getOperations4Vocab($documentation, $definition['operations'], $vocabPrefix),
            );

            foreach ($definition['properties'] as $name => $property) {
                if ('@id' === $name) {
                    continue;
                }

                $vocab[] = array(
                    '@id' => $vocabPrefix . $property['iri_fragment'],
                    '@type' => 'rdfs:Property',
                    'short_name' => $name,
                    'label' => $property['title'],
                    'description' => $property['description'],
                    'domain' => $vocabPrefix . $type,
                    'range' => $property['type'],
                    'readonly' => $property['readonly'],
                    'writeonly' => $property['writeonly'],
                    'operations' => $this->getOperations4Vocab($documentation, $property['operations'], $vocabPrefix)
                );
            }
        }

        foreach ($documentation['routes'] as $name => $route) {
            $vocab[] = array(
                '@id' => '_:{{ name }}',
                '@type' => 'hydra:Operation',
                'method' => $route['method'],
                'label' => $route['title'],
                'description' => $route['description']
            );
        }


        $vocab = array(
            '@context' => array(
                'vocab' => $vocabPrefix,
                'hydra' => 'http://purl.org/hydra/core#',
                'readonly' => 'hydra:readonly',
                'operations' => 'hydra:operations',
                'expects' => 'hydra:expects',
                'returns' => 'hydra:returns',
                'status_codes' => 'hydra:statusCodes',
                'code' => 'hydra:statusCode',
                'rdfs' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                'label' => 'rdfs:label',
                'description' => 'rdfs:comment',
                'domain' => 'rdfs:domain',
                'range' => 'rdfs:range',
            ),
            '@graph' => $vocab
        );

        return $vocab;
    }

    private function getOperations4Vocab($documentation, $operations, $vocabPrefix)
    {
        $result = array();
        foreach ($operations as $operation) {
            $statusCodes = array();
            foreach ($documentation['routes'][$operation]['status_codes'] as $code => $description) {
                $statusCodes[] = array(<commit_msg>Do not expose all documented routes in vocabulary<commit_after><?php

/*
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ML\HydraBundle;

use Doctrine\Common\Annotations\Reader;
use ML\HydraBundle\Mapping\Operation;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * The DocumentationGenerator
 */
class DocumentationGenerator
{
    const OPERATION_ANNOTATION = 'ML\\HydraBundle\\Mapping\\Operation';

    const COLLECTION_ANNOTATION = 'ML\\HydraBundle\\Mapping\\Collection';

    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    /**
     * @var \Symfony\Component\Routing\RouterInterface
     */
    private $router;

    /**
     * @var \Doctrine\Common\Annotations\Reader
     */
    private $reader;

    /**
     * Constructor
     *
     * @param ContainerInterface $container The service container.
     * @param RouterInterface    $router    The router.
     * @param Reader             $reader    The annotation reader.
     */
    public function __construct(ContainerInterface $container, RouterInterface $router, Reader $reader)
    {
        $this->container = $container;
        $this->router    = $router;
        $this->reader    = $reader;
    }

    public function isOperation(\ReflectionMethod $method)
    {
        if ($this->getAnnotation($method, self::OPERATION_ANNOTATION)) {
            return true;
        }

        return false;
    }

    /**
     * Gets the documentation
     *
     * @return array
     */
    public function getDocumentation()
    {
        $array = array();
        $resources = array();
        $documentation = array();
        $documentation['rels'] = array();
        $documentation['types2document'] = array();
        $documentation['vocab_base'] = $this->router->generate('hydra_vocab');
        $documentation['vocab_base_abs'] = $this->router->generate('hydra_vocab', array(), true);

        $this->documentType($documentation, 'ML\HydraBundle\Collection\Collection');

        foreach ($this->router->getRouteCollection()->all() as $name => $route) {
            if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
                if ($annotation = $this->getAnnotation($method, self::OPERATION_ANNOTATION)) {
                    $this->documentRoute($documentation, $name, $route, $annotation, $method);
                }
            }
        }
        while (count($documentation['types2document']) > 0)
        {
            $type = array_pop($documentation['types2document']);
            $this->documentType($documentation, $type);
        }

        unset($documentation['types2document']);

        return $documentation;
    }

    public function getVocabulary()
    {
        $documentation = $this->getDocumentation();

        $vocab = array();
        $vocabPrefix = $this->router->generate('hydra_vocab', array(), true) . '#';

        foreach ($documentation['types'] as $type => $definition) {
            $vocab[] = array(
                '@id' => $vocabPrefix . $type,
                '@type' => 'rdfs:Class',
                'short_name' => $type,
                'label' => $definition['title'],
                'description' => $definition['description'],
                'operations' => $this->getOperations4Vocab($documentation, $definition['operations'], $vocabPrefix),
            );

            foreach ($definition['properties'] as $name => $property) {
                if ('@id' === $name) {
                    continue;
                }

                $vocab[] = array(
                    '@id' => $vocabPrefix . $property['iri_fragment'],
                    '@type' => 'rdfs:Property',
                    'short_name' => $name,
                    'label' => $property['title'],
                    'description' => $property['description'],
                    'domain' => $vocabPrefix . $type,
                    'range' => $property['type'],
                    'readonly' => $property['readonly'],
                    'writeonly' => $property['writeonly'],
                    'operations' => $this->getOperations4Vocab($documentation, $property['operations'], $vocabPrefix)
                );
            }
        }

        $vocab = array(
            '@context' => array(
                'vocab' => $vocabPrefix,
                'hydra' => 'http://purl.org/hydra/core#',
                'readonly' => 'hydra:readonly',
                'operations' => 'hydra:operations',
                'expects' => 'hydra:expects',
                'returns' => 'hydra:returns',
                'status_codes' => 'hydra:statusCodes',
                'code' => 'hydra:statusCode',
                'rdfs' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                'label' => 'rdfs:label',
                'description' => 'rdfs:comment',
                'domain' => 'rdfs:domain',
                'range' => 'rdfs:range',
            ),
            '@graph' => $vocab
        );

        return $vocab;
    }

    private function getOperations4Vocab($documentation, $operations, $vocabPrefix)
    {
        $result = array();
        foreach ($operations as $operation) {
            $statusCodes = array();
            foreach ($documentation['routes'][$operation]['status_codes'] as $code => $description) {
                $statusCodes[] = array(
{'fraction_non_alphanumeric': 0.14964041244259596, 'fraction_numerical': 0.0029460185425872975, 'mean_word_length': 2.044579266684252, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493537', 'n_tokens_mistral': 3251, 'n_tokens_neox': 3099, 'n_words': 577}