lithium\analysis\Parser::tokenize PHP Method

tokenize() public static method

Splits the provided $code into PHP language tokens.
public static tokenize ( string $code, array $options = [] ) : array
$code string Source code to be tokenized.
$options array Options consists of: -'wrap': Boolean indicating whether or not to wrap the supplied code in PHP tags. -'ignore': An array containing PHP language tokens to ignore. -'include': If supplied, an array of the only language tokens to include in the output.
return array An array of tokens in the supplied source code.
    public static function tokenize($code, array $options = array())
    {
        $defaults = array('wrap' => true, 'ignore' => array(), 'include' => array());
        $options += $defaults;
        $tokens = array();
        $line = 1;
        if ($options['wrap']) {
            $code = "<?php {$code}?>";
        }
        foreach (token_get_all($code) as $token) {
            $token = isset($token[1]) ? $token : array(null, $token, $line);
            list($id, $content, $line) = $token;
            $name = $id ? token_name($id) : $content;
            if (!empty($options['include'])) {
                if (!in_array($name, $options['include']) && !in_array($id, $options['include'])) {
                    continue;
                }
            }
            if (!empty($options['ignore'])) {
                if (in_array($name, $options['ignore']) || in_array($id, $options['ignore'])) {
                    continue;
                }
            }
            $tokens[] = array('id' => $id, 'name' => $name, 'content' => $content, 'line' => $line);
            $line += count(preg_split('/\\r\\n|\\r|\\n/', $content)) - 1;
        }
        if ($options['wrap'] && empty($options['include'])) {
            $tokens = array_slice($tokens, 1, count($tokens) - 2);
        }
        return $tokens;
    }

Usage Example

 public function testFilteredTokenization()
 {
     $code = 'while (isset($countRugen)) { if ($inigoMontoya->is("alive")) { ' . "\n";
     $code .= '$inigoMontoya->say(array("hello", "name", "accusation", "die")); ' . "\n";
     $code .= 'try { $inigoMontoya->kill($countRugen); } catch (Exception $e) { continue; } } }';
     $result = Parser::tokenize($code, array('include' => array('T_IF', 'T_WHILE', 'T_CATCH')));
     $expected = array(array('id' => 318, 'name' => 'T_WHILE', 'content' => 'while', 'line' => 1), array('id' => 301, 'name' => 'T_IF', 'content' => 'if', 'line' => 1), array('id' => 338, 'name' => 'T_CATCH', 'content' => 'catch', 'line' => 3));
     $this->assertEqual($expected, $result);
 }
All Usage Examples Of lithium\analysis\Parser::tokenize