1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: <?php
namespace Peridot\Console;
use Evenement\EventEmitterInterface;
use Peridot\Core\HasEventEmitterTrait;
use Peridot\Runner\Context;
/**
 * Environment is responsible for creating necessary objects and conditions
 * for Peridot to run. It creates the event emitter, input definition, and includes
 * user configuration from the Peridot configuration file.
 *
 * @package Peridot\Console
 */
class Environment
{
    use HasEventEmitterTrait;
    /**
     * @var InputDefinition
     */
    protected $definition;
    /**
     * Environment options
     *
     * @var array
     */
    protected $options;
    /**
     * @param InputDefinition $definition
     * @param EventEmitterInterface $emitter
     * @param array $options
     */
    public function __construct(
        InputDefinition $definition,
        EventEmitterInterface $emitter,
        array $options
    ) {
        $this->definition = $definition;
        $this->eventEmitter = $emitter;
        $this->options = $options;
        $this->initializeContext($emitter);
    }
    /**
     * Attempt to load a user configuration file into the Peridot
     * environment
     *
     * @param  string $configuration The default configuration path
     *
     * @return bool
     */
    public function load($configuration)
    {
        return $this->loadConfiguration($configuration);
    }
    /**
     * Return the InputDefinition used to define the available Peridot
     * options and arguments
     *
     * @return \Peridot\Console\InputDefinition
     */
    public function getDefinition()
    {
        return $this->definition;
    }
    /**
     * Load configuration
     *
     * @param $configuration
     * @return bool
     */
    protected function loadConfiguration($configuration)
    {
        if (! $this->wasGivenAConfigurationPath()) {
            return $this->includeConfiguration($configuration);
        }
        $files = array_filter(['c', 'configuration'], [$this, 'optionIsFile']);
        if (empty($files)) {
            return false;
        }
        return $this->includeConfiguration($this->options[array_pop($files)]);
    }
    /**
     * Determine if the environment option identified by $key
     * is a file.
     *
     * @param $key
     */
    protected function optionIsFile($key)
    {
        if (! array_key_exists($key, $this->options)) {
            return false;
        }
        return is_file($this->options[$key]);
    }
    /**
     * Include the configuration file used to setup the peridot
     * environment
     *
     * @param $configuration
     * @return bool
     */
    protected function includeConfiguration($configuration)
    {
        if (file_exists($configuration)) {
            $callable = include $configuration;
            if (is_callable($callable)) {
                call_user_func($callable, $this->eventEmitter);
            }
        }
        return true;
    }
    /**
     * Returns true if the Environment was given a configuration path.
     *
     * @return bool
     */
    protected function wasGivenAConfigurationPath()
    {
        return isset($this->options['c']) || isset($this->options['configuration']);
    }
    /**
     * Initialize the Context with the same event emitter as the Environment.
     *
     * @param EventEmitterInterface $emitter
     */
    protected function initializeContext(EventEmitterInterface $emitter)
    {
        Context::getInstance()->setEventEmitter($emitter);
    }
}