<commit_before><?php
/*
 * Fusio
 * A web-application to create dynamically RESTful APIs
 *
 * Copyright (C) 2015-2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Fusio\Impl\Loader;

use Doctrine\Common\Annotations\Reader;
use Fusio\Impl\Authorization\Authorization;
use Fusio\Impl\Table;
use PSX\Api\Generator;
use PSX\Api\GeneratorInterface;

/**
 * GeneratorFactory
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/agpl-3.0
 * @link    http://fusio-project.org
 */
class GeneratorFactory extends \PSX\Api\GeneratorFactory
{
    /**
     * @var \Fusio\Impl\Table\Scope
     */
    protected $scopeTable;

    public function __construct(Table\Scope $scopeTable, Reader $reader, $namespace, $url, $dispatch)
    {
        parent::__construct($reader, $namespace, $url, $dispatch);

        $this->scopeTable = $scopeTable;
    }

    protected function configure(GeneratorInterface $generator)
    {
        if ($generator instanceof Generator\OpenAPI) {
            $authUrl  = $this->url . '/developer/auth';
            $tokenUrl = $this->url . '/' . $this->dispatch . 'authorization/token';
            $scopes   = $this->getScopes();

            $generator->setTitle('Fusio');
            $generator->setAuthorizationFlow(Authorization::APP, Generator\OpenAPI::FLOW_AUTHORIZATION_CODE, $authUrl, $tokenUrl, null, $scopes);
            $generator->setAuthorizationFlow(Authorization::APP, Generator\OpenAPI::FLOW_PASSWORD, null, $tokenUrl, null, $scopes);

            $tokenUrl = $this->url . '/' . $this->dispatch . 'backend/token';
            $generator->setAuthorizationFlow(Authorization::BACKEND, Generator\OpenAPI::FLOW_CLIENT_CREDENTIALS, null, $tokenUrl, null, ['backend' => 'Backend', 'authorization' => 'Authorization']);

            $tokenUrl = $this->url . '/' . $this->dispatch . 'consumer/token';
            $generator->setAuthorizationFlow(Authorization::CONSUMER, Generator\OpenAPI::FLOW_CLIENT_CREDENTIALS, null, $tokenUrl, null, ['consumer' => 'Consumer', 'authorization' => 'Authorization']);
        } elseif ($generator instanceof Generator\Raml) {
            $generator->setTitle('Fusio');
        } elseif ($generator instanceof Generator\Swagger) {
            $generator->setTitle('Fusio');
        }
    }

    private function getScopes()
    {
        $result = [];
        $scopes = $this->scopeTable->getAll(0, 1024);
        foreach ($scopes as $scope) {
            $result[$scope['name']] = $scope['description'];
        }

        return $result;
    }<commit_msg>add config service and improve scope selection<commit_after><?php
/*
 * Fusio
 * A web-application to create dynamically RESTful APIs
 *
 * Copyright (C) 2015-2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Fusio\Impl\Loader;

use Doctrine\Common\Annotations\Reader;
use Fusio\Impl\Authorization\Authorization;
use Fusio\Impl\Service;
use Fusio\Impl\Table;
use PSX\Api\Generator;
use PSX\Api\GeneratorInterface;

/**
 * GeneratorFactory
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/agpl-3.0
 * @link    http://fusio-project.org
 */
class GeneratorFactory extends \PSX\Api\GeneratorFactory
{
    /**
     * @var \Fusio\Impl\Table\Scope
     */
    protected $scopeTable;

    /**
     * @var \Fusio\Impl\Service\Config
     */
    protected $configService;

    public function __construct(Table\Scope $scopeTable, Service\Config $configService, Reader $reader, $namespace, $url, $dispatch)
    {
        parent::__construct($reader, $namespace, $url, $dispatch);

        $this->scopeTable    = $scopeTable;
        $this->configService = $configService;
    }

    protected function configure(GeneratorInterface $generator)
    {
        if ($generator instanceof Generator\OpenAPI) {
            $authUrl  = $this->configService->getValue('authorization_url') ?: $this->url . '/developer/auth';
            $tokenUrl = $this->url . '/' . $this->dispatch . 'authorization/token';

            list($appScopes, $backendScopes, $consumerScopes) = $this->getScopes();

            $generator->setTitle('Fusio');
            $generator->setAuthorizationFlow(Authorization::APP, Generator\OpenAPI::FLOW_AUTHORIZATION_CODE, $authUrl, $tokenUrl, null, $appScopes);
            $generator->setAuthorizationFlow(Authorization::APP, Generator\OpenAPI::FLOW_PASSWORD, null, $tokenUrl, null, $appScopes);

            $tokenUrl = $this->url . '/' . $this->dispatch . 'backend/token';
            $generator->setAuthorizationFlow(Authorization::BACKEND, Generator\OpenAPI::FLOW_CLIENT_CREDENTIALS, null, $tokenUrl, null, $backendScopes);

            $tokenUrl = $this->url . '/' . $this->dispatch . 'consumer/token';
            $generator->setAuthorizationFlow(Authorization::CONSUMER, Generator\OpenAPI::FLOW_CLIENT_CREDENTIALS, null, $tokenUrl, null, $consumerScopes);
        } elseif ($generator instanceof Generator\Raml) {
            $generator->setTitle('Fusio');
        } elseif ($generator instanceof Generator\Swagger) {
            $generator->setTitle('Fusio');
        }
    }

    private function getScopes()
    {
        $app = [];
        $backend = [];
        $consumer = [];

        $scopes = $this->scopeTable->getAll(0, 1024);
        foreach ($scopes as $scope) {
            if ($scope['name'] == 'backend') {
                $backend[$scope['name']] = $scope['description'];
            } elseif ($scope['name'] == 'consumer') {
                $consumer[$scope['name']] = $scope['description'];
            } else {
                $app[$scope['name']] = $scope['description'];
            }
        }

        return [$app, $backend, $consumer];
    }
{'fraction_non_alphanumeric': 0.13504916136495085, 'fraction_numerical': 0.004626951995373048, 'mean_word_length': 3.122169249106079, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2739851', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1977, 'n_words': 502}