Created
August 8, 2020 16:28
-
-
Save anthonyaxenov/a9d2e95584d705e5c82336a8b4e95f04 to your computer and use it in GitHub Desktop.
[PHP] Функции для проверки php-синтаксиса
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Проверяет корректность синтаксиса php-файла | |
* | |
* @param string $filepath Путь к файлу | |
* @param int $exitcode Код выхода процесса php | |
* @return array Массив строк результата | |
*/ | |
function php_syntax_file(string $filepath, int &$exitcode = 0): array { | |
$output = []; | |
exec("php -l {$filepath} 2>&1", $output, $exitcode); | |
if (count($output) > 1) { | |
unset($output[count($output)-1]); | |
} | |
return $output; | |
} | |
/** | |
* Проверяет корректность синтаксиса php-кода через файл | |
* | |
* @param string $php_code Явный PHP-код | |
* @param int $exitcode Код выхода процесса php | |
* @return array Массив строк результата | |
*/ | |
function php_syntax_text(string $php_code, int &$exitcode = 0): array { | |
$output = []; | |
$tmp_filename = 'tmp/tmp'.time(); | |
file_put_contents($tmp_filename, $php_code); | |
if (file_exists($tmp_filename)) { | |
$output = php_syntax_file($tmp_filename, $exitcode); | |
unlink($tmp_filename); | |
} else { | |
$exitcode = 255; | |
} | |
return $output; | |
} | |
/****************** | |
* Использование | |
******************/ | |
$file = 'some/testfile.php'; // путь к файлу | |
echo implode("<br>", php_syntax_file($file)); // проверка по файлу | |
echo implode("<br>", php_syntax_text(file_get_contents($file))); // проверка по содержимому файла | |
echo implode("<br>", php_syntax_text('<?php asdfasdfasdfasdfasdf phpinfo();')); // проверка по коду | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment