php_syntax.php(файл создан)
| @@ -0,0 +1,46 @@ | |||
| 1 | + | <?php | |
| 2 | + | ||
| 3 | + | /** | |
| 4 | + | * Проверяет корректность синтаксиса php-файла | |
| 5 | + | * | |
| 6 | + | * @param string $filepath Путь к файлу | |
| 7 | + | * @param int $exitcode Код выхода процесса php | |
| 8 | + | * @return array Массив строк результата | |
| 9 | + | */ | |
| 10 | + | function php_syntax_file(string $filepath, int &$exitcode = 0): array { | |
| 11 | + | $output = []; | |
| 12 | + | exec("php -l {$filepath} 2>&1", $output, $exitcode); | |
| 13 | + | if (count($output) > 1) { | |
| 14 | + | unset($output[count($output)-1]); | |
| 15 | + | } | |
| 16 | + | return $output; | |
| 17 | + | } | |
| 18 | + | ||
| 19 | + | /** | |
| 20 | + | * Проверяет корректность синтаксиса php-кода через файл | |
| 21 | + | * | |
| 22 | + | * @param string $php_code Явный PHP-код | |
| 23 | + | * @param int $exitcode Код выхода процесса php | |
| 24 | + | * @return array Массив строк результата | |
| 25 | + | */ | |
| 26 | + | function php_syntax_text(string $php_code, int &$exitcode = 0): array { | |
| 27 | + | $output = []; | |
| 28 | + | $tmp_filename = 'tmp/tmp'.time(); | |
| 29 | + | file_put_contents($tmp_filename, $php_code); | |
| 30 | + | if (file_exists($tmp_filename)) { | |
| 31 | + | $output = php_syntax_file($tmp_filename, $exitcode); | |
| 32 | + | unlink($tmp_filename); | |
| 33 | + | } else { | |
| 34 | + | $exitcode = 255; | |
| 35 | + | } | |
| 36 | + | return $output; | |
| 37 | + | } | |
| 38 | + | ||
| 39 | + | /****************** | |
| 40 | + | * Использование | |
| 41 | + | ******************/ | |
| 42 | + | ||
| 43 | + | $file = 'some/testfile.php'; // путь к файлу | |
| 44 | + | echo implode("<br>", php_syntax_file($file)); // проверка по файлу | |
| 45 | + | echo implode("<br>", php_syntax_text(file_get_contents($file))); // проверка по содержимому файла | |
| 46 | + | echo implode("<br>", php_syntax_text('<?php asdfasdfasdfasdfasdf phpinfo();')); // проверка по коду | |
Новее
Позже