Metadata-Version: 2.3
Name: porifera
Version: 0.2.1
Summary: PORIFERA: PHP code rewriting instrumentation for expression in AST
Requires-Dist: php-parser-py==2.1.0
Requires-Dist: filelock>=3.29
Requires-Dist: typing-extensions>=4.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# porifera

PHP AST instrumentation for collecting runtime values at supported expression sites. The name comes from the sponge phylum Porifera.

## Install

```bash
pip install porifera
```

Requires Python >=3.10.

## Usage

The example requires a `php` executable on `PATH`; it is the target, and its `php -l` is the [compile check](docs/design.md#deployment-callback-types) of the staged bytes.

```python
import subprocess
from collections.abc import Mapping
from pathlib import Path
from tempfile import TemporaryDirectory

from php_parser_py import Parser, PHPVersion

import porifera

VERSION = "echo PHP_MAJOR_VERSION, ' ', PHP_MINOR_VERSION;"


def php_version() -> PHPVersion:
    found = subprocess.run(
        ["php", "-r", VERSION], capture_output=True, text=True, check=True
    )
    major, minor = found.stdout.split()
    return PHPVersion(int(major), int(minor))


def compile_check(target: PHPVersion, staged: Mapping[Path, bytes]) -> Mapping[Path, str]:
    if php_version() != target:
        raise RuntimeError(f"php on PATH is not PHP {target}")
    refused = {}
    for path, content in staged.items():
        checked = subprocess.run(["php", "-l"], input=content, capture_output=True)
        if checked.returncode:
            refused[path] = (checked.stderr + checked.stdout).decode("utf-8", errors="replace")
    return refused


with TemporaryDirectory() as directory:
    source = Path(directory) / "example.php"
    source.write_text("<?php\n$price = 40 + 2;\n", encoding="utf-8")
    target = php_version()
    ast = Parser(target_php=target).parse_file(str(source))
    node = ast.first_node(lambda item: item.node_type == "Expr_BinaryOp_Plus")
    assert node is not None
    with porifera.session(
        {node.id: "price"}, ast, target_php=target, compile_check=compile_check
    ) as active:
        subprocess.run(["php", str(source)], check=True)
        print(active.collect()["price"].values)
```

See [session states and exit cleanup](docs/model.md#state-model).

Probe strategies, outcome types and deployment callbacks are specified in the
[public API](docs/design.md). [Target admission](docs/spec-target-classification.md)
selects Expression Wrap, Wrap-Assign, Statement Probe or a refusal.

## Documentation

- [Concepts](docs/concept.md) — problem context, core terminology, data flow
- [Design](docs/design.md) — class structure, session API, exception types
- [Lifecycle and reference checks](docs/spec.md) — transaction steps and reference contexts
- [Wrap safety](docs/spec-wrap-safety.md) — expression context checks
- [Statement probes](docs/spec-statement-probe.md) — assignment capture and placement
- [Implementation](docs/impl.md) — [@php-parser-py] API, developer notes

## For Agents

Start at the [agent usage index](docs/llms.txt).

## License

GPL-2.0 — see [LICENSE](LICENSE).
