/** * Constructor. * * Sets the inspected reflection. * * @param \TokenReflection\IReflection $reflection Inspected reflection * @param \ApiGen\Generator $generator ApiGen generator */ public function __construct(IReflection $reflection, Generator $generator) { if (null === self::$generator) { self::$generator = $generator; self::$config = $generator->getConfig(); self::$parsedClasses = $generator->getParsedClasses(); self::$parsedConstants = $generator->getParsedConstants(); self::$parsedFunctions = $generator->getParsedFunctions(); } $this->reflectionType = get_class($this); if (!isset(self::$reflectionMethods[$this->reflectionType])) { self::$reflectionMethods[$this->reflectionType] = array_flip(get_class_methods($this)); } $this->reflection = $reflection; }
/** * Prepares and returns used class lists. * * @return array */ protected function parseClassLists() { $allClasses = array(self::TOKENIZED_CLASSES => array(), self::INTERNAL_CLASSES => array(), self::NONEXISTENT_CLASSES => array()); $declared = array_flip(array_merge(get_declared_classes(), get_declared_interfaces())); foreach ($this->getNamespaces() as $namespace) { foreach ($namespace->getClasses() as $name => $trClass) { $class = new ReflectionClass($trClass, $this->generator); $allClasses[self::TOKENIZED_CLASSES][$name] = $class; if (!$class->isDocumented()) { continue; } foreach (array_merge($trClass->getParentClasses(), $trClass->getInterfaces()) as $parentName => $parent) { if ($parent->isInternal()) { if (!isset($allClasses[self::INTERNAL_CLASSES][$parentName])) { $allClasses[self::INTERNAL_CLASSES][$parentName] = $parent; } } elseif (!$parent->isTokenized()) { if (!isset($allClasses[self::NONEXISTENT_CLASSES][$parentName])) { $allClasses[self::NONEXISTENT_CLASSES][$parentName] = $parent; } } } $this->generator->checkMemory(); } } foreach ($allClasses[self::TOKENIZED_CLASSES] as $class) { if (!$class->isDocumented()) { continue; } foreach ($class->getOwnMethods() as $method) { $allClasses = $this->processFunction($declared, $allClasses, $method); } foreach ($class->getOwnProperties() as $property) { $annotations = $property->getAnnotations(); if (!isset($annotations['var'])) { continue; } foreach ($annotations['var'] as $doc) { foreach (explode('|', preg_replace('~\\s.*~', '', $doc)) as $name) { if ($name = rtrim($name, '[]')) { $name = Resolver::resolveClassFQN($name, $class->getNamespaceAliases(), $class->getNamespaceName()); $allClasses = $this->addClass($declared, $allClasses, $name); } } } } $this->generator->checkMemory(); } foreach ($this->getFunctions() as $function) { $allClasses = $this->processFunction($declared, $allClasses, $function); } array_walk_recursive($allClasses, function (&$reflection, $name, Generator $generator) { if (!$reflection instanceof ReflectionClass) { $reflection = new ReflectionClass($reflection, $generator); } }, $this->generator); return $allClasses; }
/** * Tries to parse a definition of a class/method/property/constant/function and returns the appropriate link if successful. * * @param string $definition Definition * @param \ApiGen\ReflectionElement $context Link context * * @return string|null */ public function resolveLink($definition, ReflectionElement $context) { if (empty($definition)) { return null; } $suffix = ''; if ('[]' === substr($definition, -2)) { $definition = substr($definition, 0, -2); $suffix = '[]'; } $element = $this->generator->resolveElement($definition, $context, $expectedName); if (null === $element) { return $expectedName; } $classes = array(); if ($element->isDeprecated()) { $classes[] = 'deprecated'; } if (!$element->isValid()) { $classes[] = 'invalid'; } if ($element instanceof ReflectionClass) { $link = $this->link($this->getClassUrl($element), $element->getName(), true, $classes); } elseif ($element instanceof ReflectionConstant && null === $element->getDeclaringClassName()) { $text = $element->inNamespace() ? $this->escapeHtml($element->getNamespaceName()) . '\\<b>' . $this->escapeHtml($element->getShortName()) . '</b>' : '<b>' . $this->escapeHtml($element->getName()) . '</b>'; $link = $this->link($this->getConstantUrl($element), $text, false, $classes); } elseif ($element instanceof ReflectionFunction) { $link = $this->link($this->getFunctionUrl($element), $element->getName() . '()', true, $classes); } else { $text = $this->escapeHtml($element->getDeclaringClassName()); if ($element instanceof ReflectionProperty) { $url = $this->propertyUrl($element); $text .= '::<var>$' . $this->escapeHtml($element->getName()) . '</var>'; } elseif ($element instanceof ReflectionMethod) { $url = $this->methodUrl($element); $text .= '::' . $this->escapeHtml($element->getName()) . '()'; } elseif ($element instanceof ReflectionConstant) { $url = $this->constantUrl($element); $text .= '::<b>' . $this->escapeHtml($element->getName()) . '</b>'; } $link = $this->link($url, $text, false, $classes); } return sprintf('<code>%s</code>', $link . $suffix); }
} if (!class_exists('TokenReflection\\Broker')) { echo "Required dependency missing: TokenReflection library\n"; die(1); } Debugger::$strictMode = true; Debugger::$onFatalError[] = function () { echo "\nFor more information turn on the debug mode using the --debug option.\n"; }; Debugger::enable(Debugger::PRODUCTION, false); $start = new \DateTime(); $options = $_SERVER['argv']; array_shift($options); $config = new Config(); $config->processCliOptions($options); $generator = new Generator($config); // Help if ($config->isHelpRequested()) { echo $generator->colorize($generator->getHeader()); echo $generator->colorize($config->getHelp()); die; } // Prepare configuration $config->prepare(); if ($config->debug) { Debugger::$onFatalError = array(); Debugger::enable(Debugger::DEVELOPMENT, false); } $generator->output($generator->getHeader()); // Check for update (only in production mode) if ($config->updateCheck && !$config->debug) {
/** * Creates template. * * @param \ApiGen\Generator $generator */ public function __construct(Generator $generator) { $this->generator = $generator; $this->config = $generator->getConfig(); $that = $this; // Output in HTML5 Nette\Utils\Html::$xhtml = false; // FSHL $fshl = new FSHL\Highlighter(new FSHL\Output\Html()); $fshl->setLexer(new FSHL\Lexer\Php()); // Texy $this->texy = new \Texy(); $this->texy->allowedTags = array_flip($this->config->allowedHtml); $this->texy->allowed['list/definition'] = false; $this->texy->allowed['phrase/em-alt'] = false; $this->texy->allowed['longwords'] = false; $this->texy->allowed['typography'] = false; $this->texy->linkModule->shorten = false; // Highlighting <code>, <pre> $this->texy->addHandler('beforeParse', function ($texy, &$text, $singleLine) { $text = preg_replace('~<code>(.+?)</code>~', '#code#\\1#/code#', $text); }); $this->texy->registerLinePattern(function ($parser, $matches, $name) use($fshl) { return \TexyHtml::el('code', $fshl->highlight($matches[1])); }, '~#code#(.+?)#/code#~', 'codeInlineSyntax'); $this->texy->registerBlockPattern(function ($parser, $matches, $name) use($fshl) { if ('code' === $matches[1]) { $lines = array_filter(explode("\n", $matches[2])); if (!empty($lines)) { $firstLine = array_shift($lines); $indent = ''; $li = 0; while (isset($firstLine[$li]) && preg_match('~\\s~', $firstLine[$li])) { foreach ($lines as $line) { if (!isset($line[$li]) || $firstLine[$li] !== $line[$li]) { break 2; } } $indent .= $firstLine[$li++]; } if (!empty($indent)) { $matches[2] = str_replace("\n" . $indent, "\n", 0 === strpos($matches[2], $indent) ? substr($matches[2], $li) : $matches[2]); } } $content = $fshl->highlight($matches[2]); } else { $content = htmlspecialchars($matches[2]); } $content = $parser->getTexy()->protect($content, \Texy::CONTENT_BLOCK); return \TexyHtml::el('pre', $content); }, '~<(code|pre)>(.+?)</\\1>~s', 'codeBlockSyntax'); // Common operations $this->registerHelperLoader('Nette\\Templating\\Helpers::loader'); // PHP source highlight $this->registerHelper('highlightPHP', function ($source, $context) use($that, $fshl) { return $that->resolveLink($that->getTypeName($source), $context) ?: $fshl->highlight((string) $source); }); $this->registerHelper('highlightValue', function ($definition, $context) use($that) { return $that->highlightPHP(preg_replace('~^(?:[ ]{4}|\\t)~m', '', $definition), $context); }); // Urls $this->registerHelper('packageUrl', new Nette\Callback($this, 'getPackageUrl')); $this->registerHelper('namespaceUrl', new Nette\Callback($this, 'getNamespaceUrl')); $this->registerHelper('groupUrl', new Nette\Callback($this, 'getGroupUrl')); $this->registerHelper('classUrl', new Nette\Callback($this, 'getClassUrl')); $this->registerHelper('methodUrl', new Nette\Callback($this, 'getMethodUrl')); $this->registerHelper('propertyUrl', new Nette\Callback($this, 'getPropertyUrl')); $this->registerHelper('constantUrl', new Nette\Callback($this, 'getConstantUrl')); $this->registerHelper('functionUrl', new Nette\Callback($this, 'getFunctionUrl')); $this->registerHelper('elementUrl', new Nette\Callback($this, 'getElementUrl')); $this->registerHelper('sourceUrl', new Nette\Callback($this, 'getSourceUrl')); $this->registerHelper('manualUrl', new Nette\Callback($this, 'getManualUrl')); // Packages & namespaces $this->registerHelper('packageLinks', new Nette\Callback($this, 'getPackageLinks')); $this->registerHelper('namespaceLinks', new Nette\Callback($this, 'getNamespaceLinks')); $this->registerHelper('subgroupName', function ($groupName) { if ($pos = strrpos($groupName, '\\')) { return substr($groupName, $pos + 1); } return $groupName; }); // Types $this->registerHelper('typeLinks', new Nette\Callback($this, 'getTypeLinks')); // Docblock descriptions $this->registerHelper('description', function ($annotation, $context) use($that) { $description = trim(strpbrk($annotation, "\n\r\t \$")); if ($context instanceof ReflectionParameter) { $description = preg_replace('~^(\\$' . $context->getName() . '(?:,\\.{3})?)(\\s+|$)~i', '\\2', $description, 1); $context = $context->getDeclaringFunction(); } return $that->doc($description, $context); }); $this->registerHelper('shortDescription', function ($element, $block = false) use($that) { return $that->doc($element->getShortDescription(), $element, $block); }); $this->registerHelper('longDescription', function ($element) use($that) { $long = $element->getLongDescription(); // Merge lines $long = preg_replace_callback('~(?:<(code|pre)>.+?</\\1>)|([^<]*)~s', function ($matches) { return !empty($matches[2]) ? preg_replace('~\\n(?:\\t|[ ])+~', ' ', $matches[2]) : $matches[0]; }, $long); return $that->doc($long, $element, true); }); // Individual annotations processing $this->registerHelper('annotation', function ($value, $name, $context) use($that, $generator) { switch ($name) { case 'param': case 'return': case 'throws': $description = $that->description($value, $context); return sprintf('<code>%s</code>%s', $that->getTypeLinks($value, $context), $description ? '<br>' . $description : ''); case 'license': list($url, $description) = $that->split($value); return $that->link($url, $description ?: $url); case 'link': list($url, $description) = $that->split($value); if (Nette\Utils\Validators::isUrl($url)) { return $that->link($url, $description ?: $url); } break; case 'see': $doc = array(); foreach (preg_split('~\\s*,\\s*~', $value) as $link) { if (null !== $generator->resolveElement($link, $context)) { $doc[] = sprintf('<code>%s</code>', $that->getTypeLinks($link, $context)); } else { $doc[] = $that->doc($link, $context); } } return implode(', ', $doc); case 'uses': case 'usedby': list($link, $description) = $that->split($value); $separator = $context instanceof ReflectionClass || !$description ? ' ' : '<br>'; if (null !== $generator->resolveElement($link, $context)) { return sprintf('<code>%s</code>%s%s', $that->getTypeLinks($link, $context), $separator, $description); } break; default: break; } // Default return $that->doc($value, $context); }); $todo = $this->config->todo; $internal = $this->config->internal; $this->registerHelper('annotationFilter', function (array $annotations, array $filter = array()) use($todo, $internal) { // Filtered, unsupported or deprecated annotations static $filtered = array('package', 'subpackage', 'property', 'property-read', 'property-write', 'method', 'abstract', 'access', 'final', 'filesource', 'global', 'name', 'static', 'staticvar'); foreach ($filtered as $annotation) { unset($annotations[$annotation]); } // Custom filter foreach ($filter as $annotation) { unset($annotations[$annotation]); } // Show/hide internal if (!$internal) { unset($annotations['internal']); } // Show/hide tasks if (!$todo) { unset($annotations['todo']); } return $annotations; }); $this->registerHelper('annotationSort', function (array $annotations) { uksort($annotations, function ($one, $two) { static $order = array('deprecated' => 0, 'category' => 1, 'copyright' => 2, 'license' => 3, 'author' => 4, 'version' => 5, 'since' => 6, 'see' => 7, 'uses' => 8, 'usedby' => 9, 'link' => 10, 'internal' => 11, 'example' => 12, 'tutorial' => 13, 'todo' => 14); if (isset($order[$one], $order[$two])) { return $order[$one] - $order[$two]; } elseif (isset($order[$one])) { return -1; } elseif (isset($order[$two])) { return 1; } else { return strcasecmp($one, $two); } }); return $annotations; }); $this->registerHelper('annotationBeautify', function ($annotation) { static $names = array('usedby' => 'Used by'); if (isset($names[$annotation])) { return $names[$annotation]; } return Nette\Utils\Strings::firstUpper($annotation); }); // Static files versioning $destination = $this->config->destination; $this->registerHelper('staticFile', function ($name) use($destination) { static $versions = array(); $filename = $destination . DIRECTORY_SEPARATOR . $name; if (!isset($versions[$filename]) && is_file($filename)) { $versions[$filename] = sprintf('%u', crc32(file_get_contents($filename))); } if (isset($versions[$filename])) { $name .= '?' . $versions[$filename]; } return $name; }); // Source anchors $this->registerHelper('sourceAnchors', function ($source) { // Classes, interfaces, traits and exceptions $source = preg_replace_callback('~(<span\\s+class="php-keyword1">(?:class|interface|trait)</span>\\s+)(\\w+)~i', function ($matches) { $link = sprintf('<a id="%1$s" href="#%1$s">%1$s</a>', $matches[2]); return $matches[1] . $link; }, $source); // Methods and functions $source = preg_replace_callback('~(<span\\s+class="php-keyword1">function</span>\\s+)(\\w+)~i', function ($matches) { $link = sprintf('<a id="_%1$s" href="#_%1$s">%1$s</a>', $matches[2]); return $matches[1] . $link; }, $source); // Constants $source = preg_replace_callback('~(<span class="php-keyword1">const</span>)(.*?)(;)~is', function ($matches) { $links = preg_replace_callback('~(\\s|,)([A-Z_]+)(\\s+=)~', function ($matches) { return $matches[1] . sprintf('<a id="%1$s" href="#%1$s">%1$s</a>', $matches[2]) . $matches[3]; }, $matches[2]); return $matches[1] . $links . $matches[3]; }, $source); // Properties $source = preg_replace_callback('~(<span\\s+class="php-keyword1">(?:private|protected|public|var|static)</span>\\s+)(<span\\s+class="php-var">.*?)(;)~is', function ($matches) { $links = preg_replace_callback('~(<span\\s+class="php-var">)(\\$\\w+)~i', function ($matches) { return $matches[1] . sprintf('<a id="%1$s" href="#%1$s">%1$s</a>', $matches[2]); }, $matches[2]); return $matches[1] . $links . $matches[3]; }, $source); return $source; }); $this->registerHelper('urlize', array($this, 'urlize')); $this->registerHelper('relativePath', array($generator, 'getRelativePath')); $this->registerHelper('resolveElement', array($generator, 'resolveElement')); $this->registerHelper('getClass', array($generator, 'getClass')); }
/** * Creates template. * * @param \ApiGen\Generator $generator * @param \ApiGen\ISourceCodeHighlighter $highlighter */ public function __construct(Generator $generator, ISourceCodeHighlighter $highlighter) { $this->generator = $generator; $this->config = $generator->getConfig(); // @todo DI $this->markup = new MarkdownMarkup($this->config->allowedHtml, $highlighter); $that = $this; // Output in HTML5 Nette\Utils\Html::$xhtml = false; // Common operations $this->registerHelperLoader('Nette\\Templating\\Helpers::loader'); // PHP source highlight $this->registerHelper('highlightPHP', function ($source, $context) use($that, $highlighter) { return $that->resolveLink($that->getTypeName($source), $context) ?: $highlighter->highlight((string) $source); }); $this->registerHelper('highlightValue', function ($definition, $context) use($that) { return $that->highlightPHP(preg_replace('~^(?:[ ]{4}|\\t)~m', '', $definition), $context); }); // Urls $this->registerHelper('packageUrl', new Nette\Callback($this, 'getPackageUrl')); $this->registerHelper('namespaceUrl', new Nette\Callback($this, 'getNamespaceUrl')); $this->registerHelper('groupUrl', new Nette\Callback($this, 'getGroupUrl')); $this->registerHelper('classUrl', new Nette\Callback($this, 'getClassUrl')); $this->registerHelper('methodUrl', new Nette\Callback($this, 'getMethodUrl')); $this->registerHelper('propertyUrl', new Nette\Callback($this, 'getPropertyUrl')); $this->registerHelper('constantUrl', new Nette\Callback($this, 'getConstantUrl')); $this->registerHelper('functionUrl', new Nette\Callback($this, 'getFunctionUrl')); $this->registerHelper('elementUrl', new Nette\Callback($this, 'getElementUrl')); $this->registerHelper('sourceUrl', new Nette\Callback($this, 'getSourceUrl')); $this->registerHelper('manualUrl', new Nette\Callback($this, 'getManualUrl')); // Packages & namespaces $this->registerHelper('packageLinks', new Nette\Callback($this, 'getPackageLinks')); $this->registerHelper('namespaceLinks', new Nette\Callback($this, 'getNamespaceLinks')); $this->registerHelper('subgroupName', function ($groupName) { if ($pos = strrpos($groupName, '\\')) { return substr($groupName, $pos + 1); } return $groupName; }); // Types $this->registerHelper('typeLinks', new Nette\Callback($this, 'getTypeLinks')); // Docblock descriptions $this->registerHelper('description', function ($annotation, $context) use($that) { $description = trim(strpbrk($annotation, "\n\r\t \$")) ?: $annotation; return $that->doc($description, $context); }); $this->registerHelper('shortDescription', function ($element, $block = false) use($that) { return $that->doc($element->getShortDescription(), $element, $block); }); $this->registerHelper('longDescription', function ($element) use($that) { $long = $element->getLongDescription(); // Merge lines $long = preg_replace_callback('~(?:<(code|pre)>.+?</\\1>)|([^<]*)~s', function ($matches) { return !empty($matches[2]) ? preg_replace('~\\n(?:\\t|[ ])+~', ' ', $matches[2]) : $matches[0]; }, $long); return $that->doc($long, $element, true); }); // Individual annotations processing $this->registerHelper('annotation', function ($value, $name, Reflection\ReflectionElement $context) use($that, $generator) { switch ($name) { case 'return': case 'throws': // TODO: Needs fix - This produces duplicate throws statements // $description = $that->description($value, $context); // return sprintf('<code>%s</code>%s', $that->getTypeLinks($value, $context), $description ? '<br>' . $description : ''); return $that->getTypeLinks($value, $context); case 'license': list($url, $description) = $that->split($value); return $that->link($url, $description ?: $url); case 'link': list($url, $description) = $that->split($value); if (Nette\Utils\Validators::isUrl($url)) { return $that->link($url, $description ?: $url); } break; case 'see': $doc = array(); foreach (preg_split('~\\s*,\\s*~', $value) as $link) { if (null !== $generator->resolveElement($link, $context)) { $doc[] = sprintf('<code>%s</code>', $that->getTypeLinks($link, $context)); } else { $doc[] = $that->doc($link, $context); } } return implode(', ', $doc); case 'uses': case 'usedby': list($link, $description) = $that->split($value); $separator = $context instanceof Reflection\ReflectionClass || !$description ? ' ' : '<br>'; if (null !== $generator->resolveElement($link, $context)) { return sprintf('<code>%s</code>%s%s', $that->getTypeLinks($link, $context), $separator, $description); } break; default: break; } // Default return $that->doc($value, $context); }); $todo = $this->config->todo; $internal = $this->config->internal; $this->registerHelper('annotationFilter', function (array $annotations, array $filter = array()) use($todo, $internal) { // Filtered, unsupported or deprecated annotations static $filtered = array('package', 'subpackage', 'property', 'property-read', 'property-write', 'method', 'abstract', 'access', 'final', 'filesource', 'global', 'name', 'static', 'staticvar'); foreach ($filtered as $annotation) { unset($annotations[$annotation]); } // Custom filter foreach ($filter as $annotation) { unset($annotations[$annotation]); } // Show/hide internal if (!$internal) { unset($annotations['internal']); } // Show/hide tasks if (!$todo) { unset($annotations['todo']); } return $annotations; }); $this->registerHelper('annotationSort', function (array $annotations) { uksort($annotations, function ($one, $two) { static $order = array('deprecated' => 0, 'category' => 1, 'copyright' => 2, 'license' => 3, 'author' => 4, 'version' => 5, 'since' => 6, 'see' => 7, 'uses' => 8, 'usedby' => 9, 'link' => 10, 'internal' => 11, 'example' => 12, 'tutorial' => 13, 'todo' => 14); if (isset($order[$one], $order[$two])) { return $order[$one] - $order[$two]; } elseif (isset($order[$one])) { return -1; } elseif (isset($order[$two])) { return 1; } else { return strcasecmp($one, $two); } }); return $annotations; }); $this->registerHelper('annotationBeautify', function ($annotation) { static $names = array('usedby' => 'Used by'); if (isset($names[$annotation])) { return $names[$annotation]; } return Nette\Utils\Strings::firstUpper($annotation); }); // Static files versioning $destination = $this->config->destination; $this->registerHelper('staticFile', function ($name) use($destination) { static $versions = array(); $filename = $destination . DIRECTORY_SEPARATOR . $name; if (!isset($versions[$filename]) && is_file($filename)) { $versions[$filename] = sprintf('%u', crc32(file_get_contents($filename))); } if (isset($versions[$filename])) { $name .= '?' . $versions[$filename]; } return $name; }); // Source anchors $this->registerHelper('sourceAnchors', function ($source) { // Classes, interfaces, traits and exceptions $source = preg_replace_callback('~(<span\\s+class="php-keyword1">(?:class|interface|trait)</span>\\s+)(\\w+)~i', function ($matches) { $link = sprintf('<a id="%1$s" href="#%1$s">%1$s</a>', $matches[2]); return $matches[1] . $link; }, $source); // Methods and functions $source = preg_replace_callback('~(<span\\s+class="php-keyword1">function</span>\\s+)(\\w+)~i', function ($matches) { $link = sprintf('<a id="_%1$s" href="#_%1$s">%1$s</a>', $matches[2]); return $matches[1] . $link; }, $source); // Constants $source = preg_replace_callback('~(<span class="php-keyword1">const</span>)(.*?)(;)~is', function ($matches) { $links = preg_replace_callback('~(\\s|,)([A-Z_]+)(\\s+=)~', function ($matches) { return $matches[1] . sprintf('<a id="%1$s" href="#%1$s">%1$s</a>', $matches[2]) . $matches[3]; }, $matches[2]); return $matches[1] . $links . $matches[3]; }, $source); // Properties $source = preg_replace_callback('~(<span\\s+class="php-keyword1">(?:private|protected|public|var|static)</span>\\s+)(<span\\s+class="php-var">.*?)(;)~is', function ($matches) { $links = preg_replace_callback('~(<span\\s+class="php-var">)(\\$\\w+)~i', function ($matches) { return $matches[1] . sprintf('<a id="%1$s" href="#%1$s">%1$s</a>', $matches[2]); }, $matches[2]); return $matches[1] . $links . $matches[3]; }, $source); return $source; }); $this->registerHelper('urlize', array($this, 'urlize')); $this->registerHelper('relativePath', array($generator, 'getRelativePath')); $this->registerHelper('resolveElement', array($generator, 'resolveElement')); $this->registerHelper('getClass', array($generator, 'getClass')); }