You are viewing documentation for an outdated version. It is no longer supported!

Meta

Whenever a stream component is created, a new meta Repository is assigned to it. It contains various values obtained by PHP's stream_get_meta_data()open in new window and fstat()open in new window. The meta repository can also be used to assign arbitrary data or information to a stream, which can be useful in situations when working with multiple streams at the same time and require some additional information to be associated with each.

$stream = FileStream::open('people.txt', 'rb');

$meta = $stream->meta();

echo $meta->get('stats.size'); // 12

Raw meta always merged

The "raw" meta-data that is provided by rawMeta() (see method description below). This data is ALWAYS merged into the meta Repository instance, whenever you invoke the meta() method.

Raw Meta

Each stream instance comes with the ability to obtain the underlying resource's "meta-data", using PHP's stream_get_meta_data()open in new window and fstat()open in new window methods.

To acquire a raw version of this meta-data, use the rawMeta() method.

$stream = FileStream::open('people.txt', 'rb');

$rawMeta = $stream->rawMeta(); // array

Note As previously mentioned, this method is always invoked and its output is automatically merged into the meta Repository, whenever the meta() method is called!

Assign Arbitrary Meta-Data

To assign arbitrary meta-data to a stream, use the setMetaRepository(). The method can accept the following types as argument:

  • array: associate array, containing key-value pairs.
  • \Aedart\Contracts\Streams\Meta\Repository: A meta Repository instance.
  • null: If null is given, then a new meta Repository instance will automatically be set.
$stream = FileStream::open('people.txt', 'rb')
    ->setMetaRepository([
        'acme.foo' => 'bar'
    ]);

$meta = $stream->meta();

echo $meta->get('acme.foo'); // bar

Alternative

Alternatively, you may also specify custom arbitrary data directly in the meta Repository, using it's set() method.

$stream->meta()->set('acme.bar', 'foo');
echo $stream->meta()->get('acme.bar'); // foo

Caution

You SHOULD always prefix your arbitrary meta-data keys, to avoid accidental naming conflicts and overwrites with values provided by rawMeta().