https://github.com/serafimarts/stream
PHP realtime source streaming library
https://github.com/serafimarts/stream
hooks patching realtime restream sources streaming transpiling wrapper
Last synced: 8 months ago
JSON representation
PHP realtime source streaming library
- Host: GitHub
- URL: https://github.com/serafimarts/stream
- Owner: SerafimArts
- License: mit
- Created: 2019-01-01T19:14:46.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-27T02:50:12.000Z (over 4 years ago)
- Last Synced: 2025-02-26T05:41:31.796Z (10 months ago)
- Topics: hooks, patching, realtime, restream, sources, streaming, transpiling, wrapper
- Language: PHP
- Size: 94.7 KB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Sources Streaming Package
## Installation
`composer require serafim/stream`
[Package on packagist.org](https://packagist.org/packages/serafim/stream)
## Introduction
Stream package provides the ability to override the data contained within the
files in real time.
## Protocol Streaming
```php
tryRead(function (string $pathname): string {
return $pathname;
});
echo \file_get_contents('some://example'); // string(7) "example"
```
```php
onRead(function (string $sources): string {
return $sources . "\n" . 'return 4;';
});
echo require 'four://example.php'; // int(1) "4"
```
## Composer
```php
when
// The stream will be triggered only on those files
// whose namespace starts with "App"
->namespace('App')
->then(function (string $sources): string {
\var_dump(42);
return $sources;
});
// When loading this class, var_dump(42) will be displayed.
new App\Example();
```
## Composer Filters
Each filter starts with calling the `$loader->when` method.
### Filter `where`
It works when the result of an anonymous function passed to the method `where`
returns the `true`.
```php
$loader->when->where(function (string $class, string $pathname): bool {
return $class === 'User';
});
$user = new User();
```
### Filter `not`
It works when the result of an anonymous function passed to the method `not`
returns the `false`.
```php
$loader->when->not(function (string $class, string $pathname): bool {
return $class !== 'User';
});
$user = new User();
```
### Filter `every`
Works when each rule applied inside an anonymous function returns
a positive result.
```php
use Serafim\Stream\Filter\Conjunction;
$loader->when->every(function (Conjunction $fn) {
$fn->where(...);
// AND
$fn->where(...);
});
```
### Filter `any`
Works when any (one of) rule applied inside an anonymous function returns
a positive result.
```php
use Serafim\Stream\Filter\Disjunction;
$loader->when->any(function (Disjunction $fn) {
$fn->where(...);
// OR
$fn->where(...);
});
```
### Filter `fqn`
Works in the case when the fqn (Fully qualified name) corresponds
to the specified.
```php
$loader->when->fqn('App\\User');
new App\User(); // Stream works
new Some\App\User(); // Stream does not work
```
### Filter `className`
Works in the case when the class name corresponds
to the specified.
```php
$loader->when->className('User');
new App\User(); // OK
new Any\User(); // OK
```
### Filter `namespace`
Works in the case when the namespace corresponds
to the specified.
```php
$loader->when->className('App');
new App\User(); // OK
new App\Message(); // OK
```
### Filter `fileName`
Works in the case when the file name corresponds
to the specified.
```php
$loader->when->fileName('App');
new App(); // The stream is triggered if the file name matches the class name.
```
### Filter `pathNameMatches`
The stream is triggered if the path matches the regular expression.
```php
$loader->when->pathNameMatches('Models/.*');
```
### Filter `fileNameMatches`
The stream is triggered if the file name matches the regular expression.
```php
$loader->when->fileNameMatches('\w+Interface');
```
### Filter `classNameMatches`
The stream is triggered if the class name matches the regular expression.
```php
$loader->when->classNameMatches('\w+Interface');
```
### Filter `fqnMatches`
The stream is triggered if the fqn (Fully qualified name) matches the regular expression.
```php
$loader->when->fqnMatches('App\\.*?\\\w+Interface');
```
### Filter `withVendors`
The stream is triggered if the file is loaded from the vendor
directory (by default, all vendor files are ignored)
```php
$loader->when->withVendors();
```

