lithium\core\Environment::is PHP Метод

is() публичный статический Метод

This method also handles how the environment is detected at the beginning of the request. #### Custom Detection While the default detection rules are very simple (if the 'SERVER_ADDR' variable is set to 127.0.0.1, the environment is assumed to be 'development', or if the string 'test' is found anywhere in the host name, it is assumed to be 'test', and in all other cases it is assumed to be 'production'), you can define your own detection rule set easily using a closure that accepts an instance of the Request object, and returns the name of the correct environment, as in the following example: embed:lithium\tests\cases\core\EnvironmentTest::testCustomDetector(1-9) In the above example, the user-specified closure takes in a Request object, and using the server data which it encapsulates, returns the correct environment name as a string. #### Host Mapping The most common use case is to set the environment depending on host name. For convenience, the is() method also accepts an array that matches host names to environment names, where each key is an environment, and each value is either an array of valid host names, or a regular expression used to match a valid host name. embed:lithium\tests\cases\core\EnvironmentTest::testDetectionWithArrayMap(1-5) In this example, a regular expression is being used to match local domains (i.e. localhost), as well as the built-in .console domain, for console requests. Note that in the console, the environment can always be overridden by specifying the --env option. Then, one or more host names are matched up to 'test' and 'staging', respectively. Note that no rule is present for production: this is because 'production' is the default value if no other environment matches.
public static is ( mixed $detect ) : boolean
$detect mixed Either the name of an environment to check against the current, i.e. `'development'` or `'production'`, or a closure which `Environment` will use to determine the current environment name, or an array mapping environment names to host names.
Результат boolean If `$detect` is a string, returns `true` if the current environment matches the value of `$detect`, or `false` if no match. If used to set a custom detector, returns `null`.
    public static function is($detect)
    {
        if (is_callable($detect)) {
            static::$_detector = $detect;
        }
        if (!is_array($detect)) {
            return static::$_current == $detect;
        }
        static::$_detector = function ($request) use($detect) {
            if ($request->env || $request->command == 'test') {
                return $request->env ?: 'test';
            }
            $host = method_exists($request, 'get') ? $request->get('http:host') : '.console';
            foreach ($detect as $environment => $hosts) {
                if (is_string($hosts) && preg_match($hosts, $host)) {
                    return $environment;
                }
                if (is_array($hosts) && in_array($host, $hosts)) {
                    return $environment;
                }
            }
            return "production";
        };
    }

Usage Example

Пример #1
0
 /**
  * Constructor for this adapter - sets relevant default configurations for Twig to be used
  * when instantiating a new Twig_Environment and Twig_Loader_Filesystem.
  *
  * @param array $config Optional configuration directives.
  *        Please see http://www.twig-project.org/book/03-Twig-for-Developers for all
  *        available configuration keys and their description.
  *        There are 4 settings that is set
  *        - `cache`: Path to /resources/tmp/cache/templates/ where compiled templates will be stored
  *        - `auto_reload`: If Environment is not production, templates will be reloaded once edited
  *        - `base_template_class`: Overriden to the Template adapter, be carefull with changing this
  *        - `autoescape`: Set to false because the way we inject content is with full html that should not be escaped
  * @return void
  */
 public function __construct(array $config = array())
 {
     /**
      * TODO Change hardcoded LITHIUM_APP_PATH to be dynamic
      */
     $defaults = array('cache' => LITHIUM_APP_PATH . '/resources/tmp/cache/templates', 'auto_reload' => !Environment::is('production'), 'base_template_class' => 'li3_twig\\template\\view\\adapter\\Template', 'autoescape' => false);
     parent::__construct($config + $defaults);
 }
All Usage Examples Of lithium\core\Environment::is