intecture-api 0.3.2

API component for Intecture infrastructure. Intecture is the developer friendly, multi-lingual configuration management tool.
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 Copyright 2015-2017 Intecture Developers. See the COPYRIGHT file at the
 top-level directory of this distribution and at
 https://intecture.io/COPYRIGHT.

 Licensed under the Mozilla Public License 2.0 <LICENSE or
 https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
 modified, or distributed except according to those terms.
*/

#include "php_inapi.h"
#include "src/command.h"
#include "src/directory.h"
#include "src/file.h"
#include "src/host.h"
#include "src/package.h"
#include "src/payload.h"
#include "src/service.h"
#include "src/template.h"
#include <zend_exceptions.h>

zend_class_entry *inapi_ce_command, *inapi_ce_command_ex,
                 *inapi_ce_directory, *inapi_ce_directory_ex,
                 *inapi_ce_file, *inapi_ce_file_ex,
                 *inapi_ce_host, *inapi_ce_host_ex,
                 *inapi_ce_package, *inapi_ce_package_ex,
                 *inapi_ce_payload, *inapi_ce_payload_ex,
                 *inapi_ce_service, *inapi_ce_service_ex, *inapi_ce_service_runnable,
                 *inapi_ce_template, *inapi_ce_template_ex;

zend_object_handlers inapi_command_handlers,
                     inapi_directory_handlers,
                     inapi_file_handlers,
                     inapi_host_handlers,
                     inapi_package_handlers,
                     inapi_payload_handlers,
                     inapi_service_handlers, inapi_service_runnable_handlers,
                     inapi_template_handlers;

/*
 * Command
 */
