|
| 1 | +# Examples |
| 2 | + |
| 3 | +This document provides comprehensive guidance for working with Flow PHP examples, including how to add new examples, |
| 4 | +update existing ones, and run them effectively. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +Flow PHP includes a comprehensive collection of executable examples organized by topic, demonstrating all major |
| 9 | +framework features. Each example is self-contained with isolated dependencies and serves as both documentation and |
| 10 | +integration testing. |
| 11 | + |
| 12 | +## Directory Structure |
| 13 | + |
| 14 | +Examples are organized hierarchically under `/examples/topics/` with the following structure: |
| 15 | + |
| 16 | +``` |
| 17 | +examples/ |
| 18 | +├── run.php # Main execution script |
| 19 | +├── clean.php # Cleanup utility |
| 20 | +└── topics/ # Topic-based organization |
| 21 | + ├── aggregations/ # Data aggregation operations (8 examples) |
| 22 | + ├── data_frame/ # DataFrame operations (7 examples) |
| 23 | + ├── data_reading/ # Data input methods (13 examples) |
| 24 | + ├── data_writing/ # Data output methods (9 examples) |
| 25 | + ├── filesystem/ # Filesystem operations (4 examples) |
| 26 | + ├── join/ # Data joining (2 examples) |
| 27 | + ├── partitioning/ # Data partitioning (4 examples) |
| 28 | + ├── schema/ # Schema management (4 examples) |
| 29 | + ├── transformations/ # Data transformations (12 examples) |
| 30 | + ├── types/ # Type system (2 examples) |
| 31 | + └── window_functions/ # Window functions (1 example) |
| 32 | +``` |
| 33 | + |
| 34 | +### Standard Example Structure |
| 35 | + |
| 36 | +Each example follows this consistent structure: |
| 37 | + |
| 38 | +``` |
| 39 | +examples/topics/{topic}/{example}/ |
| 40 | +├── code.php # Main executable script (required) |
| 41 | +├── composer.json # Isolated dependencies (required) |
| 42 | +├── composer.lock # Locked dependencies (generated) |
| 43 | +├── output.txt # Expected output (required) |
| 44 | +├── description.md # Human-readable explanation (optional) |
| 45 | +├── priority.txt # Numeric priority for ordering (optional) |
| 46 | +├── hidden.txt # Hide from website (optional) |
| 47 | +├── input/ # Sample input data (optional) |
| 48 | +├── output/ # Generated output files (optional) |
| 49 | +├── vendor/ # Composer dependencies (generated) |
| 50 | +└── flow_php_example.zip # Distribution archive (generated) |
| 51 | +``` |
| 52 | + |
| 53 | +## Running Examples |
| 54 | + |
| 55 | +### Command Line Interface |
| 56 | + |
| 57 | +Use the main execution script to run examples: |
| 58 | + |
| 59 | +```bash |
| 60 | +# Run all examples |
| 61 | +./examples/run.php |
| 62 | + |
| 63 | +# Run specific topic |
| 64 | +./examples/run.php --topic=data_reading |
| 65 | + |
| 66 | +# Run specific example |
| 67 | +./examples/run.php --topic=data_reading --example=csv |
| 68 | + |
| 69 | +# Update dependencies instead of install |
| 70 | +./examples/run.php --composer-update |
| 71 | + |
| 72 | +# Generate distribution archives |
| 73 | +./examples/run.php --composer-archive |
| 74 | +``` |
| 75 | + |
| 76 | +### Available Options |
| 77 | + |
| 78 | +- `--topic (-t)`: Run examples from a specific topic |
| 79 | +- `--example (-e)`: Run a specific example (requires --topic) |
| 80 | +- `--composer-update (-u)`: Update dependencies instead of install |
| 81 | +- `--composer-archive (-a)`: Generate ZIP archives for distribution |
| 82 | + |
| 83 | +### Integration with Build System |
| 84 | + |
| 85 | +Examples are integrated into the build process: |
| 86 | + |
| 87 | +```bash |
| 88 | +# Run examples as part of test suite |
| 89 | +composer test:examples |
| 90 | + |
| 91 | +# Run full test suite including examples |
| 92 | +composer test |
| 93 | +``` |
| 94 | + |
| 95 | +Examples run automatically in CI/CD on changes to `examples/**` directory. |
| 96 | + |
| 97 | +## Adding New Examples |
| 98 | + |
| 99 | +### Step-by-Step Process |
| 100 | + |
| 101 | +1. **Choose Topic and Name** |
| 102 | + - Use existing topics when possible |
| 103 | + - Create new topics only for distinct feature areas |
| 104 | + - Use descriptive, lowercase names with underscores |
| 105 | + |
| 106 | +2. **Create Directory Structure** |
| 107 | + ```bash |
| 108 | + mkdir -p examples/topics/{topic}/{example} |
| 109 | + cd examples/topics/{topic}/{example} |
| 110 | + ``` |
| 111 | + |
| 112 | +3. **Create Required Files** |
| 113 | + |
| 114 | + **`code.php`** - Main executable script: |
| 115 | + ```php |
| 116 | + <?php |
| 117 | + declare(strict_types=1); |
| 118 | + |
| 119 | + use function Flow\ETL\DSL\{data_frame, /* other functions */}; |
| 120 | + use function Flow\ETL\Adapter\[Adapter]\{/* adapter functions */}; |
| 121 | + |
| 122 | + require __DIR__ . '/vendor/autoload.php'; |
| 123 | + |
| 124 | + data_frame() |
| 125 | + ->read(/* extractor */) |
| 126 | + ->transform(/* transformations */) |
| 127 | + ->write(/* loader */) |
| 128 | + ->run(); |
| 129 | + ``` |
| 130 | + |
| 131 | + **`composer.json`** - Dependencies: |
| 132 | + ```json |
| 133 | + { |
| 134 | + "name": "flow-php/examples", |
| 135 | + "description": "Flow PHP - Examples", |
| 136 | + "license": "MIT", |
| 137 | + "type": "library", |
| 138 | + "require": { |
| 139 | + "flow-php/etl": "1.x-dev", |
| 140 | + "flow-php/etl-adapter-[specific]": "1.x-dev" |
| 141 | + }, |
| 142 | + "archive": { |
| 143 | + "exclude": [".env", "vendor"] |
| 144 | + } |
| 145 | + } |
| 146 | + ``` |
| 147 | + |
| 148 | + **`priority.txt`** - Numeric priority (1-99, lower = higher priority): |
| 149 | + ``` |
| 150 | + 10 |
| 151 | + ``` |
| 152 | + |
| 153 | +4. **Add Optional Files** |
| 154 | + |
| 155 | + **`description.md`** - Human-readable explanation: |
| 156 | + ```markdown |
| 157 | + # Example Title |
| 158 | + |
| 159 | + Brief description of what this example demonstrates. |
| 160 | + |
| 161 | + ## Key Features |
| 162 | + |
| 163 | + - Feature 1 |
| 164 | + - Feature 2 |
| 165 | + |
| 166 | + ## Use Cases |
| 167 | + |
| 168 | + When to use this pattern... |
| 169 | + ``` |
| 170 | + |
| 171 | + **`input/`** directory - Sample data files if needed |
| 172 | + **`hidden.txt`** - Empty file to hide from website |
| 173 | + |
| 174 | +5. **Test the Example** |
| 175 | + ```bash |
| 176 | + # Test locally |
| 177 | + ./examples/run.php --topic={topic} --example={example} |
| 178 | + ``` |
| 179 | + |
| 180 | +## Updating Existing Examples |
| 181 | + |
| 182 | +### Common Update Scenarios |
| 183 | + |
| 184 | +1. **Code Changes** |
| 185 | + - Edit `code.php` following existing patterns |
| 186 | + - Test thoroughly with `./examples/run.php` |
| 187 | + - Update `output.txt` if output changes |
| 188 | + |
| 189 | +2. **Dependency Updates** |
| 190 | + - Modify `composer.json` as needed |
| 191 | + - Run `./examples/run.php --composer-update --topic={topic} --example={example}` to refresh dependencies |
| 192 | + - Commit both `composer.json` and `composer.lock` |
| 193 | + |
| 194 | +3. **Documentation Updates** |
| 195 | + - Edit `description.md` for clarity |
| 196 | + - Keep documentation concise and focused |
| 197 | + |
| 198 | +4. **Priority Changes** |
| 199 | + - Modify `priority.txt` value (1-99) |
| 200 | + - Lower numbers appear first in listings |
| 201 | + |
| 202 | +## Priority and Organization System |
| 203 | + |
| 204 | +### Priority System |
| 205 | + |
| 206 | +Examples use numeric priorities for ordering: |
| 207 | + |
| 208 | +- **Range**: 1-99 (lower numbers = higher priority) |
| 209 | +- **Default**: 99 if `priority.txt` doesn't exist |
| 210 | +- **Application**: Both topics and individual examples |
| 211 | +- **Display**: Lower priority numbers appear first |
| 212 | + |
| 213 | +### Visibility Control |
| 214 | + |
| 215 | +- **`hidden.txt`**: Empty file that hides examples from website |
| 216 | +- **Use cases**: Internal examples, development utilities, deprecated examples |
| 217 | +- **Current usage**: Applied to some sequence generator examples |
| 218 | + |
| 219 | +## Maintenance and Cleanup |
| 220 | + |
| 221 | +### Cleanup Operations |
| 222 | + |
| 223 | +Remove generated files and dependencies: |
| 224 | + |
| 225 | +```bash |
| 226 | +# Clean all examples |
| 227 | +./examples/clean.php |
| 228 | + |
| 229 | +# This removes: |
| 230 | +# - vendor/ directories |
| 231 | +# - flow_php_example.zip files |
| 232 | +``` |
| 233 | + |
| 234 | +### Archive Generation |
| 235 | + |
| 236 | +Create distribution packages: |
| 237 | + |
| 238 | +```bash |
| 239 | +# Generate archives for all examples |
| 240 | +./examples/run.php --composer-archive |
| 241 | + |
| 242 | +# Archives are created as flow_php_example.zip in each example directory |
| 243 | +``` |
| 244 | + |
| 245 | +## Integration with Website |
| 246 | + |
| 247 | +Examples are automatically integrated into the Flow PHP website: |
| 248 | + |
| 249 | +- **Dynamic content**: Website reads examples directly from filesystem |
| 250 | +- **Priority ordering**: Uses `priority.txt` for display order |
| 251 | +- **Visibility**: Respects `hidden.txt` files |
| 252 | +- **Multi-format**: Supports various output formats (txt, xml, csv, json) |
| 253 | +- **Code highlighting**: Automatic syntax highlighting for code blocks |
0 commit comments