S
S
Sergey Gladyshev2018-02-09 19:32:52
PHP
Sergey Gladyshev, 2018-02-09 19:32:52

PHP. How to parse a PHP file?

Good afternoon.
There is a php file with approximate content:

/**
 * перед добавлением инфоблока
 *
 * даем права на инфоблоки на чтение по умолчанию
 */


AddEventHandler("iblock", "OnBeforeIBlockAdd", "iblock_OnBeforeIBlockAdd_FixIBlockPermissions");
function iblock_OnBeforeIBlockAdd_FixIBlockPermissions(&$arFields) {
    Bitrix\Main\Diag\Debug::writeToFile("iblock_OnBeforeIBlockAdd_FixIBlockPermissions", "", "iblock_OnBeforeIBlockAdd_FixIBlockPermissions.txt");
    $rsGroups = CGroup::GetList(($by = "c_sort"), ($order = "desc"), $filter);
    while ($arGroup = $rsGroups->Fetch()) {
        if ($arGroup['ANONYMOUS'] == 'Y') {
            $group_id = $arGroup['ID'];
            break;
        }
    }
    $arFields['GROUP_ID'][$group_id] = "R";
}

I need to use PHP to get:
1) comment
/**
 * перед добавлением инфоблока
 *
 * даем права на инфоблоки на чтение по умолчанию
 */

2) Function name
iblock_OnBeforeIBlockAdd_FixIBlockPermissions
Is there a library that can be used to do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2018-02-09
@Resident234

There is a built-in function token_get_all() that allows you to parse a PHP file into tokens:
php.net/manual/en/function.token-get-all.php
Example: https://3v4l.org/em0It
Token codes: php.net/ manual/en/tokens.php

M
Mikhail Osher, 2018-02-09
@miraage

From one of the core PHP developers: https://github.com/nikic/php-parser

K
Kirill Nesmeyanov, 2018-02-10
@SerafimArts

The two options above are fine. Well, or write your own EBNF grammar using:
1) Hoa LL(1)/LL(k): https://github.com/hoaproject/Compiler (The example above nikic from Mikhail Osher uses a LALR parser based on Yacc )
2) Or my fork: https://github.com/railt/compiler (grammar features are almost the same , but a few bugs have been fixed, sources have been rewritten and lexical analysis has been accelerated by 140 times ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question