lithium\analysis\Parser::tokenize()

public static method

Splits the provided $code into PHP language tokens.

Parameters

  • string $code

    Source code to be tokenized.

  • array $options

    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.

Returns

array

An array of tokens in the supplied source code.

Source

	public static function tokenize($code, array $options = []) {
		$defaults = ['wrap' => true, 'ignore' => [], 'include' => []];
		$options += $defaults;
		$tokens = [];
		$line = 1;

		if ($options['wrap']) {
			$code = "<?php {$code}?>";
		}
		foreach (token_get_all($code) as $token) {
			$token = (isset($token[1])) ? $token : [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[] = ['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;
	}