• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IlyasDeckers / ody-core / 13532154862

25 Feb 2025 10:24PM UTC coverage: 30.374% (+1.7%) from 28.706%
13532154862

push

github

web-flow
Update php.yml

544 of 1791 relevant lines covered (30.37%)

9.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Middleware/OutputBufferingMiddleware.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Ody\Core\Middleware;
5

6
use InvalidArgumentException;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\StreamFactoryInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Throwable;
13

14
use function in_array;
15
use function ob_end_clean;
16
use function ob_get_clean;
17
use function ob_start;
18

19
/** @api */
20
class OutputBufferingMiddleware implements MiddlewareInterface
21
{
22
    public const APPEND = 'append';
23
    public const PREPEND = 'prepend';
24

25
    protected StreamFactoryInterface $streamFactory;
26

27
    protected string $style;
28

29
    /**
30
     * @param string $style Either "append" or "prepend"
31
     */
32
    public function __construct(StreamFactoryInterface $streamFactory, string $style = 'append')
×
33
    {
34
        $this->streamFactory = $streamFactory;
×
35
        $this->style = $style;
×
36

37
        if (!in_array($style, [static::APPEND, static::PREPEND], true)) {
×
38
            throw new InvalidArgumentException("Invalid style `{$style}`. Must be `append` or `prepend`");
×
39
        }
40
    }
41

42
    /**
43
     * @throws Throwable
44
     */
45
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
×
46
    {
47
        try {
48
            ob_start();
×
49
            $response = $handler->handle($request);
×
50
            $output = ob_get_clean();
×
51
        } catch (Throwable $e) {
×
52
            ob_end_clean();
×
53
            throw $e;
×
54
        }
55

56
        if (!empty($output)) {
×
57
            if ($this->style === static::PREPEND) {
×
58
                $body = $this->streamFactory->createStream();
×
59
                $body->write($output . $response->getBody());
×
60
                $response = $response->withBody($body);
×
61
            } elseif ($this->style === static::APPEND && $response->getBody()->isWritable()) {
×
62
                $response->getBody()->write($output);
×
63
            }
64
        }
65

66
        return $response;
×
67
    }
68
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc