Help us understand the problem. What is going on with this article?

Fuel::$envのConfigディレクトリ下にあるConfigを更新できなかった

More than 5 years have passed since last update.

Production, Staging, DevelopmentとConfigファイルを環境に合わせて分けています。
(app/config下に置いておくと開発環境の変更が他の環境にも適用されてしまうため)

ですが、Fuel::$envのConfigディレクトリ下にあるConfigを更新することができなくなりました。

事象を整理すると

1.読込はできる
 Fuel::$envが"production"で、app/config/hoge.phpとapp/config/production/hoge.phpがあった場合、app/config/production/hoge.phpがオーバーライドします。

2.書込み対象がapp/config下に
 Fuel::$envが"production"で、Config::save()した場合、app/config/hoge.phpが更新されてしまいます。app/config/hoge.phpがなくても勝手に作ってしまう。

上記を踏まえて、Fuel/Core/Config_Fileのソースコード読みました。
以下は一部抜粋

fuel/core/classes/config/file.php
    /**
     * Formats the output and saved it to disc.
     *
     * @param   $contents  $contents    config array to save
     * @return  bool       \File::update result
     */
    public function save($contents)
    {
        // get the formatted output
        $output = $this->export_format($contents);

        if ( ! $output)
        {
            return false;
        }

        if ( ! $path = \Finder::search('config', $this->file, $this->ext))
        {
            if ($pos = strripos($this->file, '::'))
            {
                // get the namespace path
                if ($path = \Autoloader::namespace_path('\\'.ucfirst(substr($this->file, 0, $pos))))
                {
                    // strip the namespace from the filename
                    $this->file = substr($this->file, $pos+2);

                    // strip the classes directory as we need the module root
                    $path = substr($path,0, -8).'config'.DS.$this->file;
                }
                else
                {
                    // invalid namespace requested
                    return false;
                }
            }
        }

        // absolute path requested?
        if ($this->file[0] === '/' or (isset($this->file[1]) and $this->file[1] === ':'))
        {
            $path = $this->file;
        }

        // make sure we have a fallback
        $path or $path = APPPATH.'config'.DS.$this->file.$this->ext;

        $path = pathinfo($path);
        if ( ! is_dir($path['dirname']))
        {
            mkdir($path['dirname'], 0777, true);
        }

        return \File::update($path['dirname'], $path['basename'], $output);
    }

Fuel::$envとか一切使ってないですね。
Autoloaderでオーバーライドしようと思いましたが、Fuel/Core/Config_Fileは抽象クラス。(ぐぬぬ・・・)
仕方がないのでFuel/Core/Config_Phpをオーバーライドすることにしました。

fuel/app/classes/core/config_php.php
<?php
/*
 * Copyright (c) 2013 Yasuyuki Sasaki
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
class Config_Php extends \Fuel\Core\Config_Php
{
    /**
     * Formats the output and saved it to disc.
     *
     * @param   $contents  $contents    config array to save
     * @return  bool       \File::update result
     */
    public function save($contents)
    {
        // get the formatted output
        $output = $this->export_format($contents);

        if ( ! $output)
        {
            return false;
        }

        // Fuel::$envから探しにいく
        $path = \Finder::search('config'.DS.Fuel::$env, $this->file, $this->ext);
        // なかったらapp/configから
        $path or $path = \Finder::search('config', $this->file, $this->ext);

        if (!$path)
        {
            if ($pos = strripos($this->file, '::'))
            {
                // get the namespace path
                if ($path = \Autoloader::namespace_path('\\'.ucfirst(substr($this->file, 0, $pos))))
                {
                    // strip the namespace from the filename
                    $this->file = substr($this->file, $pos+2);

                    // strip the classes directory as we need the module root
                    $path = substr($path,0, -8).'config'.DS.$this->file;
                }
                else
                {
                    // invalid namespace requested
                    return false;
                }
            }
        }

        // absolute path requested?
        if ($this->file[0] === '/' or (isset($this->file[1]) and $this->file[1] === ':'))
        {
            $path = $this->file;
        }

        // make sure we have a fallback
        $path or $path = APPPATH.'config'.DS.$this->file.$this->ext;

        $path = pathinfo($path);
        if ( ! is_dir($path['dirname']))
        {
            mkdir($path['dirname'], 0777, true);
        }
        return \File::update($path['dirname'], $path['basename'], $output);
    }
}

app/bootstrap.php
Autoloader::add_classes(array(
    'Config_Php'          => __DIR__.'/classes/core/config_php.php', // 追加
));

イケてない点が、jsonやymlも使いたい場合は、同じようにConfig_JsonやConfig_Ymlをオーバーライドしないといけない点。
本当はFuel/Core/Config_Fileを直してプルリクするのがいいんだろうけど、こんなんでほんとにいいのか自信ない。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away