summaryrefslogtreecommitdiff
path: root/include/environment.h
blob: a7512d7722ab25a53004abf5e6fb8ee42ce40332 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 *  Environment.h
 *
 *  During the lifetime of the extension, multiple requests can be handled
 *  by it. For every request that is handled, an environment object is created.
 *
 *  The base class for the environment is defined in this file. If you'd like
 *  to add state variables to the environment you can override this class and
 *  add the extra features you'd like. If you override this method, you should
 *  also override Extension::createEnvironment() to return an instance of a 
 *  different class.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */
 
/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Forward definitions
 */
class Extension;
class Global;

/**
 *  Class definition
 */
class Environment
{
public:
    /**
     *  Constructor
     *  @param  extension
     */
    Environment(Extension *extension) : _extension(extension) {}
    
    /**
     *  Disable copy and move operations
     */
    Environment(const Environment &environment) = delete;
    Environment(Environment &&environment) = delete;
    
    /**
     *  Destructor
     */
    virtual ~Environment() {}
    
    /**
     *  Initialize the request
     * 
     *  This method is called directly after the object was destructed. You can
     *  override this method to add your own initialization code.
     * 
     *  @return bool
     */
    virtual bool initialize()
    {
        return true;
    }
    
    /**
     *  Finalize the request
     * 
     *  This method is called right before the object is destructed. Note that
     *  the object is going to be destructed anyway, even if this method returns
     *  false
     * 
     *  @return bool
     */
    virtual bool finalize()
    {
        return true;
    }
    
    /**
     *  Get access to the user supplied data
     *  @return void*
     */
    virtual void *data()
    {
        return _data;
    }
    
    /**
     *  Change the user supplied data
     *  @param  data
     */
    virtual void setData(void *data)
    {
        _data = data;
    }
    
    /**
     *  Get access to a global variable
     *  @param  name
     *  @return Global
     */
    Global operator[](const char *name);
    
    /**
     *  Get access to a global variable
     *  @param  name
     *  @return Global
     */
    Global operator[](const std::string &name);
    
    /**
     *  Call a function in PHP
     *  We have ten variants of this function, depending on the number of parameters
     *  @param  name        Name of the function
     *  @return Value
     */
    Value call(const Value &name);
    Value call(const Value &name, Value p0);
    Value call(const Value &name, Value p0, Value p1);
    Value call(const Value &name, Value p0, Value p1, Value p2);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8);
    Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9);

private:
    /**
     *  Call function with a number of parameters
     *  @param  name        Function name
     *  @param  argc        Number of parameters
     *  @param  argv        The parameters
     *  @return Value
     */
    Value exec(const Value &name, int argc, struct _zval_struct ***params);

protected:
    /**
     *  The extension that this environment belongs to
     *  @var Extension*
     */
    Extension *_extension;
    
    /**
     *  Pointer to user supplied data
     *  @var void*
     */
    void *_data = NULL;
};

/**
 *  End of namespace
 */
}