<commit_before><?php
/*
 * Fusio
 * A web-application to create dynamically RESTful APIs
 *
 * Copyright (C) 2015-2018 <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\Service\System\Deploy;

use RuntimeException;

/**
 * EnvProperties
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/agpl-3.0
 * @link    http://fusio-project.org
 */
class EnvProperties
{
    /**
     * @param string $data
     * @return string
     */
    public static function replace($data)
    {
        $vars = [];

        // dir properties
        $vars['dir'] = [
            'cache'  => PSX_PATH_CACHE,
            'src'    => PSX_PATH_SRC,
            'public' => PSX_PATH_PUBLIC,
            'temp'   => sys_get_temp_dir(),
        ];

        // env properties
        $vars['env'] = [];
        foreach ($_SERVER as $key => $value) {
            if (is_scalar($value)) {
                $vars['env'][strtolower($key)] = $value;
            }
        }

        // replace
        $data = preg_replace_callback('/\$\{([0-9A-Za-z_]+).([0-9A-Za-z_]+)\}/', function($matches) use ($vars){
            $type = strtolower($matches[1]);
            $key  = strtolower($matches[2]);

            if (isset($vars[$type])) {
                if (isset($vars[$type][$key])) {
                    $value = $vars[$type][$key];

                    if (is_string($value)) {
                        $value = trim(json_encode($value), '"');
                    }

                    return $value;
                } else {
                    throw new RuntimeException('Usage of unknown variable key "' . $key . '", allowed is (' . implode(', ', array_keys($vars[$type])) . ')');
                }<commit_msg>add option to provide server env variables through parameter<commit_after><?php
/*
 * Fusio
 * A web-application to create dynamically RESTful APIs
 *
 * Copyright (C) 2015-2018 <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\Service\System\Deploy;

use RuntimeException;

/**
 * EnvProperties
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/agpl-3.0
 * @link    http://fusio-project.org
 */
class EnvProperties
{
    /**
     * @param string $data
     * @param array $env
     * @return string
     */
    public static function replace($data, array $env = null)
    {
        $vars = [];

        // dir properties
        $vars['dir'] = [
            'cache'  => PSX_PATH_CACHE,
            'src'    => PSX_PATH_SRC,
            'public' => PSX_PATH_PUBLIC,
            'temp'   => sys_get_temp_dir(),
        ];

        // env properties
        if ($env === null) {
            $env = $_SERVER;
        }

        $vars['env'] = [];
        foreach ($env as $key => $value) {
            if (is_scalar($value)) {
                $vars['env'][strtolower($key)] = $value;
            }
        }

        // replace
        $data = preg_replace_callback('/\$\{([0-9A-Za-z_]+).([0-9A-Za-z_]+)\}/', function($matches) use ($vars){
            $type = strtolower($matches[1]);
            $key  = strtolower($matches[2]);

            if (isset($vars[$type])) {
                if (isset($vars[$type][$key])) {
                    $value = $vars[$type][$key];

                    if (is_string($value)) {
                        $value = trim(json_encode($value), '"');
                    }

                    return $value;
                } else {
                    throw new RuntimeException('Usage of unknown variable key "' . $key . '", allowed is (' . implode(', ', array_keys($vars[$type])) . ')');
                }
{'fraction_non_alphanumeric': 0.1402061855670103, 'fraction_numerical': 0.0070103092783505155, 'mean_word_length': 2.0130434782608697, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, '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': '6976522', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1360, 'n_words': 409}