Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace GuzzleHttp\Psr7;
   6  
   7  use Psr\Http\Message\StreamInterface;
   8  
   9  /**
  10   * Lazily reads or writes to a file that is opened only after an IO operation
  11   * take place on the stream.
  12   */
  13  #[\AllowDynamicProperties]
  14  final class LazyOpenStream implements StreamInterface
  15  {
  16      use StreamDecoratorTrait;
  17  
  18      /** @var string */
  19      private $filename;
  20  
  21      /** @var string */
  22      private $mode;
  23  
  24      /**
  25       * @param string $filename File to lazily open
  26       * @param string $mode     fopen mode to use when opening the stream
  27       */
  28      public function __construct(string $filename, string $mode)
  29      {
  30          $this->filename = $filename;
  31          $this->mode = $mode;
  32      }
  33  
  34      /**
  35       * Creates the underlying stream lazily when required.
  36       */
  37      protected function createStream(): StreamInterface
  38      {
  39          return Utils::streamFor(Utils::tryFopen($this->filename, $this->mode));
  40      }
  41  }