static zend_function_entry command_methods[] = {
  PHP_ME(Command, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  PHP_ME(Command, exec, NULL, ZEND_ACC_PUBLIC)
  {NULL, NULL, NULL}
};

zend_object *inapi_command_create(zend_class_entry *ce) {
    php_command *intern = ecalloc(1, sizeof(php_command) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_command_handlers;

    return &intern->std;
}

void inapi_command_free(zend_object *object TSRMLS_DC) {
    php_command *command = (php_command *)((char *)object - XtOffsetOf(php_command, std));
    if (command->command) {
        int rc = command_free(command->command);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

/*
 * Directory
 */
static zend_function_entry directory_methods[] = {
    PHP_ME(Directory, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Directory, exists, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, create, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, delete, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, mv, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, get_owner, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, set_owner, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, get_mode, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Directory, set_mode, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

zend_object *inapi_directory_create(zend_class_entry *ce) {
    php_directory *intern = ecalloc(1, sizeof(php_directory) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_directory_handlers;

    return &intern->std;
}

void inapi_directory_free(zend_object *object TSRMLS_DC) {
    php_directory *directory = (php_directory *)((char *)object - XtOffsetOf(php_directory, std));
    if (directory->directory) {
        int rc = directory_free(directory->directory);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

/*
 * File
 */
 static zend_function_entry file_methods[] = {
     PHP_ME(File, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
     PHP_ME(File, exists, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, upload, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, upload_file, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, delete, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, mv, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, copy, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, get_owner, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, set_owner, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, get_mode, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(File, set_mode, NULL, ZEND_ACC_PUBLIC)
     {NULL, NULL, NULL}
 };

zend_object *inapi_file_create(zend_class_entry *ce) {
    php_file *intern = ecalloc(1, sizeof(php_file) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_file_handlers;

    return &intern->std;
}

void inapi_file_free(zend_object *object TSRMLS_DC) {
    php_file *file = (php_file *)((char *)object - XtOffsetOf(php_file, std));
    if (file->file) {
        int rc = file_free(file->file);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

/*
 * Host
 */
static zend_function_entry host_methods[] = {
  PHP_ME(Host, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
  PHP_ME(Host, connect, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  PHP_ME(Host, connect_endpoint, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  PHP_ME(Host, connect_payload, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  PHP_ME(Host, data, NULL, ZEND_ACC_PUBLIC)
  {NULL, NULL, NULL}
};

zend_object *inapi_host_create(zend_class_entry *ce) {
    php_host *intern = ecalloc(1, sizeof(php_host) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_host_handlers;

    return &intern->std;
}

void inapi_host_free(zend_object *object TSRMLS_DC) {
    php_host *host = (php_host *)((char *)object - XtOffsetOf(php_host, std));
    host_close(host->host);
    zval_dtor(&host->data);
    zend_object_std_dtor(object);
}

/*
 * Package
 */
static zend_function_entry package_methods[] = {
    PHP_ME(Package, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Package, is_installed, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Package, install, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Package, uninstall, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

zend_object *inapi_package_create(zend_class_entry *ce) {
    php_package *intern = ecalloc(1, sizeof(php_package) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_package_handlers;

    return &intern->std;
}

void inapi_package_free(zend_object *object TSRMLS_DC) {
    php_package *package = (php_package *)((char *)object - XtOffsetOf(php_package, std));
    if (package->package) {
        int rc = package_free(package->package);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

/*
 * Payload
 */
static zend_function_entry payload_methods[] = {
    PHP_ME(Payload, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Payload, build, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Payload, run, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

zend_object *inapi_payload_create(zend_class_entry *ce) {
    php_payload *intern = ecalloc(1, sizeof(php_payload) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_payload_handlers;

    return &intern->std;
}

void inapi_payload_free(zend_object *object TSRMLS_DC) {
    php_payload *payload = (php_payload *)((char *)object - XtOffsetOf(php_payload, std));
    if (payload->payload) {
        int rc = payload_free(payload->payload);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

/*
 * Service
 */
static zend_function_entry service_methods[] = {
    PHP_ME(Service, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Service, action, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

zend_object *inapi_service_create(zend_class_entry *ce) {
    php_service *intern = ecalloc(1, sizeof(php_service) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_service_handlers;

    return &intern->std;
}

void inapi_service_free(zend_object *object TSRMLS_DC) {
    php_service *service = (php_service *)((char *)object - XtOffsetOf(php_service, std));
    if (service->service) {
        int rc = service_free(service->service);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

/*
 * ServiceRunnable
 */
static zend_function_entry service_runnable_methods[] = {
    PHP_ME(ServiceRunnable, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    {NULL, NULL, NULL}
};

zend_object *inapi_service_runnable_create(zend_class_entry *ce) {
    php_service_runnable *intern = ecalloc(1, sizeof(php_service_runnable) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_service_runnable_handlers;

    return &intern->std;
}

void inapi_service_runnable_free(zend_object *object TSRMLS_DC) {
    php_service_runnable *runnable = (php_service_runnable *)((char *)object - XtOffsetOf(php_service_runnable, std));

    if (runnable->service_runnable.command) {
        efree(runnable->service_runnable.command);
    }

    if (runnable->service_runnable.service) {
        efree(runnable->service_runnable.service);
    }

    zend_object_std_dtor(object);
}

/*
 * Template
 */
static zend_function_entry template_methods[] = {
    PHP_ME(Template, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Template, render, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

zend_object *inapi_template_create(zend_class_entry *ce) {
    php_template *intern = ecalloc(1, sizeof(php_template) + zend_object_properties_size(ce));

    zend_object_std_init(&intern->std, ce);
    object_properties_init(&intern->std, ce);

    intern->std.handlers = &inapi_template_handlers;

    return &intern->std;
}

void inapi_template_free(zend_object *object TSRMLS_DC) {
    php_template *template = (php_template *)((char *)object - XtOffsetOf(php_template, std));
    if (template->template) {
        int rc = template_free(template->template);
        assert(rc == 0);
    }
    zend_object_std_dtor(object);
}

PHP_MINIT_FUNCTION(inapi)
{
    zend_class_entry ce_command, ce_command_ex,
                     ce_directory, ce_directory_ex,
                     ce_file, ce_file_ex,
                     ce_host, ce_host_ex,
                     ce_package, ce_package_ex,
                     ce_payload, ce_payload_ex,
                     ce_service, ce_service_ex, ce_service_runnable,
                     ce_template, ce_template_ex;

    memcpy(&inapi_command_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_directory_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_file_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_host_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_package_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_payload_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_service_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_service_runnable_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    memcpy(&inapi_template_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));

    /*
     * Command
     */
    INIT_CLASS_ENTRY(ce_command, "Intecture\\Command", command_methods);
    ce_command.create_object = inapi_command_create;

    inapi_command_handlers.offset = XtOffsetOf(php_command, std);
    inapi_command_handlers.free_obj = inapi_command_free;

    inapi_ce_command = zend_register_internal_class(&ce_command);

    INIT_CLASS_ENTRY(ce_command_ex, "Intecture\\CommandException", NULL);
    inapi_ce_command_ex = zend_register_internal_class_ex(&ce_command_ex, zend_exception_get_default());

    /*
     * Directory
     */
    INIT_CLASS_ENTRY(ce_directory, "Intecture\\Directory", directory_methods);
    ce_directory.create_object = inapi_directory_create;

    inapi_directory_handlers.offset = XtOffsetOf(php_directory, std);
    inapi_directory_handlers.free_obj = inapi_directory_free;

    inapi_ce_directory = zend_register_internal_class(&ce_directory);
    zend_declare_class_constant_long(inapi_ce_directory, "OPT_DO_RECURSIVE", 16, OPT_DO_RECURSIVE TSRMLS_CC);

    INIT_CLASS_ENTRY(ce_directory_ex, "Intecture\\DirectoryException", NULL);
    inapi_ce_directory_ex = zend_register_internal_class_ex(&ce_directory_ex, zend_exception_get_default());

    /*
     * File
     */
    INIT_CLASS_ENTRY(ce_file, "Intecture\\File", file_methods);
    ce_file.create_object = inapi_file_create;

    inapi_file_handlers.offset = XtOffsetOf(php_file, std);
    inapi_file_handlers.free_obj = inapi_file_free;

    inapi_ce_file = zend_register_internal_class(&ce_file);
    zend_declare_class_constant_long(inapi_ce_file, "OPT_BACKUP_EXISTING", 19, OPT_BACKUP_EXISTING TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_file, "OPT_CHUNK_SIZE", 14, OPT_CHUNK_SIZE TSRMLS_CC);

    INIT_CLASS_ENTRY(ce_file_ex, "Intecture\\FileException", NULL);
    inapi_ce_file_ex = zend_register_internal_class_ex(&ce_file_ex, zend_exception_get_default());

    /*
     * Host
     */
    INIT_CLASS_ENTRY(ce_host, "Intecture\\Host", host_methods);
    ce_host.create_object = inapi_host_create;

    inapi_host_handlers.offset = XtOffsetOf(php_host, std);
    inapi_host_handlers.free_obj = inapi_host_free;

    inapi_ce_host = zend_register_internal_class(&ce_host);

    INIT_CLASS_ENTRY(ce_host_ex, "Intecture\\HostException", NULL);
    inapi_ce_host_ex = zend_register_internal_class_ex(&ce_host_ex, zend_exception_get_default());

    /*
     * Package
     */
    INIT_CLASS_ENTRY(ce_package, "Intecture\\Package", package_methods);
    ce_package.create_object = inapi_package_create;

    inapi_package_handlers.offset = XtOffsetOf(php_package, std);
    inapi_package_handlers.free_obj = inapi_package_free;

    inapi_ce_package = zend_register_internal_class(&ce_package);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_APT", 12, 1 TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_DNF", 12, 2 TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_HOMEBREW", 17, 3 TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_MACPORTS", 17, 4 TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_PKG", 12, 5 TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_PORTS", 14, 6 TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_package, "PROVIDER_YUM", 12, 7 TSRMLS_CC);

    INIT_CLASS_ENTRY(ce_package_ex, "Intecture\\PackageException", NULL);
    inapi_ce_package_ex = zend_register_internal_class_ex(&ce_package_ex, zend_exception_get_default());

    /*
     * Payload
     */
    INIT_CLASS_ENTRY(ce_payload, "Intecture\\Payload", payload_methods);
    ce_payload.create_object = inapi_payload_create;

    inapi_payload_handlers.offset = XtOffsetOf(php_payload, std);
    inapi_payload_handlers.free_obj = inapi_payload_free;

    inapi_ce_payload = zend_register_internal_class(&ce_payload);

    INIT_CLASS_ENTRY(ce_payload_ex, "Intecture\\PayloadException", NULL);
    inapi_ce_payload_ex = zend_register_internal_class_ex(&ce_payload_ex, zend_exception_get_default());

    /*
     * Service
     */
    INIT_CLASS_ENTRY(ce_service, "Intecture\\Service", service_methods);
    ce_service.create_object = inapi_service_create;

    inapi_service_handlers.offset = XtOffsetOf(php_service, std);
    inapi_service_handlers.free_obj = inapi_service_free;

    inapi_ce_service = zend_register_internal_class(&ce_service);

    INIT_CLASS_ENTRY(ce_service_ex, "Intecture\\ServiceException", NULL);
    inapi_ce_service_ex = zend_register_internal_class_ex(&ce_service_ex, zend_exception_get_default());

    /*
     * ServiceRunnable
     */
    INIT_CLASS_ENTRY(ce_service_runnable, "Intecture\\ServiceRunnable", service_runnable_methods);
    ce_service_runnable.create_object = inapi_service_runnable_create;

    inapi_service_runnable_handlers.offset = XtOffsetOf(php_service_runnable, std);
    inapi_service_runnable_handlers.free_obj = inapi_service_runnable_free;

    inapi_ce_service_runnable = zend_register_internal_class(&ce_service_runnable);
    zend_declare_class_constant_long(inapi_ce_service_runnable, "COMMAND", 7, RUNNABLE_COMMAND TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_service_runnable, "SERVICE", 7, RUNNABLE_SERVICE TSRMLS_CC);

    /*
     * Template
     */
    INIT_CLASS_ENTRY(ce_template, "Intecture\\Template", template_methods);
    ce_template.create_object = inapi_template_create;

    inapi_template_handlers.offset = XtOffsetOf(php_template, std);
    inapi_template_handlers.free_obj = inapi_template_free;

    inapi_ce_template = zend_register_internal_class(&ce_template);

    INIT_CLASS_ENTRY(ce_template_ex, "Intecture\\TemplateException", NULL);
    inapi_ce_template_ex = zend_register_internal_class_ex(&ce_template_ex, zend_exception_get_default());

    return SUCCESS;
}

zend_module_entry inapi_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_INAPI_EXTNAME,     /* Extension name */
    NULL,                  /* Functions */
    PHP_MINIT(inapi),      /* Methods */
    NULL,                  /* MSHUTDOWN */
    NULL,                  /* RINIT */
    NULL,                  /* RSHUTDOWN */
    NULL,                  /* MINFO */
    PHP_INAPI_EXTVER,      /* Extension version */
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_INAPI
ZEND_GET_MODULE(inapi)
#endif