<commit_before><?php
/**
 * Widget Framework
 *
 * @copyright   Copyright (c) 2008-2013 <NAME>
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */

namespace Widget;

/**
 * A widget to detect the environment name and load configuration by environment name
 *
 * The environment name detect order:
 *
 *     user defined $env > $envDetect callback > $envMap
 *
 * @author      <NAME> <<EMAIL>>
 * @property    Request $request A widget that handles the HTTP request Data
 */
class Env extends AbstractWidget
{
    /**
     * The environment name
     *
     * @var string
     */
    protected $env;

    /**
     * A callback to detect the environment name
     *
     * @var callable
     */
    protected $detector;

    /**
     * An associative array contains server ip and environment name pairs
     *
     * @var array
     */
    protected $envMap = array(
        '127.0.0.1' => 'dev'
    );

    /**
     * The configuration directory pattern
     *
     * @var string
     */
    protected $configDir = 'config/config_%env%.php';

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        parent::__construct($options);

        // Detect environment name if not set
        if (!$this->env) {
            $this->detectEnvName();
        }

        // Load configuration by environment name
        $file = str_replace('%env%', $this->env, $this->configDir);
        $this->loadConfigFile($file);

        // Load CLI configuration when run in CLI mode
        if (php_sapi_name() === 'cli') {
            $file = str_replace('%env%', 'cli', $this->configDir);
            $this->loadConfigFile($file);
        }
    }

    /**
     * Returns the environment name
     *
     * @return string
     */
    public function __invoke()
    {
        return $this->env;
    }

    /**
     * Detect environment by server IP
     */
    public function detectEnvName()
    {
        if ($this->detector) {
            $this->env = call_user_func($this->detector);
            return;
        }

        // Executes in web server, like Apache, Nginx
        if ($ip = $this->request->getServer('SERVER_ADDR')) {
            if (isset($this->envMap[$ip])) {
                $this->env = $this->envMap[$ip];
            } else {
                $this->env = 'prod';
            }
            return;
        }

        // Executes in CLI
        if (php_sapi_name() == 'cli' && $ips = $this->getServerIps()) {
            foreach ($ips as $ip) {
                if (isset($this->envMap[$ip])) {
                    $this->env = $this->envMap[$ip];
                    break;
                }
            }
            if (!$this->env) {
                $this->env = 'prod';
            }
            return;
        }

        $this->env = 'prod';
        return;
    }

    /**
     * Check if in the development environment
     *
     * @return bool
     */<commit_msg>updated detect env name logic 2<commit_after><?php
/**
 * Widget Framework
 *
 * @copyright   Copyright (c) 2008-2013 <NAME>
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */

namespace Widget;

/**
 * A widget to detect the environment name and load configuration by environment name
 *
 * The environment name detect order:
 *
 *     user defined $env > $envDetect callback > $envMap
 *
 * @author      <NAME> <<EMAIL>>
 * @property    Request $request A widget that handles the HTTP request Data
 */
class Env extends AbstractWidget
{
    /**
     * The environment name
     *
     * @var string
     */
    protected $env;

    /**
     * A callback to detect the environment name
     *
     * @var callable
     */
    protected $detector;

    /**
     * An associative array contains server ip and environment name pairs
     *
     * @var array
     */
    protected $envMap = array(
        '127.0.0.1' => 'dev'
    );

    /**
     * The configuration directory pattern
     *
     * @var string
     */
    protected $configDir = 'config/config_%env%.php';

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        parent::__construct($options);

        // Detect environment name if not set
        if (!$this->env) {
            $this->detectEnvName();
        }

        // Load configuration by environment name
        $file = str_replace('%env%', $this->env, $this->configDir);
        $this->loadConfigFile($file);

        // Load CLI configuration when run in CLI mode
        if (php_sapi_name() === 'cli') {
            $file = str_replace('%env%', 'cli', $this->configDir);
            $this->loadConfigFile($file);
        }
    }

    /**
     * Returns the environment name
     *
     * @return string
     */
    public function __invoke()
    {
        return $this->env;
    }

    /**
     * Detect environment by server IP
     */
    public function detectEnvName()
    {
        if ($this->detector) {
            $this->env = call_user_func($this->detector);
            return;
        }

        // Executes in web server, like Apache, Nginx
        if ($ip = $this->request->getServer('SERVER_ADDR')) {
            if (isset($this->envMap[$ip])) {
                $this->env = $this->envMap[$ip];
            } else {
                $this->env = 'prod';
            }
            return;
        }

        // Executes in CLI
        if (php_sapi_name() == 'cli' && $ips = $this->getServerIps()) {
            foreach ($ips as $ip) {
                if (isset($this->envMap[$ip])) {
                    $this->env = $this->envMap[$ip];
                    return;
                }
            }
        }

        $this->env = 'prod';
        return;
    }

    /**
     * Check if in the development environment
     *
     * @return bool
     */
{'fraction_non_alphanumeric': 0.13473612760902767, 'fraction_numerical': 0.004921092821992194, 'mean_word_length': 1.6477987421383649, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2712334', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1648, 'n_words': 440}