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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Macros for interacting with PHP, mainly when the function takes variadic
//! arguments. Unfortunately, this is the best way to handle these.
//! Note that most of these will introduce unsafe into your code base.
/// Starts the PHP extension information table displayed when running
/// `phpinfo();` Must be run *before* rows are inserted into the table.
/// Ends the PHP extension information table. Must be run *after* all rows have
/// been inserted into the table.
/// Sets the header for the PHP extension information table. Takes as many
/// string arguments as required.
/// Adds a row to the PHP extension information table. Takes as many string
/// arguments as required.
/// INTERNAL: Calls a variadic C function with the number of parameters, then
/// following with the parameters.
/// Attempts to call a given PHP callable.
///
/// # Parameters
///
/// * `$fn` - The 'function' to call. Can be an [`Arg`] or a [`Zval`].
/// * ...`$param` - The parameters to pass to the function. Must be able to be
/// converted into a [`Zval`].
///
/// [`Arg`]: crate::args::Arg
/// [`Zval`]: crate::types::Zval
/// Attempts to call a given PHP callable with named arguments.
///
/// This macro supports PHP 8.0+ named arguments, allowing you to pass
/// arguments by name rather than position.
///
/// # Syntax
///
/// ```ignore
/// // Named arguments only
/// call_user_func_named!(callable, name1: value1, name2: value2)
///
/// // Positional arguments followed by named arguments
/// call_user_func_named!(callable, [pos1, pos2], name1: value1, name2: value2)
/// ```
///
/// # Parameters
///
/// * `$fn` - The 'function' to call. Can be an [`Arg`] or a [`Zval`].
/// * `$name: $value` - Named parameters as `name: value` pairs.
/// * `[$($pos),*]` - Optional positional parameters in square brackets.
///
/// # Examples
///
/// ```ignore
/// use ext_php_rs::{call_user_func_named, types::ZendCallable};
///
/// let str_replace = ZendCallable::try_from_name("str_replace").unwrap();
///
/// // Using named arguments only
/// let result = call_user_func_named!(str_replace,
/// search: "world",
/// replace: "PHP",
/// subject: "Hello world"
/// ).unwrap();
///
/// // Mixing positional and named arguments
/// let result = call_user_func_named!(str_replace, ["world", "PHP"],
/// subject: "Hello world"
/// ).unwrap();
/// ```
///
/// [`Arg`]: crate::args::Arg
/// [`Zval`]: crate::types::Zval
/// Note: Parameter names must be valid Rust identifiers.
/// For other names, use `try_call_named` directly.
/// Parses a given list of arguments using the [`ArgParser`] class.
///
/// # Examples
///
/// This example parses all of the arguments. If one is invalid, execution of
/// the function will stop at the `parse_args!` macro invocation. The user is
/// notified via PHP's argument parsing system.
///
/// In this case, all of the arguments are required.
///
/// ```
/// # #[macro_use] extern crate ext_php_rs;
/// use ext_php_rs::{
/// parse_args,
/// args::Arg,
/// flags::DataType,
/// zend::ExecuteData,
/// types::Zval,
/// };
///
/// pub extern "C" fn example_fn(execute_data: &mut ExecuteData, _: &mut Zval) {
/// let mut x = Arg::new("x", DataType::Long);
/// let mut y = Arg::new("y", DataType::Long);
/// let mut z = Arg::new("z", DataType::Long);
///
/// parse_args!(execute_data, x, y, z);
/// }
/// ```
///
/// This example is similar to the one above, apart from the fact that the `z`
/// argument is not required. Note the semicolon separating the first two
/// arguments from the second.
///
/// ```
/// use ext_php_rs::{
/// parse_args,
/// args::Arg,
/// flags::DataType,
/// zend::ExecuteData,
/// types::Zval,
/// };
///
/// pub extern "C" fn example_fn(execute_data: &mut ExecuteData, _: &mut Zval) {
/// let mut x = Arg::new("x", DataType::Long);
/// let mut y = Arg::new("y", DataType::Long);
/// let mut z = Arg::new("z", DataType::Long);
///
/// parse_args!(execute_data, x, y; z);
/// }
/// ```
///
/// [`ArgParser`]: crate::args::ArgParser
/// Throws an exception and returns from the current function.
///
/// Wraps the [`throw`] function by inserting a `return` statement after
/// throwing the exception.
///
/// [`throw`]: crate::exception::throw
///
/// # Examples
///
/// ```
/// use ext_php_rs::{
/// throw,
/// zend::{ce, ClassEntry, ExecuteData},
/// types::Zval,
/// };
///
/// pub extern "C" fn example_fn(execute_data: &mut ExecuteData, _: &mut Zval) {
/// let something_wrong = true;
/// if something_wrong {
/// throw!(ce::exception(), "Something is wrong!");
/// }
///
/// assert!(false); // This will not run.
/// }
/// ```
/// Implements a set of traits required to convert types that implement
/// [`RegisteredClass`] to and from [`ZendObject`]s and [`Zval`]s. Generally,
/// this macro should not be called directly, as it is called on any type that
/// uses the [`php_class`] macro.
///
/// The following traits are implemented:
///
/// * `FromZendObject for &'a T`
/// * `FromZendObjectMut for &'a mut T`
/// * `FromZval for &'a T`
/// * `FromZvalMut for &'a mut T`
/// * `IntoZendObject for T`
/// * `IntoZval for T`
///
/// These implementations are required while we wait on the stabilisation of
/// specialisation.
///
/// # Examples
///
/// ```
/// # use ext_php_rs::{convert::{IntoZval, FromZval, IntoZvalDyn}, types::{Zval, ZendObject}, class::{RegisteredClass, ConstructorMeta, ClassEntryInfo}, builders::{ClassBuilder, FunctionBuilder}, zend::ClassEntry, flags::{ClassFlags, MethodFlags}, internal::property::PropertyInfo, describe::DocComments};
/// use ext_php_rs::class_derives;
///
/// struct Test {
/// a: i32,
/// b: i64
/// }
///
/// impl RegisteredClass for Test {
/// const CLASS_NAME: &'static str = "Test";
///
/// const BUILDER_MODIFIER: Option<fn(ClassBuilder) -> ClassBuilder> = None;
/// const EXTENDS: Option<ClassEntryInfo> = None;
/// const IMPLEMENTS: &'static [ClassEntryInfo] = &[];
/// const FLAGS: ClassFlags = ClassFlags::empty();
/// const DOC_COMMENTS: DocComments = &[];
///
/// fn get_metadata() -> &'static ext_php_rs::class::ClassMetadata<Self> {
/// todo!()
/// }
///
/// fn get_properties<'a>(
/// ) -> std::collections::HashMap<&'static str, PropertyInfo<'a, Self>>
/// {
/// todo!()
/// }
///
/// fn method_builders() -> Vec<(FunctionBuilder<'static>, MethodFlags)> {
/// todo!()
/// }
///
/// fn constructor() -> Option<ConstructorMeta<Self>> {
/// todo!()
/// }
///
/// fn constants() -> &'static [(&'static str, &'static dyn IntoZvalDyn, DocComments)] {
/// todo!()
/// }
/// }
///
/// class_derives!(Test);
///
/// fn into_zval_test() -> Zval {
/// let x = Test { a: 5, b: 10 };
/// x.into_zval(false).unwrap()
/// }
///
/// fn from_zval_test<'a>(zv: &'a Zval) -> &'a Test {
/// <&Test>::from_zval(zv).unwrap()
/// }
/// ```
///
/// [`RegisteredClass`]: crate::class::RegisteredClass
/// [`ZendObject`]: crate::types::ZendObject
/// [`Zval`]: crate::types::Zval
/// [`php_class`]: crate::php_class
/// Derives `From<T> for Zval` and `IntoZval` for a given type.
/// Derives `TryFrom<Zval> for T` and `FromZval for T` on a given type.
/// Prints to the PHP standard output, without a newline.
///
/// Acts exactly the same as the built-in [`print`] macro.
///
/// # Panics
///
/// Panics if the generated string could not be converted to a `CString` due to
/// `NUL` characters.
/// Prints to the PHP standard output, with a newline.
///
/// The newline is only a newline character regardless of platform (no carriage
/// return).
///
/// Acts exactly the same as the built-in [`println`] macro.
///
/// # Panics
///
/// Panics if the generated string could not be converted to a `CString` due to
/// `NUL` characters.
/// Writes binary data to the PHP standard output.
///
/// Unlike [`php_print!`], this macro is binary-safe and can handle data
/// containing `NUL` bytes. It uses the SAPI module's `ub_write` function.
///
/// # Arguments
///
/// * `$data` - A byte slice (`&[u8]`) or byte literal (`b"..."`) to write.
///
/// # Returns
///
/// A `Result<usize>` containing the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::SapiWriteUnavailable`] if the SAPI's `ub_write` function
/// is not available.
///
/// [`Error::SapiWriteUnavailable`]: crate::error::Error::SapiWriteUnavailable
///
/// # Examples
///
/// ```ignore
/// use ext_php_rs::php_write;
///
/// // Write a byte literal
/// php_write!(b"Hello World").expect("write failed");
///
/// // Write binary data with NUL bytes (would panic with php_print!)
/// php_write!(b"Hello\x00World").expect("write failed");
///
/// // Write a byte slice
/// let data: &[u8] = &[0x48, 0x65, 0x6c, 0x6c, 0x6f];
/// php_write!(data).expect("write failed");
/// ```
/// Writes binary data to PHP's output stream with output buffering support.
///
/// This macro is both binary-safe (can handle `NUL` bytes) AND respects PHP's
/// output buffering (`ob_start()`). Use this when you need both capabilities.
///
/// # Arguments
///
/// * `$data` - A byte slice (`&[u8]`) or byte literal (`b"..."`) to write.
///
/// # Returns
///
/// The number of bytes written.
///
/// # Comparison
///
/// | Macro | Binary-safe | Output Buffering |
/// |-------|-------------|------------------|
/// | `php_print!` | No | Yes |
/// | `php_write!` | Yes | No (unbuffered) |
/// | `php_output!` | Yes | Yes |
///
/// # Examples
///
/// ```ignore
/// use ext_php_rs::php_output;
///
/// // Write binary data that will be captured by ob_start()
/// php_output!(b"Hello\x00World");
///
/// // Use with output buffering
/// // ob_start();
/// // php_output!(b"captured");
/// // $data = ob_get_clean(); // Contains "captured"
/// ```