Parses a PHP file for messages marked as translatable. Recognized as message marking are `$t()` and `$tn()` which are implemented in the `View` class. This is a rather simple and stupid parser but also fast and easy to grasp. It doesn't actively attempt to detect and work around syntax errors in marker functions.

Parameters

  • string $file Absolute path to a PHP file.

Returns

array

Source

						protected function _parsePhp($file) {
		$contents = file_get_contents($file);
		$contents = Compiler::compile($contents);

		$defaults = array(
			'ids' => array(),
			'open' => false,
			'position' => 0,
			'occurrence' => array('file' => $file, 'line' => null)
		);
		extract($defaults);
		$data = array();

		if (strpos($contents, '$t(') === false && strpos($contents, '$tn(') == false) {
			return $data;
		}

		$tokens = token_get_all($contents);
		unset($contents);

		foreach ($tokens as $key => $token) {
			if (!is_array($token)) {
				$token = array(0 => null, 1 => $token, 2 => null);
			}

			if ($open) {
				if ($position >= ($open === 'singular' ? 1 : 2)) {
					$data = $this->_merge($data, array(
						'id' => $ids['singular'],
						'ids' => $ids,
						'occurrences' => array($occurrence)
					));
					extract($defaults, EXTR_OVERWRITE);
				} elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
					$ids[$ids ? 'plural' : 'singular'] = $token[1];
					$position++;
				}
			} else {
				if (isset($tokens[$key + 1]) && $tokens[$key + 1] === '(') {
					if ($token[1] === '$t') {
						$open = 'singular';
					} elseif ($token[1] === '$tn') {
						$open = 'plural';
					} else {
						continue;
					}
					$occurrence['line'] = $token[2];
				}
			}
		}
		return $data;
	}