summaryrefslogtreecommitdiff
path: root/include/extension.h
blob: 72c7d561be5c266e8cc137e5318cde8b3fef50b5 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
 *  Extension.h
 *
 *  The extension class is the starting point of your PHP extension. This class
 *  is instantiated the moment the PHP engine starts - for example when the
 *  apache process starts - and will be used for all subsequent requests that
 *  are handled by Apache.
 *
 *  For some environments (for example CLI scripts and FastCGI calls) only one 
 *  request is handled by an extension instance, but for others (when PHP runs
 *  as module in a webserver) many requests are handled by the same extension
 *  instance.
 * 
 *  This is a template class. You need to pass in the type of an object
 *  that you use for storing request specific state information.
 * 
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */

/**
 *  Structures referenced in this class
 */
struct _zend_module_entry;

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

/**
 *  Forward declaration
 */
class Extension;

/**
 *  Optional callback types for starting and stopping the request
 *  @param  extension
 */
typedef bool    (*request_callback)(Extension *extension);

/**
 *  Class definition
 */
class Extension : public Namespace
{
public:
    /**
     *  Constructor that defines a number of functions right away
     *  @param  name        Extension name
     *  @param  version     Extension version string
     *  @param  callback    Function that is called when request starts
     *  @param  callback    Function that is called when request ends
     */
    Extension(const char *name = NULL, const char *version = NULL, request_callback start = NULL, request_callback stop = NULL);
    
    /**
     *  No copy'ing and no moving
     */
    Extension(const Extension &extension) = delete;
    Extension(Extension &&extension) = delete;
    
    /**
     *  Destructor
     */
    virtual ~Extension();
    
    /**
     *  Initialize the extension after it was registered
     * 
     *  You can override this method to add your own initialization code. The
     *  default implementation registers all classes and namespaces
     * 
     *  @return bool
     */
    virtual bool initialize()
    {
        // initialize the namespace
        Namespace::initialize("");
        
        // ok
        return true;
    }
    
    /**
     *  Finalize the extension after it was registered
     *
     *  You can override this method to do your own cleanup right before the
     *  extension object is going to be destructed
     * 
     *  @return bool
     */
    virtual bool finalize()
    {
        // ok
        return true;
    }

    /**
     *  Start a request
     * 
     *  This method is called when the zend engine is about to start a new
     *  request. Internally, it calls the request() method to instantiate
     *  a new request object, and after that it initializes the request.
     * 
     *  @return boolean
     */
    virtual bool startRequest()
    {
        // ok if no callback was set
        if (!_start) return true;
        
        // call the callback function
        return _start(this);
    }
    
    /**
     *  End a request
     * 
     *  This method is called when the Zend engine is ready with a request.
     *  Internally, it destructs the request
     *
     *  @return boolean
     */
    virtual bool endRequest()
    {
        // ok if no callback is set
        if (!_stop) return true;
        
        // call callback
        return _stop(this);
    }
    
    /**
     *  Retrieve the module entry
     * 
     *  This is the memory address that should be exported by the get_module()
     *  function.
     *
     *  @return _zend_module_entry
     */
    _zend_module_entry *module();
    
    /**
     *  Cast to a module entry
     *  @return _zend_module_entry*
     */
    operator _zend_module_entry * ()
    {
        return module();
    }
    
private:
    /**
     *  Initialize all functions in this namespace
     *  @param  ns          Namespace prefix
     *  @param  entries     The array to be filled
     *  @return int         Number of functions that were initialized
     */
    size_t initialize(const std::string &ns, zend_function_entry entries[]);

    /**
     *  The information that is passed to the Zend engine
     * 
     *  Although it would be slightly faster to not make this a pointer, this
     *  would require that client code also includes the PHP header files, which
     *  we try to prevent with the PHP-CPP library, so we allocate it dynamically.
     * 
     *  @var zend_module_entry
     */
    _zend_module_entry *_entry;
    
    /**
     *  Callback that is called before each request
     *  @var request_callback
     */
    request_callback _start;
    
    /**
     *  Callback that is called after each request
     *  @var request_callback
     */
    request_callback _stop;
    
};

/**
 *  End of namespace
 */
}