summaryrefslogtreecommitdiff
path: root/include/ini.h
blob: a1899a4cbcf979bde90b28206cd7ceac7ced0a22 (plain)
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
/**
 *  Ini.h
 *
 *  Class that can be instantiated in the get_module() startup function to
 *  define settings from the php.ini file that are supported.
 *
 *  @copyright 2014 Copernica BV
 */

/**
 *  Forward declarations
 */
struct _zend_ini_entry;

/**
 *  Set up namespace
 */
namespace Php {


/**
 *  Class definition
 */
class PHPCPP_EXPORT Ini
{
public:
    /**
     *  Supported place-types for ini setting
     *
     *  The possible settings for where the configuration can be changed are:
     *  PHP_INI_USER, PHP_INI_PERDIR, PHP_INI_SYSTEM and PHP_INI_ALL
     *
     *  Usually you would choose where the setting can be changed based on how
     *  it is used. For example if you want to access the setting during RINIT
     *  stage then you would want PHP_INI_PERDIR because the setting would have
     *  no use after RINIT.
     */
    enum Place : int {
        User      = (1<<0),                         // ZEND_INI_USER   (1<<0)
        Perdir    = (1<<1),                         // ZEND_INI_PERDIR (1<<1)
        System    = (1<<2),                         // ZEND_INI_SYSTEM (1<<2)
        All       = (1<<0) | (1<<1) | (1<<2)        // ZEND_INI_ALL  (ZEND_INI_USER|ZEND_INI_PERDIR|ZEND_INI_SYSTEM)
    };

    /**
     *  Constructors for string values
     *
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, const char *value, const Place place = Place::All) :
        _name(name), _value(value), _place(place) {}

    /**
     *  Constructors for bool values
     *
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, bool value, const Place place = Place::All) :
        _name(name), _value(bool2str(value)), _place(place) {}

    /**
     *  Constructors for integer values
     *
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, const int16_t value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _place(place) {}

    Ini(const char *name, const int32_t value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _place(place) {}

    Ini(const char *name, const int64_t value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _place(place) {}

    /**
     *  Constructors for floating point values
     *
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, const double value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _place(place) {}


    /**
     *  Filling ini_entries
     *  @param  ini_entry
     *  @param  module_number
     */
    void fill(struct _zend_ini_entry_def *ini_entry, int module_number);


private:
    /**
     *  Helper function to convert a boolean to a string
     *  @param  value
     *  @return string
     */
#ifdef _MSC_VER
    static const char* bool2str(const bool value)
#else
    static constexpr const char* bool2str(const bool value)
#endif
    {
        // cast to a string
        return (  static_cast<bool>(value) ? "On" : "Off");
    }

    /**
     *  ini entry name
     *  @var    std::string
     */
    std::string _name;

    /**
     *  ini entry value
     *  @var    std::string
     */
    std::string _value;

    /**
     *  Place where the configuration can be changed
     *  @var    Place
     */
    Place _place;


};

/**
 *  End of namespace
 */
}