summaryrefslogtreecommitdiff
path: root/src/namespace.cpp
blob: d462684225acde7194e67db94fcff92f0f4ad09e (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
/**
 *  Namespace.cpp
 *
 *  Implementation of the namespace class
 * 
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 Copernica BV
 */
#include "includes.h"

/**
 *  Open namespace
 */
namespace Php {

/**
 *  Add a native function directly to the extension
 *  @param  name        Name of the function
 *  @param  function    The function to add
 *  @param  arguments   Optional argument specification
 *  @return Namespace   Same object to allow chaining
 */
Namespace &Namespace::add(const char *name, const native_callback_0 &function, const Arguments &arguments)
{
    // add a function
    _functions.push_back(std::make_shared<Function>(name, function, arguments));
    
    // allow chaining
    return *this;
}

/**
 *  Add a native function directly to the extension
 *  @param  name        Name of the function
 *  @param  function    The function to add
 *  @param  arguments   Optional argument specification
 *  @return Namespace   Same object to allow chaining
 */
Namespace &Namespace::add(const char *name, const native_callback_1 &function, const Arguments &arguments)
{
    // add a function
    _functions.push_back(std::make_shared<Function>(name, function, arguments));

    // allow chaining
    return *this;
}

/**
 *  Add a native function directly to the extension
 *  @param  name        Name of the function
 *  @param  function    The function to add
 *  @param  arguments   Optional argument specification
 *  @return Namespace   Same object to allow chaining
 */
Namespace &Namespace::add(const char *name, const native_callback_2 &function, const Arguments &arguments)
{
    // add a function
    _functions.push_back(std::make_shared<Function>(name, function, arguments));

    // allow chaining
    return *this;
}

/**
 *  Add a native function directly to the extension
 *  @param  name        Name of the function
 *  @param  function    The function to add
 *  @param  arguments   Optional argument specification
 *  @return Namespace   Same object to allow chaining
 */
Namespace &Namespace::add(const char *name, const native_callback_3 &function, const Arguments &arguments)
{
    // add a function
    _functions.push_back(std::make_shared<Function>(name, function, arguments));

    // allow chaining
    return *this;
}

/**
 *  Initialize all functions in this namespace
 *  @param  parent      Namespace prefix of the parent
 *  @param  entries     The array to be filled
 *  @return int         Number of functions that were initialized
 */
size_t Namespace::initialize(const std::string &parent, struct _zend_function_entry entries[])
{
    // keep iterator counter
    int count = 0;
    
    // the namespace to use
    std::string prefix = parent.size() ? parent + "\\" + _name : _name;

    // loop through the functions
    for (auto &function : _functions)
    {
        // retrieve entry
        zend_function_entry *entry = &entries[count++];

        // let the function fill the entry
        function->initialize(prefix, entry);
    }
    
    // loop through the namespace
    for (auto &ns : _namespaces)
    {
        // let the namespace initialize
        count += ns->initialize(prefix, &entries[count]);
    }
    
    // done
    return count;
}

/**
 *  Initialize the namespace after it was registered
 *  @param  parent      Parent namespace
 *  @param  tsrm_ls
 */
void Namespace::initialize(const std::string &parent TSRMLS_DC)
{
    // the namespace to use
    std::string prefix = parent.size() ? parent + "\\" + _name : _name;
    
    // loop through the classes in this namespace
    for (auto &c : _classes) c->implementation()->initialize(c.get(), prefix TSRMLS_CC);
    
    // and loop through the other namespaces
    for (auto &n : _namespaces) n->initialize(prefix TSRMLS_CC);
}


/**
 *  End namespace
 */
}