<?php

/*
 * This file is a part of Sculpin.
 *
 * (c) Dragonfly Development Inc.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sculpin\Core\SiteConfiguration;

use Dflydev\DotAccessConfiguration\Configuration;
use Dflydev\DotAccessConfiguration\YamlFileConfigurationBuilder;

/**
 * Site Configuration Factory.
 *
 * @author Beau Simensen <beau@dflydev.com>
 */
class SiteConfigurationFactory
{
    /**
     * Constructor.
     *
     * @param string $rootDir     Root directory
     * @param string $environment Environment
     */
    public function __construct($rootDir, $environment)
    {
        $this->rootDir = $rootDir;
        $this->environment = $environment;
    }

    /**
     * Create Site Configuration
     *
     * @return Configuration
     */
    public function create()
    {
        if (file_exists($file = $this->rootDir.'/config/sculpin_site_'.$this->environment.'.yml')) {
            $builder = new YamlFileConfigurationBuilder(array($file));

            return $builder->build();
        } elseif (file_exists($file = $this->rootDir.'/config/sculpin_site.yml')) {
            $builder = new YamlFileConfigurationBuilder(array($file));

            return $builder->build();
        }

        return new Configuration;
    }
}

<?php

/*
 * This file is a part of Sculpin.
 *
 * (c) Dragonfly Development Inc.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sculpin\Core\SiteConfiguration;

use Dflydev\DotAccessConfiguration\Configuration;
use Dflydev\DotAccessConfiguration\YamlFileConfigurationBuilder;

/**
 * Site Configuration Factory.
 *
 * @author Beau Simensen <beau@dflydev.com>
 */
class SiteConfigurationFactory
{
    /**
     * Constructor.
     *
     * @param string $rootDir     Root directory
     * @param string $environment Environment
     */
    public function __construct($rootDir, $environment)
    {
        $this->rootDir = $rootDir;
        $this->environment = $environment;
    }

    /**
     * Get an instance of the Configuration() class from the given file.
     *
     * @param  string $configFile
     * @return YamlFileConfigurationBuilder
     */
    private function getConfigFile($configFile)
    {
        $builder = new YamlFileConfigurationBuilder(array($configFile));
        $config = $builder->build();

        return $config;
    }

    /**
     * Create Site Configuration
     *
     * @return Configuration
     */
    public function create()
    {
        if (file_exists($file = $this->rootDir.'/config/sculpin_site_'.$this->environment.'.yml')) {
            $config = $this->getConfigFile($file);
        } elseif (file_exists($file = $this->rootDir.'/config/sculpin_site.yml')) {
            $config =  $this->getConfigFile($file);
        } else {
            $config = new Configuration();
        }

        $config->set('env', $this->environment);

        return $config;
    }
}

Add 'env' as a default variable available via {{ site.env }} in Twig templates.
Add 'env' as a default variable available via {{ site.env }} in Twig templates.

The environment is available while pulling in config files so this change
adds the environment to the 'site' variable.

I've tried to avoid code duplication by adding the getConfigFile() method,
but I'm not totally happy with the if .. elseif .. else block.
