Conf::getIni()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 28
rs 8.439
cc 5
eloc 14
nc 7
nop 2
1
<?php
2
/**
3
 * Conf
4
 *
5
 * コンフィグ操作のためのファンクション群
6
 *
7
 * @package           risoluto
8
 * @author            Risoluto Developers
9
 * @license           http://opensource.org/licenses/bsd-license.php new BSD license
10
 * @copyright     (C) 2008-2015 Risoluto Developers / All Rights Reserved.
11
 */
12
13
//------------------------------------------------------//
14
// 名前空間の定義
15
//------------------------------------------------------//
16
namespace Risoluto;
17
18
//------------------------------------------------------//
19
// クラス定義
20
//------------------------------------------------------//
21
class Conf
22
{
23
    //------------------------------------------------------//
24
    // クラス変数定義
25
    //------------------------------------------------------//
26
    /**
27
     * $parsedconf
28
     * @access private
29
     * @var    array    パース済みコンフィグ情報
30
     */
31
    private $parsedconf = [ ];
32
33
    /**
34
     * $parsestatus
35
     * @access private
36
     * @var    boolean    パース状況
37
     */
38
    private $parsestatus = false;
39
40
    //------------------------------------------------------//
41
    // クラスメソッド定義
42
    //------------------------------------------------------//
43
    /**
44
     * getParseStatus()
45
     *
46
     * パース状況を取得する
47
     *
48
     * @access    public
49
     *
50
     * @param     void
51
     *
52
     * @return    boolean パース状況(true:パース済み/false:未パース)
53
     */
54
    public function getParseStatus()
55
    {
56
        return $this->parsestatus;
57
    }
58
59
    /**
60
     * parse($path)
61
     *
62
     * 引数で与えられたパスよりiniファイルを読み込みパースする
63
     *
64
     * @access    public
65
     *
66
     * @param     string $path iniファイルのパス
67
     *
68
     * @return    boolean    パース結果(true:正常終了/false:異常終了)
69
     */
70
    public function parse( $path )
71
    {
72
        // ファイルが存在しているかをテスト
73
        clearstatcache();
74
        if (is_file( $path )) {
75
            // ファイルが存在していれば、
76
            // 指定されたiniファイルをロードし、パースする
77
            $this->parsedconf = parse_ini_file( $path, true );
78
            $this->parsestatus = true;
79
80
            return true;
81
        } else {
82
            // ファイルが存在しない場合(または読めない場合)
83
            // クラス変数に未パースである旨をセットする
84
            $this->parsestatus = false;
85
86
            return false;
87
        }
88
    }
89
90
    /**
91
     * getIni($section = '', $key = '')
92
     *
93
     * パース済みiniファイルより、セクションのみが指定された場合はセクション内すべての値を配列を、
94
     * キーが指定された場合はキーが持つ値を返却する
95
     * セクションもキーも指定されていない場合は全体を返却する
96
     * パースされていない場合やセクション、キーが存在しない場合は、nullが返却される
97
     *
98
     * @access    public
99
     *
100
     * @param     string $section 検索対象のセクション
101
     * @param     string $key 検索対象のキー
102
     *
103
     * @return    mixed    セクションに対応する配列、またはキーに対応する値。どちらも存在しない場合はnull
104
     */
105
    public function getIni( $section = '', $key = '' )
106
    {
107
        // 一度もパースされていない場合は、nullを返す
108
        if (!$this->parsestatus) {
109
            return null;
110
        }
111
112
        // セクションが指定されている場合
113
        if (!empty( $section )) {
114
            // キーが指定されていればキーに対応する値を取得
115
            if (!empty( $key )) {
116
                $gotDat = $this->parsedconf[ $section ][ $key ];
117
                // キーが指定されていなければセクション内すべての値を取得
118
            } else {
119
                $gotDat = $this->parsedconf[ $section ];
120
            }
121
            // セクションが指定されていない場合はparseしたコンフィグ丸ごと取得
122
        } else {
123
            $gotDat = $this->parsedconf;
124
        }
125
126
        // 取得する値があればそのまま返却し、無ければnullを返却
127
        if (!empty( $gotDat )) {
128
            return $gotDat;
129
        } else {
130
            return null;
131
        }
132
    }
133
}
134