/** * Convert a Protobuf package to a target language one * * @param string $ns * @return string */ public function ns($ns) { // Alias the packages registry to a local var $map =& $this->compiler->packages; // Remove leading dot (used in references) $package = ltrim($ns, '.'); if (empty($package)) { return ''; } if (isset($map[$package])) { return $map[$package]; } // Check the currently registered packages to find a root one $found = null; foreach ($map as $pkg => $ns) { // Keep only the longest match if (0 === strpos($package, $pkg . '.') && strlen($found) < strlen($pkg)) { $found = $pkg; } } // If no matching package was found issue a warning and use the package name if (!$found) { $this->compiler->warning('Non tracked package name found "' . $package . '" '); $namespace = str_replace('.', $this->nsSep, $package); } else { // Complete the namespace with the remaining package $namespace = $map[$found]; $namespace .= substr($package, strlen($found)); $namespace = str_replace('.', $this->nsSep, $namespace); // Set the newly found namespace in the registry $map[$package] = $namespace; } return $namespace; }