-
Notifications
You must be signed in to change notification settings - Fork 127
Make Context leaner and enable third parties to create them #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org> | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT | ||
* @link http://phpdoc.org | ||
*/ | ||
|
||
namespace phpDocumentor\Reflection\DocBlock; | ||
|
||
/** | ||
* Provides information about the Context in which the DocBlock occurs that receives this context. | ||
* | ||
* A DocBlock does not know of its own accord in which namespace it occurs and which namespace aliases are applicable | ||
* for the block of code in which it is in. This information is however necessary to resolve Class names in tags since | ||
* you can provide a short form or make use of namespace aliases. | ||
* | ||
* The phpDocumentor Reflection component knows how to create this class but if you use the DocBlock parser from your | ||
* own application it is possible to generate a Context class using the ContextFactory; this will analyze the file in | ||
* which an associated class resides for its namespace and imports. | ||
* | ||
* @see ContextFactory::createFromClassReflector() | ||
* @see ContextFactory::createForNamespace() | ||
*/ | ||
final class Context | ||
{ | ||
/** @var string The current namespace. */ | ||
private $namespace = ''; | ||
|
||
/** @var array List of namespace aliases => Fully Qualified Namespace. */ | ||
private $namespaceAliases = []; | ||
|
||
/** | ||
* Initializes the new context and normalizes all passed namespaces to be in Qualified Namespace Name (QNN) | ||
* format (without a preceding `\`). | ||
* | ||
* @param string $namespace The namespace where this DocBlock resides in. | ||
* @param array $namespaceAliases List of namespace aliases => Fully Qualified Namespace. | ||
*/ | ||
public function __construct($namespace, array $namespaceAliases = []) | ||
{ | ||
$this->namespace = ('global' !== $namespace && 'default' !== $namespace) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. global and default should be class consts, But I don't get it yet why global and default are not allowed as namespace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backwards compatibility with parts of phpDocumentor that have been there for a long time; I cannot predict what will break if I remove them. Perhaps after we have cleaned up the rest of the app? |
||
? trim((string)$namespace, '\\') | ||
: ''; | ||
|
||
foreach ($namespaceAliases as $alias => $fqnn) { | ||
$this->namespaceAliases[$alias] = trim((string)$fqnn, '\\'); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the Qualified Namespace Name (thus without `\` in front) where the associated element is in. | ||
* | ||
* @return string | ||
*/ | ||
public function getNamespace() | ||
{ | ||
return $this->namespace; | ||
} | ||
|
||
/** | ||
* Returns a list of Qualified Namespace Names (thus without `\` in front) that are imported, the keys represent | ||
* the alias for the imported Namespace. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getNamespaceAliases() | ||
{ | ||
return $this->namespaceAliases; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
/** | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org> | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT | ||
* @link http://phpdoc.org | ||
*/ | ||
|
||
namespace phpDocumentor\Reflection\DocBlock; | ||
|
||
/** | ||
* Convenience class to create a Context for DocBlocks when not using the Reflection Component of phpDocumentor. | ||
* | ||
* For a DocBlock to be able to resolve types that use partial namespace names or rely on namespace imports we need to | ||
* provide a bit of context so that the DocBlock can read that and based on it decide how to resolve the types to | ||
* Fully Qualified names. | ||
* | ||
* @see Context for more information. | ||
*/ | ||
final class ContextFactory | ||
{ | ||
/** The literal used at the end of a use statement. */ | ||
const T_LITERAL_END_OF_USE = ';'; | ||
|
||
/** The literal used between sets of use statements */ | ||
const T_LITERAL_USE_SEPARATOR = ','; | ||
|
||
/** | ||
* Build a Context given a Class Reflection. | ||
* | ||
* @param \ReflectionClass $class | ||
* | ||
* @see Context for more information on Contexts. | ||
* | ||
* @return Context | ||
*/ | ||
public function createFromClassReflector(\ReflectionClass $class) | ||
{ | ||
return $this->createForNamespace( | ||
$class->getNamespaceName(), | ||
file_get_contents($class->getFileName()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might conflict with applictions fetching files which are not directly accessable. An adapter could be a solution? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you have a ReflectionClass instance than that means the file can be loaded using autoloading and thus it must be local; I do not immediately see how this situation can occur; can you think of a use-case? |
||
); | ||
} | ||
|
||
/** | ||
* Build a Context for a namespace in the provided file contents. | ||
* | ||
* @param string $namespace It does not matter if a `\` precedes the namespace name, this method first normalizes. | ||
* @param string $fileContents the file's contents to retrieve the aliases from with the given namespace. | ||
* | ||
* @see Context for more information on Contexts. | ||
* | ||
* @return Context | ||
*/ | ||
public function createForNamespace($namespace, $fileContents) | ||
{ | ||
$namespace = trim($namespace, '\\'); | ||
$useStatements = []; | ||
$currentNamespace = ''; | ||
$tokens = new \ArrayIterator(token_get_all($fileContents)); | ||
|
||
while ($tokens->valid()) { | ||
switch ($tokens->current()[0]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is [0] always available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [0] is, [1] and [2] not always |
||
case T_NAMESPACE: | ||
$currentNamespace = $this->parseNamespace($tokens); | ||
break; | ||
case T_USE: | ||
if ($currentNamespace === $namespace) { | ||
$useStatements = array_merge($useStatements, $this->parseUseStatement($tokens)); | ||
} | ||
break; | ||
} | ||
$tokens->next(); | ||
} | ||
|
||
return new Context($namespace, $useStatements); | ||
} | ||
|
||
/** | ||
* Deduce the name from tokens when we are at the T_NAMESPACE token. | ||
* | ||
* @param \ArrayIterator $tokens | ||
* | ||
* @return string | ||
*/ | ||
private function parseNamespace(\ArrayIterator $tokens) | ||
{ | ||
// skip to the first string or namespace separator | ||
$this->skipToNextStringOrNamespaceSeparator($tokens); | ||
|
||
$name = ''; | ||
while ($tokens->valid() && ($tokens->current()[0] === T_STRING || $tokens->current()[0] === T_NS_SEPARATOR) | ||
) { | ||
$name .= $tokens->current()[1]; | ||
$tokens->next(); | ||
} | ||
|
||
return $name; | ||
} | ||
|
||
/** | ||
* Deduce the names of all imports when we are at the T_USE token. | ||
* | ||
* @param \ArrayIterator $tokens | ||
* | ||
* @return string[] | ||
*/ | ||
private function parseUseStatement(\ArrayIterator $tokens) | ||
{ | ||
$uses = []; | ||
$continue = true; | ||
|
||
while ($continue) { | ||
$this->skipToNextStringOrNamespaceSeparator($tokens); | ||
|
||
list($alias, $fqnn) = $this->extractUseStatement($tokens); | ||
$uses[$alias] = $fqnn; | ||
if ($tokens->current()[0] === self::T_LITERAL_END_OF_USE) { | ||
$continue = false; | ||
} | ||
} | ||
|
||
return $uses; | ||
} | ||
|
||
/** | ||
* Fast-forwards the iterator as longs as we don't encounter a T_STRING or T_NS_SEPARATOR token. | ||
* | ||
* @param \ArrayIterator $tokens | ||
* | ||
* @return void | ||
*/ | ||
private function skipToNextStringOrNamespaceSeparator(\ArrayIterator $tokens) | ||
{ | ||
while ($tokens->valid() && ($tokens->current()[0] !== T_STRING) && ($tokens->current()[0] !== T_NS_SEPARATOR)) { | ||
$tokens->next(); | ||
} | ||
} | ||
|
||
/** | ||
* Deduce the namespace name and alias of an import when we are at the T_USE token or have not reached the end of | ||
* a USE statement yet. | ||
* | ||
* @param \ArrayIterator $tokens | ||
* | ||
* @return string | ||
*/ | ||
private function extractUseStatement(\ArrayIterator $tokens) | ||
{ | ||
$result = ['']; | ||
while ($tokens->valid() | ||
&& ($tokens->current()[0] !== self::T_LITERAL_USE_SEPARATOR) | ||
&& ($tokens->current()[0] !== self::T_LITERAL_END_OF_USE) | ||
) { | ||
if ($tokens->current()[0] === T_AS) { | ||
$result[] = ''; | ||
} | ||
if ($tokens->current()[0] === T_STRING || $tokens->current()[0] === T_NS_SEPARATOR) { | ||
$result[count($result) - 1] .= $tokens->current()[1]; | ||
} | ||
$tokens->next(); | ||
} | ||
|
||
if (count($result) == 1) { | ||
$result[] = substr($result[0], strrpos($result[0], '\\') + 1); | ||
} | ||
|
||
return array_reverse($result); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
$namespace
required? This is a BC break, the previous default was''
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the short array syntax isn't supported in 5.3. So if this was intentional, bump the composer.json requirements.