<commit_before><?php

namespace WP_CLI_Dotenv\WP_CLI;

use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Dotenv\Dotenv\File;

/**
 * Manage a .env file
 */
class DotenvCommand extends Command
{
    /**
     * Initialize the environment file
     *
     * [--file=<path-to-dotenv>]
     * : Path to the environment file.  Default: '.env'
     *
     * [--with-salts]
     * : Additionally, generate and define keys for salts
     *
     * [--template=<template-name>]
     * : Path to a template to use to interactively set values
     *
     * [--interactive]
     * : Set new values from the template interactively with prompts for each key-value pair
     *
     * [--force]
     * : Overwrite existing destination file if it exists
     *
     * @when before_wp_load
     *
     * @param $_
     * @param $assoc_args
     */
    public function init($_, $assoc_args)
    {
        $this->init_args(func_get_args());
        $path = $this->resolve_file_path();

        if (file_exists($path) && ! $this->get_flag('force')) {
            WP_CLI::error("Environment file already exists at: $path");

            return;
        }

        $env = File::create($path);

        if (! $env->exists()) {
            WP_CLI::error('Failed to create environment file at: ' . $env->path());

            return;
        }

        if ($this->args->template) {
            $this->init_from_template($env, $this->args->template);
        }

        if ($this->get_flag('with-salts')) {
            WP_CLI::run_command(['env', 'salts', 'generate'], ['file' => $env->path()]);
        }

        WP_CLI::success("$path created.");
    }

    /**
     * @param $env
     * @param $template
     */
    protected function init_from_template(File $env, $template)
    {
        if (! $env->is_writable()) {
            WP_CLI::error('Environment file is not readable at: ' . $env->path());

            return;
        }

        $template_path = $this->resolve_file_path($template);

        try {
            $env_template = File::at($template_path);
        } catch (\Exception $e) {
            WP_CLI::error("Template file is not readable at: $template_path");

            return;
        }

        WP_CLI::line('Initializing from template: ' . $env_template->path());

        copy($env_template->path(), $env->path());

        // we can't use WP-CLI --prompt because we're working off the template, not the synopsis
        if (! $interactive = $this->get_flag('interactive')) {
            return;
        }

        $env->load(); // reload the new copied data from template

        WP_CLI::line();
        WP_CLI::line('Interactive init');
        WP_CLI::line('Specify a new value for each key, or leave blank to keep the current value.');
        WP_CLI::line();

        $this->prompt_all($env);
    }

    /**
     * Set a value in the environment file for a given key.
     * Updates an existing value or creates a new entry.
     *
     * <key><commit_msg>remove unused temporary variable<commit_after><?php

namespace WP_CLI_Dotenv\WP_CLI;

use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Dotenv\Dotenv\File;

/**
 * Manage a .env file
 */
class DotenvCommand extends Command
{
    /**
     * Initialize the environment file
     *
     * [--file=<path-to-dotenv>]
     * : Path to the environment file.  Default: '.env'
     *
     * [--with-salts]
     * : Additionally, generate and define keys for salts
     *
     * [--template=<template-name>]
     * : Path to a template to use to interactively set values
     *
     * [--interactive]
     * : Set new values from the template interactively with prompts for each key-value pair
     *
     * [--force]
     * : Overwrite existing destination file if it exists
     *
     * @when before_wp_load
     *
     * @param $_
     * @param $assoc_args
     */
    public function init($_, $assoc_args)
    {
        $this->init_args(func_get_args());
        $path = $this->resolve_file_path();

        if (file_exists($path) && ! $this->get_flag('force')) {
            WP_CLI::error("Environment file already exists at: $path");

            return;
        }

        $env = File::create($path);

        if (! $env->exists()) {
            WP_CLI::error('Failed to create environment file at: ' . $env->path());

            return;
        }

        if ($this->args->template) {
            $this->init_from_template($env, $this->args->template);
        }

        if ($this->get_flag('with-salts')) {
            WP_CLI::run_command(['env', 'salts', 'generate'], ['file' => $env->path()]);
        }

        WP_CLI::success("$path created.");
    }

    /**
     * @param $env
     * @param $template
     */
    protected function init_from_template(File $env, $template)
    {
        if (! $env->is_writable()) {
            WP_CLI::error('Environment file is not readable at: ' . $env->path());

            return;
        }

        $template_path = $this->resolve_file_path($template);

        try {
            $env_template = File::at($template_path);
        } catch (\Exception $e) {
            WP_CLI::error("Template file is not readable at: $template_path");

            return;
        }

        WP_CLI::line('Initializing from template: ' . $env_template->path());

        copy($env_template->path(), $env->path());

        // we can't use WP-CLI --prompt because we're working off the template, not the synopsis
        if (! $this->get_flag('interactive')) {
            return;
        }

        $env->load(); // reload the new copied data from template

        WP_CLI::line();
        WP_CLI::line('Interactive init');
        WP_CLI::line('Specify a new value for each key, or leave blank to keep the current value.');
        WP_CLI::line();

        $this->prompt_all($env);
    }

    /**
     * Set a value in the environment file for a given key.
     * Updates an existing value or creates a new entry.
     *
     * <key>
{'fraction_non_alphanumeric': 0.14430550861777627, 'fraction_numerical': 0.0, 'mean_word_length': 2.0989528795811516, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2858151', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1768, 'n_words': 451}