|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Highlight\Languages\Nginx\Patterns; |
| 6 | + |
| 7 | +use Tempest\Highlight\IsPattern; |
| 8 | +use Tempest\Highlight\Pattern; |
| 9 | +use Tempest\Highlight\PatternTest; |
| 10 | +use Tempest\Highlight\Tokens\TokenTypeEnum; |
| 11 | + |
| 12 | +#[PatternTest(input: 'server {', output: 'server')] |
| 13 | +#[PatternTest(input: 'listen 80;', output: 'listen')] |
| 14 | +#[PatternTest(input: ' proxy_pass http://backend;', output: 'proxy_pass')] |
| 15 | +final readonly class NginxDirectivePattern implements Pattern |
| 16 | +{ |
| 17 | + use IsPattern; |
| 18 | + |
| 19 | + public function getPattern(): string |
| 20 | + { |
| 21 | + $directives = implode('|', [ |
| 22 | + 'server', 'location', 'listen', 'root', 'index', 'proxy_pass', |
| 23 | + 'upstream', 'http', 'events', 'worker_processes', 'worker_connections', |
| 24 | + 'error_log', 'access_log', 'ssl_certificate', 'ssl_certificate_key', |
| 25 | + 'try_files', 'return', 'rewrite', 'if', 'set', 'map', 'include', |
| 26 | + 'server_name', 'client_max_body_size', 'keepalive_timeout', |
| 27 | + 'gzip', 'gzip_types', 'add_header', 'deny', 'allow', |
| 28 | + 'fastcgi_pass', 'fastcgi_param', 'proxy_set_header', |
| 29 | + 'proxy_http_version', 'proxy_cache', 'proxy_cache_valid', |
| 30 | + 'sendfile', 'tcp_nopush', 'tcp_nodelay', 'types', 'default_type', |
| 31 | + 'log_format', 'expires', 'autoindex', 'charset', 'resolver', |
| 32 | + 'ssl', 'ssl_protocols', 'ssl_ciphers', 'ssl_prefer_server_ciphers', |
| 33 | + 'ssl_session_cache', 'ssl_session_timeout', |
| 34 | + 'limit_req', 'limit_req_zone', 'limit_conn', 'limit_conn_zone', |
| 35 | + 'geo', 'stub_status', |
| 36 | + ]); |
| 37 | + |
| 38 | + return "\b(?<match>(?:{$directives}))\b"; |
| 39 | + } |
| 40 | + |
| 41 | + public function getTokenType(): TokenTypeEnum |
| 42 | + { |
| 43 | + return TokenTypeEnum::KEYWORD; |
| 44 | + } |
| 45 | +} |
0 commit comments