IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Développement d'une extension PHP


Sujet :

C

  1. #1
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    507
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 507
    Points : 359
    Points
    359
    Par défaut Développement d'une extension PHP
    Bonjour,
    je suis en train de developper un generateur d'extension php en python à partir d'un fichier gabarit XML.
    j'aimerais simplement savoir si je suis sur la bonne voie pour créer mon extension, la question ne porte pas sur le generateur python mais sur les fichiers d'extensions php que je genere :
    il y en a 2 que je genere :

    - module.c
    - php_module.h

    ma question porte d'abord sur le fichier php_module.h, je voudrais savoir si il est correctement généré (honetement j'ai du mal a comprendre les fonctions de l'API ZEND) je me suis basé sur different tutos et jai aussi regardé les differentes extensions deja intégré à PHP, voici mon code généré :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifndef FREEAGE_MODULE_MODULE_INCLUDED
    #define FREEAGE_MODULE_MODULE_INCLUDED
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    #define PHP_MODULE_EXTNAME	"module"
    #define PHP_MODULE_EXTVER	"1.0"
     
    #ifdef ZTS
    #include "TSRM.h"
    #endif
     
    #include "net_types.h"
    #include <stdarg.h>
     
    /* Registration module inputs.	*/
    extern zend_module_entry module_module_entry;
    #define phpext_module_ptr &module_module_entry
     
    /* Declaration of functions to create.	*/
    PHP_MINIT_FUNCTION(module);
    PHP_MINFO_FUNCTION(module);
    PHP_MSHUTDOWN_FUNCTION(module);
     
    PHP_FUNCTION(test_module);
    PHP_FUNCTION(test_module_result);
    PHP_FUNCTION(test_sql);
    PHP_FUNCTION(test_sql_result);
     
    ZEND_BEGIN_MODULE_GLOBALS(module)
     
    /* test_module params declaration.	*/
    typedef struct
    {
    	net_int_t arg;	/*< An argument.	*/
    }
    test_module_params;
     
    /* test_module result declaration.	*/
    typedef struct
    {
    	net_int_t ret;	/*< A result.	*/
    }
    test_module_result;
     
    /* test_sql params declaration.	*/
    typedef struct
    {
    	net_char_t name [32];	/*< The test argument.	*/
    }
    test_sql_params;
     
    /* test_sql result declaration.	*/
    typedef struct
    {
    	net_int_t intval;	/*< An integer.	*/
    	net_float_t floatval;	/*< A float.	*/
    	net_char_t strval [32];	/*< A string.	*/
    }
    test_sql_result;
     
    ZEND_END_MODULE_GLOBALS(module)
     
    #ifdef ZTS
    #define MODULE_G(v) TSRMG(module_globals_id, zend_module_globals *, v)
    #else
    #define MODULE_G(v) (module_globals.v)
    #endif
     
    ZEND_EXTERN_MODULE_GLOBALS(module)
     
    #ifdef __cplusplus
    }
    #endif
     
    #endif /* FREEAGE_MODULE_MODULE_INCLUDED */
    avant de m'attaquer a la generation du fichier d'implementation module.c, ce fichier deja est t'il bon ? y a t'il des erreurs ?

    j'aimerais aussi savoir quel type en PHP remplace ce type de structure en C :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    typedef struct
    {
    	net_int_t arg;	/*< An argument.	*/
    }
    test_module_params;
    ou tout autre structure d'ailleurs ! sachant que les types en PHP sont :

    Boolean, Long, Double, String, Resources, Array, Object.

    merci d'avance.

  2. #2
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    507
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 507
    Points : 359
    Points
    359
    Par défaut
    Je continue de galerer mais j'avance un peu voici mon header :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifndef FREEAGE_MODULE_MODULE_INCLUDED
    #define FREEAGE_MODULE_MODULE_INCLUDED
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    #define PHP_MODULE_EXTNAME    "module"
    #define PHP_MODULE_EXTVER    "1.0"
     
    #ifdef ZTS
    #include "TSRM.h"
    #endif
     
    #include "net_types.h"
    #include <stdarg.h>
     
    /* Registration module inputs.    */
    extern zend_module_entry module_module_entry;
    #define phpext_module_ptr &module_module_entry
     
    /* Declaration of functions to create.    */
    PHP_MINIT_FUNCTION(module);
    PHP_MINFO_FUNCTION(module);
    PHP_MSHUTDOWN_FUNCTION(module);
     
    PHP_FUNCTION(test_module);
    PHP_FUNCTION(test_module_result);
    PHP_FUNCTION(test_sql);
    PHP_FUNCTION(test_sql_result);
     
    ZEND_BEGIN_MODULE_GLOBALS(module)
     
    /* test_module result declaration.    */
    typedef struct
    {
        net_int_t ret;    /*< A result.    */
    }
    test_module_result;
     
    /* test_sql result declaration.    */
    typedef struct
    {
        net_int_t intval;    /*< An integer.    */
        net_float_t floatval;    /*< A float.    */
        net_char_t strval [32];    /*< A string.    */
    }
    test_sql_result;
     
    ZEND_END_MODULE_GLOBALS(module)
     
    #ifdef ZTS
    #define MODULE_G(v) TSRMG(module_globals_id, zend_module_globals *, v)
    #else
    #define MODULE_G(v) (module_globals.v)
    #endif
     
    ZEND_EXTERN_MODULE_GLOBALS(module)
     
    #ifdef __cplusplus
    }
    #endif
     
    #endif /* FREEAGE_MODULE_MODULE_INCLUDED */
    je crois qu'il est tout bon, a part mes types perso 'net_int_t', 'net_char_t', je ne sais pas encore si je peux les laisser , en tout cas, ils sont definis dans net_type.h.

    mon fichier c maintenant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
     
    #if HAVE_MODULE
     
    #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif
     
    #include "php.h"
    #include "php_ini.h"
    #include "php_module.h"
    #include "net_endian.h"
    #include "net_struct.h"
    #include "net_func.h"
    #include "net_client.h"
    #include <stddef.h>
    #include <string.h>
     
    /* Arginfo    */
    ZEND_BEGIN_ARG_INFO_EX(arginfo_test_module)
    ZEND_END_ARG_INFO()
     
    ZEND_BEGIN_ARG_INFO_EX(arginfo_test_module_result)
    ZEND_END_ARG_INFO()
     
    ZEND_BEGIN_ARG_INFO_EX(arginfo_test_sql)
    ZEND_END_ARG_INFO()
     
    ZEND_BEGIN_ARG_INFO_EX(arginfo_test_sql_result)
    ZEND_END_ARG_INFO()
     
    /* Initialise global variables    */
    ZEN_DECLARE_MODULE_GLOBALS(module)
     
    static void php_module_init_globals (zend_module_globals * module_globals)
    {
        module_globals->test_module_result->ret = 0;
        module_globals->test_sql_result->intval = 0;
        module_globals->test_sql_result->floatval = 0;
        module_globals->test_sql_result->strval = NULL;
    }
     
    /* Every user visible function must have an entry in this function.    */
    const zend_function_entry module_functions [] = 
    {
        PHP_FE(test_module, arginfo_test_module)
        PHP_FE(test_module_result, arginfo_test_module_result)
        PHP_FE(test_sql, arginfo_test_sql)
        PHP_FE(test_sql_result, arginfo_test_sql_result)
        {NULL, NULL, NULL}
    };
     
    /* Information module.    */
    zend_module_entry module_module_entry =
    {
        #if ZEND_MODULE_API_NO >= 20010901
        STANDARD_MODULE_HEADER,
        #endif
        PHP_MODULE_EXTNAME,
        module_functions,        /* Functions    */
        PHP_MINIT(module),        /* MINIT        */
        PHP_MSHUTDOWN(module),    /* MSHUTDOWN    */
        NULL,                    /* RINIT        */
        NULL,                    /* RSHUTDOWN    */
        PHP_MINFO(module),        /* MINFO        */
        #if ZEND_MODULE_API_NO >= 20010901
        PHP_MODULE_EXTVER,
        #endif
        STANDARD_MODULE_PROPERTIES
    };
     
    #ifdef COMPILE_DL_MODULE
    ZEND_GET_MODULE(module)
    #endif
     
    /* Function to add php_info extension information.    */
    PHP_MINFO_FUNCTION(module)
    {
        php_info_print_table_start();
        php_info_print_table_row(2, "module Module", "enabled");
        php_info_print_table_row(2, "version", PHP_MODULE_EXTVER);
        php_info_print_table_end();
        return SUCCESS;
    }
     
    PHP_MINIT_FUNCTION(module)
    {
        #ifdef ZTS
            ZEND_INIT_MODULE_GLOBALS(module, php_module_init_globals, NULL);
        #endif
        return SUCCESS;
    }
     
    PHP_MSHUTDOWN_FUNCTION(module)
    {
        return SUCCESS;
    }
     
    struct net_func_instance;
    struct net_func_callbacks;
    struct net_client;
     
    /* test_module params formatting function.    */
    /*
     * \param target    A pointer to the structure.
     * \param ap    A list of parameters.
     */
    void
    test_module_params_format_fn ( struct test_module_params * target, va_list ap );
     
    /* test_module params byte swapping function.    */
    /*
     * This function byte swap the structure in-place.
     * \param target    A pointer to the structure.
     */
    void
    test_module_params_bswap_fn ( struct test_module_params * target );
     
    /* test_module params format definition.    */
    static const struct net_struct test_module_params_format =
    {
        1, sizeof(struct test_module_params),
        NULL, /* not used by client */
        (net_struct_format_fn_t)test_module_params_format_fn,
        (net_struct_bswap_fn_t)test_module_params_bswap_fn,
        {
            { NET_TYPE_INT, 1, offsetof(struct test_module_params, arg) }
        }
    };
     
    void
    test_module_params_format_fn ( struct test_module_params * target, va_list ap )
    {
        {
            target->arg = va_arg(ap, net_int_t);
        }
    }
     
    void
    test_module_params_bswap_fn ( struct test_module_params * target )
    {
        net_bswap_int(&target->arg);
    }
     
    /* test_module result byte swapping function.    */
    /*
     * This function byte swap the structure in-place.
     * \param target    A pointer to the structure.
     */
    void
    test_module_result_bswap_fn ( struct test_module_result * target );
     
    /* test_module result format definition.    */
    static const struct net_struct test_module_result_format =
    {
        1, sizeof(struct test_module_result),
        NULL, /* not used by client */
        NULL, /* not used for results */
        (net_struct_bswap_fn_t)test_module_result_bswap_fn,
        {
            { NET_TYPE_INT, 1, offsetof(struct test_module_result, ret) }
        }
    };
     
    void
    test_module_result_bswap_fn ( struct test_module_result * target )
    {
        net_bswap_int(&target->ret);
    }
     
    /* test_module function definition.    */
    static const struct net_func test_module_func_def =
    {
        1,
        0,
        &test_module_params_format,
        &test_module_result_format
    };
     
    /* struct net_func_instance *
    test_module ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_int_t arg ) */
    PHP_FUNCTION(test_module)
    {
        struct net_client * client;
        const struct net_func_callbacks * callbacks;
        const net_int_t arg;
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrl", &client, &callbacks, &arg) == FAILURE)
        {
            RETURN_FALSE();
        }
        return net_client_call_function(client, &test_module_func_def, callbacks, arg);
    }
     
    /* const struct test_module_result *
    test_module_result ( struct net_func_instance * instance ) */
    PHP_FUNCTION(test_module_result)
    {
        test_module_result * ptr_test_module;
        zval * instance;
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &instance) == FAILURE)
        {
            RETURN_FALSE();
        }
        ptr_test_module = (test_module_result *)net_func_pop_result(instance);
        ZEND_REGISTER_RESOURCE(return_value, ptr_test_module, le_test_module);
    }
     
     
    /* test_sql params formatting function.    */
    /*
     * \param target    A pointer to the structure.
     * \param ap    A list of parameters.
     */
    void
    test_sql_params_format_fn ( struct test_sql_params * target, va_list ap );
     
    /* test_sql params byte swapping function.    */
    /*
     * This function byte swap the structure in-place.
     * \param target    A pointer to the structure.
     */
    void
    test_sql_params_bswap_fn ( struct test_sql_params * target );
     
    /* test_sql params format definition.    */
    static const struct net_struct test_sql_params_format =
    {
        1, sizeof(struct test_sql_params),
        NULL, /* not used by client */
        (net_struct_format_fn_t)test_sql_params_format_fn,
        (net_struct_bswap_fn_t)test_sql_params_bswap_fn,
        {
            { NET_TYPE_CHAR, 32, offsetof(struct test_sql_params, name) }
        }
    };
     
    void
    test_sql_params_format_fn ( struct test_sql_params * target, va_list ap )
    {
        {
            const char * source = va_arg(ap, const char *);
            strncpy(target->name, source, 32);
        }
    }
     
    void
    test_sql_params_bswap_fn ( struct test_sql_params * target )
    {
    }
     
    /* test_sql result byte swapping function.    */
    /*
     * This function byte swap the structure in-place.
     * \param target    A pointer to the structure.
     */
    void
    test_sql_result_bswap_fn ( struct test_sql_result * target );
     
    /* test_sql result format definition.    */
    static const struct net_struct test_sql_result_format =
    {
        3, sizeof(struct test_sql_result),
        NULL, /* not used by client */
        NULL, /* not used for results */
        (net_struct_bswap_fn_t)test_sql_result_bswap_fn,
        {
            { NET_TYPE_INT, 1, offsetof(struct test_sql_result, intval) },
            { NET_TYPE_FLOAT, 1, offsetof(struct test_sql_result, floatval) },
            { NET_TYPE_CHAR, 32, offsetof(struct test_sql_result, strval) }
        }
    };
     
    void
    test_sql_result_bswap_fn ( struct test_sql_result * target )
    {
        net_bswap_int(&target->intval);
        net_bswap_float(&target->floatval);
    }
     
    /* test_sql function definition.    */
    static const struct net_func test_sql_func_def =
    {
        1,
        1,
        &test_sql_params_format,
        &test_sql_result_format
    };
     
    /* struct net_func_instance *
    test_sql ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_char_t name [32] ) */
    PHP_FUNCTION(test_sql)
    {
        struct net_client * client;
        const struct net_func_callbacks * callbacks;
        const net_char_t name [32];
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrs", &client, &callbacks, &name) == FAILURE)
        {
            RETURN_FALSE();
        }
        return net_client_call_function(client, &test_sql_func_def, callbacks, name);
    }
     
    /* const struct test_sql_result *
    test_sql_result ( struct net_func_instance * instance ) */
    PHP_FUNCTION(test_sql_result)
    {
        test_sql_result * ptr_test_sql;
        zval * instance;
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &instance) == FAILURE)
        {
            RETURN_FALSE();
        }
        ptr_test_sql = (test_sql_result *)net_func_pop_result(instance);
        ZEND_REGISTER_RESOURCE(return_value, ptr_test_sql, le_test_sql);
    }
     
     
    #endif
    ici c'est plus complexe, j'ai déclaré donc 2 structures globales dans le header et je les initialise a l'appel de la fonction MINIT_FUNCTION.
    - Est ce que je dois laisser les lignes 'arginfo', pour defnir les arguments de chaque fonctions ?
    - mes pointeurs de structures dans les fonctions php peuvent ils etre laissé comme ca, est ce que je dois utiliser plutot zval * ?

    merci d'avance

Discussions similaires

  1. [PHP 5.3] La création d'une extension PHP
    Par Xavier.vdw dans le forum Langage
    Réponses: 0
    Dernier message: 23/08/2010, 17h47
  2. [MySQL] Installer une extension PHP
    Par zesavantfou dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 17/03/2009, 17h23
  3. Ajout d'une extension PHP
    Par Shin_RockmanX dans le forum IIS
    Réponses: 0
    Dernier message: 12/08/2008, 21h11
  4. Développement d'une extension php liée à ocilib
    Par grincheux01 dans le forum Visual C++
    Réponses: 4
    Dernier message: 22/02/2008, 18h55
  5. charger une extension php sur un hébergeur mutualisé (php 4)
    Par benjamin raspel dans le forum Bibliothèques et frameworks
    Réponses: 9
    Dernier message: 07/11/2007, 17h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo