1<?php 2/** 3 * This file is part of phpDocumentor. 4 * 5 * For the full copyright and license information, please view the LICENSE 6 * file that was distributed with this source code. 7 * 8 * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org> 9 * @license http://www.opensource.org/licenses/mit-license.php MIT 10 * @link http://phpdoc.org 11 */ 12 13namespace phpDocumentor\Reflection\Types; 14 15/** 16 * Provides information about the Context in which the DocBlock occurs that receives this context. 17 * 18 * A DocBlock does not know of its own accord in which namespace it occurs and which namespace aliases are applicable 19 * for the block of code in which it is in. This information is however necessary to resolve Class names in tags since 20 * you can provide a short form or make use of namespace aliases. 21 * 22 * The phpDocumentor Reflection component knows how to create this class but if you use the DocBlock parser from your 23 * own application it is possible to generate a Context class using the ContextFactory; this will analyze the file in 24 * which an associated class resides for its namespace and imports. 25 * 26 * @see ContextFactory::createFromClassReflector() 27 * @see ContextFactory::createForNamespace() 28 */ 29final class Context 30{ 31 /** @var string The current namespace. */ 32 private $namespace; 33 34 /** @var array List of namespace aliases => Fully Qualified Namespace. */ 35 private $namespaceAliases; 36 37 /** 38 * Initializes the new context and normalizes all passed namespaces to be in Qualified Namespace Name (QNN) 39 * format (without a preceding `\`). 40 * 41 * @param string $namespace The namespace where this DocBlock resides in. 42 * @param array $namespaceAliases List of namespace aliases => Fully Qualified Namespace. 43 */ 44 public function __construct($namespace, array $namespaceAliases = []) 45 { 46 $this->namespace = ('global' !== $namespace && 'default' !== $namespace) 47 ? trim((string)$namespace, '\\') 48 : ''; 49 50 foreach ($namespaceAliases as $alias => $fqnn) { 51 if ($fqnn[0] === '\\') { 52 $fqnn = substr($fqnn, 1); 53 } 54 if ($fqnn[strlen($fqnn) - 1] === '\\') { 55 $fqnn = substr($fqnn, 0, -1); 56 } 57 58 $namespaceAliases[$alias] = $fqnn; 59 } 60 61 $this->namespaceAliases = $namespaceAliases; 62 } 63 64 /** 65 * Returns the Qualified Namespace Name (thus without `\` in front) where the associated element is in. 66 * 67 * @return string 68 */ 69 public function getNamespace() 70 { 71 return $this->namespace; 72 } 73 74 /** 75 * Returns a list of Qualified Namespace Names (thus without `\` in front) that are imported, the keys represent 76 * the alias for the imported Namespace. 77 * 78 * @return string[] 79 */ 80 public function getNamespaceAliases() 81 { 82 return $this->namespaceAliases; 83 } 84} 85