summaryrefslogtreecommitdiff
path: root/zend/constantfuncs.cpp
blob: c350f9119b338852adab8b117fc3b03ec7a0a12c (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
/**
 *  ConstantFuncs.cpp
 *
 *  C++ implementation of PHP functions to retrieve and set constants.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 Copernica BV
 */

/**
 *  Dependencies
 */
#include "includes.h"

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

/**
 *  Retrieve the value of a constant by its name
 *  @param  name            Name of the constant
 *  @return Value           Actual constant value
 */
Value constant(const char *name)
{
    // pass on to other implementation
    return constant(name, ::strlen(name));
}

/**
 *  Retrieve a constant by its name, and the size of the name
 *  @param  constant        Name of the constant
 *  @param  size            Size of the name
 *  @return Value
 */
Value constant(const char *constant, size_t size)
{
    // we need the tsrm_ls variable
    TSRMLS_FETCH();

    // the value that holds the result
    Value result;

    // retrieve the constant
    if (!zend_get_constant(constant, size, result._val TSRMLS_CC)) return nullptr;
    
    // zval was correctly retrieved, wrap in value
    return result;
}

/**
 *  Retrieve the value of a constant by its name
 *  @param  name            Name of the constant
 *  @return Value           Actual constant value
 */
Value constant(const std::string &name)
{
    // pass on to other implementation
    return constant(name.c_str(), name.size());
}

/**
 *  Check whether a constant exists
 *  @param  name
 *  @param  size
 *  @return bool
 */
bool defined(const char *name, size_t size) 
{
    // we need the tsrm_ls variable
    TSRMLS_FETCH();

    // result variable
    zval c;

    // retrieve the constant
    if (!zend_get_constant_ex(name, size, &c, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) return false;

    // constant exists, but the returned zval should first be destructed
    zval_dtor(&c);
    
    // done
    return true;
}

/**
 *  Check whether a constant exists
 *  @param  name
 *  @return bool
 */
bool defined(const char *name)
{
    // pass on
    return defined(name, ::strlen(name));
}

/**
 *  Check whether a constant exists
 *  @param  name
 *  @return bool
 */
bool defined(const std::string &name)
{
    // pass on
    return defined(name.c_str(), name.size());
}

/**
 *  End namespace
 */
}