2017-25 © Pedro Peláez
 

library throwable-generator

ThrowableGenerator

image

glen/throwable-generator

ThrowableGenerator

  • Thursday, July 26, 2018
  • by glen
  • Repository
  • 0 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

ThrowableGenerator

Generator wrapper for yield be sequential regardless of using Generator::throw., (*1)

From top level view, Generator::throw discards next value from generator, but it's not completely lost, but instead it becames return value of throw() method., (*2)

This is rather inconvenient if you want values be returned by yield., (*3)

<?php

function generator() {
  foreach (range(1, 6) as $x) {
    try {
      yield $x;
    } catch (Throwable $e) {
      echo " !! exception: {$e->getMessage()}\n";
    }
  }
}

$generator = generator();

foreach ($generator as $x) {
  echo "process: $x\n";
  if ($x % 2 === 0) {
    $generator->throw(new RuntimeException($x));
  }
}

The above code prints:, (*4)

process: 1
process: 2
 !! exception: 2
process: 4
 !! exception: 4
process: 6
 !! exception: 6

i.e values 3 and 5 were "lost" by using throw. This library makes result to be:, (*5)

process: 1
process: 2
 !! exception: 2
process: 3
process: 4
 !! exception: 4
process: 5
process: 6
 !! exception: 6

See question and discussion on stackoverflow post, (*6)

To use this class, wrap your original generator with this class:, (*7)

$generator = new ThrowableGenerator($generator);

Note: There is API change, throw() will return nothing instead of next value from generator., (*8)

The Versions

26/07 2018

dev-master

9999999-dev

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

26/07 2018

1.1.0

1.1.0.0

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

26/07 2018

dev-send-wrapper

dev-send-wrapper

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

26/07 2018

dev-unit-tests

dev-unit-tests

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

18/07 2018

1.0.2

1.0.2.0

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

18/07 2018

1.0.1

1.0.1.0

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

18/07 2018

1.0.0

1.0.0.0

ThrowableGenerator

  Sources   Download

MIT

The Requires

  • php ^7.0