summaryrefslogtreecommitdiff
path: root/src/namespace.cpp
blob: 8dda791ee6ba4c3361cb33f02ffe4f836be60c8b (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
/**
 *  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
 */
void 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));
}

/**
 *  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
 */
void 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));
}

/**
 *  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
 */
void 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));
}

/**
 *  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
 */
void 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));
}

/**
 *  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;
}

/**
 *  End namespace
 */
}