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

tempestphp / tempest-framework / 14161923512

30 Mar 2025 01:41PM UTC coverage: 80.964% (+0.2%) from 80.716%
14161923512

push

github

web-flow
ci: prevent coveralls failures from failing tests (#1104)

11058 of 13658 relevant lines covered (80.96%)

100.68 hits per line

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

91.3
/src/Tempest/Router/src/Static/StaticGenerateCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Router\Static;
6

7
use Tempest\Console\Console;
8
use Tempest\Console\ConsoleArgument;
9
use Tempest\Console\ConsoleCommand;
10
use Tempest\Console\ExitCode;
11
use Tempest\Console\HasConsole;
12
use Tempest\Container\Container;
13
use Tempest\Core\Kernel;
14
use Tempest\EventBus\EventBus;
15
use Tempest\Http\Method;
16
use Tempest\Http\Status;
17
use Tempest\Router\DataProvider;
18
use Tempest\Router\GenericRequest;
19
use Tempest\Router\Router;
20
use Tempest\Router\Static\Exceptions\InvalidStatusCodeException;
21
use Tempest\Router\Static\Exceptions\NoTextualBodyException;
22
use Tempest\View\Exceptions\ViewCompilationError;
23
use Tempest\View\View;
24
use Tempest\View\ViewRenderer;
25
use Tempest\Vite\Exceptions\ManifestNotFoundException;
26
use Throwable;
27

28
use function Tempest\Support\path;
29
use function Tempest\uri;
30

31
final readonly class StaticGenerateCommand
32
{
33
    use HasConsole;
34

35
    public function __construct(
5✔
36
        private Console $console,
37
        private Kernel $kernel,
38
        private Container $container,
39
        private StaticPageConfig $staticPageConfig,
40
        private Router $router,
41
        private ViewRenderer $viewRenderer,
42
        private EventBus $eventBus,
43
    ) {}
5✔
44

45
    #[ConsoleCommand(name: 'static:generate', description: 'Compiles static pages')]
5✔
46
    public function __invoke(
47
        ?string $filter = null,
48
        #[ConsoleArgument(aliases: ['v'])]
49
        bool $verbose = false,
50
    ): ExitCode {
51
        $publicPath = path($this->kernel->root, 'public');
5✔
52

53
        $generated = 0;
5✔
54
        $failures = 0;
5✔
55

56
        $this->console->header('Generating static pages');
5✔
57

58
        $this->eventBus->listen(StaticPageGenerated::class, function (StaticPageGenerated $event) use (&$generated): void {
5✔
59
            $generated++;
5✔
60
            $this->keyValue("<style='fg-gray'>{$event->uri}</style>", "<style='fg-green'>{$event->path}</style>");
5✔
61
        });
5✔
62

63
        $this->eventBus->listen(StaticPageGenerationFailed::class, function (StaticPageGenerationFailed $event) use (&$failures, $verbose): void {
5✔
64
            $failures++;
2✔
65

66
            match (true) {
67
                $event->exception instanceof InvalidStatusCodeException => $this->keyValue(
2✔
68
                    "<style='fg-gray'>{$event->path}</style>",
2✔
69
                    "<style='fg-red'>HTTP {$event->exception->status->value}</style>",
2✔
70
                ),
2✔
71
                $event->exception instanceof NoTextualBodyException => $this->keyValue(
1✔
72
                    "<style='fg-gray'>{$event->path}</style>",
1✔
73
                    "<style='fg-red'>NO CONTENT</style>",
1✔
74
                ),
1✔
75
                $verbose === true => $this->error("Failed to generate static page: {$event->exception->getMessage()}"),
×
76
                default => $this->keyValue("<style='fg-gray'>{$event->path}</style>", "<style='fg-red'>FAILED</style>"),
×
77
            };
78
        });
5✔
79

80
        foreach ($this->staticPageConfig->staticPages as $staticPage) {
5✔
81
            /** @var DataProvider $dataProvider */
82
            $dataProvider = $this->container->get($staticPage->dataProviderClass ?? GenericDataProvider::class);
5✔
83

84
            foreach ($dataProvider->provide() as $params) {
5✔
85
                if (! is_array($params)) {
5✔
86
                    $params = [$params];
×
87
                }
88

89
                $uri = parse_url(uri($staticPage->handler, ...$params), PHP_URL_PATH);
5✔
90

91
                $fileName = $uri === '/'
5✔
92
                    ? 'index.html'
×
93
                    : ($uri . '/index.html');
5✔
94

95
                if ($filter !== null && $uri !== $filter) {
5✔
96
                    continue;
×
97
                }
98

99
                $file = path($publicPath, $fileName);
5✔
100

101
                try {
102
                    $response = $this->router->dispatch(
5✔
103
                        new GenericRequest(
5✔
104
                            method: Method::GET,
5✔
105
                            uri: $uri,
5✔
106
                        ),
5✔
107
                    );
5✔
108

109
                    if ($response->status !== Status::OK) {
5✔
110
                        throw new InvalidStatusCodeException($uri, $response->status);
1✔
111
                    }
112

113
                    $body = $response->body;
5✔
114

115
                    $content = ($body instanceof View)
5✔
116
                        ? $this->viewRenderer->render($body)
5✔
117
                        : $body;
1✔
118

119
                    if (! is_string($content)) {
5✔
120
                        throw new NoTextualBodyException($uri);
1✔
121
                    }
122

123
                    $directory = $file->dirname();
5✔
124

125
                    if (! $directory->isDirectory()) {
5✔
126
                        mkdir($directory->toString(), recursive: true);
2✔
127
                    }
128

129
                    file_put_contents($file->toString(), $content);
5✔
130

131
                    $this->eventBus->dispatch(new StaticPageGenerated($uri, $file->toString(), $content));
5✔
132
                } catch (Throwable $exception) {
3✔
133
                    if (ob_get_contents()) {
3✔
134
                        ob_clean();
×
135
                    }
136

137
                    if ($exception instanceof ViewCompilationError && $exception->getPrevious() instanceof ManifestNotFoundException) {
3✔
138
                        $this->error("A Vite build is needed for [{$uri}]. Run <code>vite build</code> first.");
1✔
139
                        return ExitCode::ERROR;
1✔
140
                    }
141

142
                    $this->eventBus->dispatch(new StaticPageGenerationFailed($uri, $exception));
2✔
143

144
                    continue;
2✔
145
                }
146
            }
147
        }
148

149
        if ($failures) {
4✔
150
            $this->keyValue('Failures', "<style='fg-red'>{$failures}</style>");
2✔
151
        }
152

153
        $this->keyValue('Static pages generated', "<style='fg-green'>{$generated}</style>");
4✔
154

155
        return $failures > 0
4✔
156
            ? ExitCode::ERROR
2✔
157
            : ExitCode::SUCCESS;
4✔
158
    }
159
}
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

© 2025 Coveralls, Inc