|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bfabio\PublicCodeParser; |
| 6 | + |
| 7 | +use FFI; |
| 8 | +use Bfabio\PublicCodeParser\Exception\ParserException; |
| 9 | +use Bfabio\PublicCodeParser\Exception\ValidationException; |
| 10 | + |
| 11 | +class Parser |
| 12 | +{ |
| 13 | + private ParserOptions $options; |
| 14 | + private FFI $ffi; |
| 15 | + private static ?FFI $ffiInstance = null; |
| 16 | + |
| 17 | + public function __construct(?ParserOptions $options = null) |
| 18 | + { |
| 19 | + $this->options = $options ?? new ParserOptions(); |
| 20 | + $this->ffi = $this->getFFI(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Parse publiccode.yml content |
| 25 | + * |
| 26 | + * @param string $content YAML content |
| 27 | + * @return PublicCode |
| 28 | + * @throws ParserException |
| 29 | + * @throws ValidationException |
| 30 | + */ |
| 31 | + public function parse(string $content): PublicCode |
| 32 | + { |
| 33 | + $result = $this->ffi->ParseString($content); |
| 34 | + |
| 35 | + if ($result === null) { |
| 36 | + throw new ParserException('Failed to parse publiccode.yml content'); |
| 37 | + } |
| 38 | + |
| 39 | + return $this->processResult($result); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Parse publiccode.yml file |
| 44 | + * |
| 45 | + * @param string $filePath Path to publiccode.yml |
| 46 | + * @return PublicCode |
| 47 | + * @throws ParserException |
| 48 | + * @throws ValidationException |
| 49 | + */ |
| 50 | + public function parseFile(string $filePath): PublicCode |
| 51 | + { |
| 52 | + if (!file_exists($filePath)) { |
| 53 | + throw new ParserException("File not found: {$filePath}"); |
| 54 | + } |
| 55 | + |
| 56 | + $options = FFI::new('struct ParseOptions'); |
| 57 | + $options->DisableNetwork = $this->options->isDisableNetwork(); |
| 58 | + |
| 59 | + $result = $this->ffi->ParseFile($filePath, FFI::addr($options)); |
| 60 | + |
| 61 | + if ($result === null) { |
| 62 | + throw new ParserException('Failed to parse publiccode.yml file'); |
| 63 | + } |
| 64 | + |
| 65 | + return $this->processResult($result); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Validate publiccode.yml file without parsing |
| 70 | + * |
| 71 | + * @param string $filePath |
| 72 | + * @return bool |
| 73 | + */ |
| 74 | + public function validate(string $filePath): bool |
| 75 | + { |
| 76 | + try { |
| 77 | + $this->parseFile($filePath); |
| 78 | + return true; |
| 79 | + } catch (ParserException | ValidationException $e) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get or create FFI instance |
| 86 | + * |
| 87 | + * @return FFI |
| 88 | + * @throws ParserException |
| 89 | + */ |
| 90 | + private function getFFI(): FFI |
| 91 | + { |
| 92 | + if (self::$ffiInstance !== null) { |
| 93 | + return self::$ffiInstance; |
| 94 | + } |
| 95 | + |
| 96 | + $libraryPath = $this->findLibrary(); |
| 97 | + |
| 98 | + $cdef = <<<'CDEF' |
| 99 | + typedef struct { |
| 100 | + bool DisableNetwork; |
| 101 | + char *Branch; |
| 102 | + char *BaseURL; |
| 103 | + } ParseOptions; |
| 104 | +
|
| 105 | + typedef struct { |
| 106 | + char* Data; |
| 107 | + char* Error; |
| 108 | + int ErrorCount; |
| 109 | + char** Errors; |
| 110 | + } ParseResult; |
| 111 | +
|
| 112 | + ParseResult* ParseFile(const char* filepath, ParseOptions* options); |
| 113 | + ParseResult* ParseString(const char* content); |
| 114 | + void FreeResult(ParseResult* result); |
| 115 | + CDEF; |
| 116 | + |
| 117 | + try { |
| 118 | + self::$ffiInstance = FFI::cdef($cdef, $libraryPath); |
| 119 | + return self::$ffiInstance; |
| 120 | + } catch (\FFI\Exception $e) { |
| 121 | + throw new ParserException( |
| 122 | + 'Failed to load publiccode-parser library: ' . $e->getMessage(), |
| 123 | + 0, |
| 124 | + $e |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Find the publiccode-parser shared library |
| 131 | + * |
| 132 | + * @return string |
| 133 | + * @throws ParserException |
| 134 | + */ |
| 135 | + private function findLibrary(): string |
| 136 | + { |
| 137 | + $possiblePaths = [ |
| 138 | + __DIR__ . '/', |
| 139 | + __DIR__ . '/../lib/', |
| 140 | + __DIR__ . '/../vendor/lib/', |
| 141 | + '/usr/local/lib/', |
| 142 | + '/usr/lib/' |
| 143 | + ]; |
| 144 | + |
| 145 | + foreach ($possiblePaths as $path) { |
| 146 | + if (file_exists($path . 'libpubliccode-parser.so')) { |
| 147 | + return $path; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + throw new ParserException('libpubliccode-parser.so not found'); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Process FFI result and convert to PublicCode object |
| 156 | + * |
| 157 | + * @param mixed $result FFI ParseResult pointer |
| 158 | + * @return PublicCode |
| 159 | + * @throws ValidationException |
| 160 | + * @throws ParserException |
| 161 | + */ |
| 162 | + private function processResult($result): PublicCode |
| 163 | + { |
| 164 | + if ($result->Error !== null) { |
| 165 | + $errors = []; |
| 166 | + |
| 167 | + if ($result->ErrorCount > 0 && $result->Errors !== null) { |
| 168 | + for ($i = 0; $i < $result->ErrorCount; $i++) { |
| 169 | + $errors[] = FFI::string($result->Errors[$i]); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + $errorMessage = FFI::string($result->Error); |
| 174 | + $this->ffi->FreeResult($result); |
| 175 | + |
| 176 | + throw new ValidationException($errorMessage, $errors); |
| 177 | + } |
| 178 | + |
| 179 | + if ($result->Data === null) { |
| 180 | + $this->ffi->FreeResult($result); |
| 181 | + throw new ParserException('No data returned from parser'); |
| 182 | + } |
| 183 | + |
| 184 | + $jsonData = FFI::string($result->Data); |
| 185 | + $this->ffi->FreeResult($result); |
| 186 | + |
| 187 | + $data = json_decode($jsonData, true); |
| 188 | + |
| 189 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 190 | + throw new ParserException('Failed to decode JSON data: ' . json_last_error_msg()); |
| 191 | + } |
| 192 | + |
| 193 | + return new PublicCode($data); |
| 194 | + } |
| 195 | +} |
0 commit comments