From da1617340d2626829b7c99b0870e04697224f0ea Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 5 Jan 2014 17:45:56 +0100 Subject: [PATCH] finished offline template summaries --- .../yii/apidoc/components/OfflineRenderer.php | 88 ++++++++++++++++------ extensions/yii/apidoc/models/ClassDoc.php | 6 ++ extensions/yii/apidoc/models/Context.php | 42 ++++++++++- extensions/yii/apidoc/models/TypeDoc.php | 53 +++++++++++++ extensions/yii/apidoc/views/class.php | 38 ++++++---- extensions/yii/apidoc/views/classSummary.php | 79 +++++++++---------- extensions/yii/apidoc/views/constSummary.php | 34 +++++++++ extensions/yii/apidoc/views/eventSummary.php | 34 +++++++++ extensions/yii/apidoc/views/index.php | 2 +- extensions/yii/apidoc/views/methodSummary.php | 40 ++++++++++ extensions/yii/apidoc/views/propertySummary.php | 42 +++++++++++ 11 files changed, 372 insertions(+), 86 deletions(-) create mode 100644 extensions/yii/apidoc/views/constSummary.php create mode 100644 extensions/yii/apidoc/views/eventSummary.php create mode 100644 extensions/yii/apidoc/views/methodSummary.php create mode 100644 extensions/yii/apidoc/views/propertySummary.php diff --git a/extensions/yii/apidoc/components/OfflineRenderer.php b/extensions/yii/apidoc/components/OfflineRenderer.php index 509c51d..a3660f7 100644 --- a/extensions/yii/apidoc/components/OfflineRenderer.php +++ b/extensions/yii/apidoc/components/OfflineRenderer.php @@ -8,6 +8,10 @@ namespace yii\apidoc\components; +use yii\apidoc\models\ConstDoc; +use yii\apidoc\models\EventDoc; +use yii\apidoc\models\MethodDoc; +use yii\apidoc\models\PropertyDoc; use yii\base\ViewContextInterface; use yii\console\Controller; use yii\helpers\Console; @@ -85,17 +89,50 @@ class OfflineRenderer extends BaseRenderer implements ViewContextInterface } /** - * creates a link to an item - * @param ClassDoc|InterfaceDoc|TraitDoc $item + * creates a link to a type (class, interface or trait) + * @param ClassDoc|InterfaceDoc|TraitDoc $types * @param string $title * @return string */ - public function link($item, $title = null) + public function typeLink($types, $title = null) + { + if (!is_array($types)) { + $types = [$types]; + } + $links = []; + foreach($types as $type) { + if (!is_object($type) && ($t = $this->context->getType($type)) !== null) { + $type = $t; + } + if (!is_object($type)) { + $links[] = $type; + } else { + $links[] = Html::a( + $title !== null ? $title : $type->name, + null, + ['href' => $this->generateFileName($type->name)] + ); + } + } + return implode('|', $links); + } + + /** + * creates a link to a subject + * @param PropertyDoc|MethodDoc|ConstDoc|EventDoc $subject + * @param string $title + * @return string + */ + public function subjectLink($subject, $title = null) { if ($title === null) { - $title = $item->name; + $title = $subject->name; + } + if (($type = $this->context->getType($subject->definedBy)) === null) { + return $subject->name; + } else { + return Html::a($title, null, ['href' => $this->generateFileName($type->name) . '#' . $subject->name . '-detail']); } - return Html::a($title, null, ['href' => $this->generateFileName($item->name)]); } /** @@ -104,11 +141,11 @@ class OfflineRenderer extends BaseRenderer implements ViewContextInterface */ public function renderInheritance($class) { - $parents[] = $this->link($class); + $parents[] = $this->typeLink($class); while ($class->parentClass !== null) { if(isset($this->context->classes[$class->parentClass])) { $class = $this->context->classes[$class->parentClass]; - $parents[] = $this->link($class); + $parents[] = $this->typeLink($class); } else { $parents[] = $class->parentClass; // TODO link to php.net break; @@ -118,15 +155,16 @@ class OfflineRenderer extends BaseRenderer implements ViewContextInterface } /** - * @param ClassDoc $class + * @param array $names * @return string */ - public function renderImplements($class) + public function renderInterfaces($names) { $interfaces = []; - foreach($class->interfaces as $interface) { + sort($names, SORT_STRING); + foreach($names as $interface) { if(isset($this->context->interfaces[$interface])) { - $interfaces[] = $this->link($this->context->interfaces[$interface]); + $interfaces[] = $this->typeLink($this->context->interfaces[$interface]); } else { $interfaces[] = $interface; // TODO link to php.net } @@ -135,15 +173,16 @@ class OfflineRenderer extends BaseRenderer implements ViewContextInterface } /** - * @param ClassDoc|TraitDoc $class + * @param array $names * @return string */ - public function renderTraitUses($class) + public function renderTraits($names) { $traits = []; - foreach($class->traits as $trait) { + sort($names, SORT_STRING); + foreach($names as $trait) { if(isset($this->context->traits[$trait])) { - $traits[] = $this->link($this->context->traits[$trait]); + $traits[] = $this->typeLink($this->context->traits[$trait]); } else { $traits[] = $trait; // TODO link to php.net } @@ -151,17 +190,22 @@ class OfflineRenderer extends BaseRenderer implements ViewContextInterface return implode(', ',$traits); } - public function renderSubclasses($class) + /** + * @param array $names + * @return string + */ + public function renderClasses($names) { - $subclasses = []; - foreach($class->subclasses as $subclass) { - if(isset($this->context->classes[$subclass])) { - $subclasses[] = $this->link($this->context->classes[$subclass]); + $classes = []; + sort($names, SORT_STRING); + foreach($names as $class) { + if(isset($this->context->classes[$class])) { + $classes[] = $this->typeLink($this->context->classes[$class]); } else { - $subclasses[] = $subclass; // TODO link to php.net + $classes[] = $class; // TODO link to php.net } } - return implode(', ',$subclasses); + return implode(', ',$classes); } diff --git a/extensions/yii/apidoc/models/ClassDoc.php b/extensions/yii/apidoc/models/ClassDoc.php index 9bb7191..7e7e24b 100644 --- a/extensions/yii/apidoc/models/ClassDoc.php +++ b/extensions/yii/apidoc/models/ClassDoc.php @@ -22,7 +22,13 @@ class ClassDoc extends TypeDoc // will be set by Context::updateReferences() public $subclasses = []; + /** + * @var EventDoc[] + */ public $events = []; + /** + * @var ConstDoc[] + */ public $constants = []; diff --git a/extensions/yii/apidoc/models/Context.php b/extensions/yii/apidoc/models/Context.php index c3b3246..0670140 100644 --- a/extensions/yii/apidoc/models/Context.php +++ b/extensions/yii/apidoc/models/Context.php @@ -32,6 +32,18 @@ class Context extends Component public $traits = []; + public function getType($type) + { + if (isset($this->classes[$type])) { + return $this->classes[$type]; + } elseif (isset($this->interfaces[$type])) { + return $this->interfaces[$type]; + } elseif (isset($this->traits[$type])) { + return $this->traits[$type]; + } + return null; + } + public function addFile($fileName) { if (isset($this->files[$fileName])) { @@ -107,7 +119,6 @@ class Context extends Component } // update interfaces of subclasses foreach($this->classes as $class) { - // TODO do the same for events, constants, methods, properties $this->updateSubclassInferfacesTraits($class); } // update implementedBy and usedBy for interfaces and traits @@ -119,10 +130,17 @@ class Context extends Component } foreach($class->traits as $trait) { if (isset($this->traits[$trait])) { - $this->traits[$trait]->usedBy[] = $class->name; + $trait = $this->traits[$trait]; + $trait->usedBy[] = $class->name; + $class->properties = array_merge($trait->properties, $class->properties); // TODO make unique + $class->methods = array_merge($trait->methods, $class->methods); // TODO make unique } } } + // update properties, methods, contants and events of subclasses + foreach($this->classes as $class) { + $this->updateSubclassInheritance($class); + } } /** @@ -135,7 +153,27 @@ class Context extends Component $subclass = $this->classes[$subclass]; $subclass->interfaces = array_unique(array_merge($subclass->interfaces, $class->interfaces)); $subclass->traits = array_unique(array_merge($subclass->traits, $class->traits)); + $subclass->events = array_merge($class->events, $subclass->events); // TODO make unique + $subclass->constants = array_merge($class->constants, $subclass->constants); // TODO make unique + $subclass->properties = array_merge($class->properties, $subclass->properties); // TODO make unique + $subclass->methods = array_merge($class->methods, $subclass->methods); // TODO make unique $this->updateSubclassInferfacesTraits($subclass); } } + + /** + * Add implemented interfaces and used traits to subclasses + * @param ClassDoc $class + */ + protected function updateSubclassInheritance($class) + { + foreach($class->subclasses as $subclass) { + $subclass = $this->classes[$subclass]; + $subclass->events = array_merge($class->events, $subclass->events); // TODO make unique + $subclass->constants = array_merge($class->constants, $subclass->constants); // TODO make unique + $subclass->properties = array_merge($class->properties, $subclass->properties); // TODO make unique + $subclass->methods = array_merge($class->methods, $subclass->methods); // TODO make unique + $this->updateSubclassInheritance($subclass); + } + } } \ No newline at end of file diff --git a/extensions/yii/apidoc/models/TypeDoc.php b/extensions/yii/apidoc/models/TypeDoc.php index a1349a4..7ba1027 100644 --- a/extensions/yii/apidoc/models/TypeDoc.php +++ b/extensions/yii/apidoc/models/TypeDoc.php @@ -13,9 +13,61 @@ use yii\base\Exception; class TypeDoc extends BaseDoc { public $authors = []; + /** + * @var MethodDoc[] + */ public $methods = []; + /** + * @var PropertyDoc[] + */ public $properties = []; + + public function getPublicMethods() + { + return $this->getFilteredMethods('public'); + } + + public function getProtectedMethods() + { + return $this->getFilteredMethods('protected'); + } + + private function getFilteredMethods($visibility) + { + $methods = []; + foreach($this->methods as $method) { + if ($method->visibility == $visibility) { + $methods[] = $method; + } + } + return $methods; + } + + public function getPublicProperties() + { + return $this->getFilteredProperties('public'); + } + + public function getProtectedProperties() + { + return $this->getFilteredProperties('protected'); + } + + private function getFilteredProperties($visibility) + { + if ($this->properties === null) { + return []; + } + $properties = []; + foreach($this->properties as $property) { + if ($property->visibility == $visibility) { + $properties[] = $property; + } + } + return $properties; + } + /** * @param \phpDocumentor\Reflection\InterfaceReflector $reflector * @param array $config @@ -48,6 +100,7 @@ class TypeDoc extends BaseDoc $method = new MethodDoc($methodReflector); $method->definedBy = $this->name; + // TODO only set property when subclass of Object if (!strncmp($method->name, 'set', 3)) { $propertyName = lcfirst(substr($method->name, 3)); if (isset($this->properties[$propertyName])) { diff --git a/extensions/yii/apidoc/views/class.php b/extensions/yii/apidoc/views/class.php index 5c0ddaa..f3db8cf 100644 --- a/extensions/yii/apidoc/views/class.php +++ b/extensions/yii/apidoc/views/class.php @@ -26,30 +26,36 @@ use yii\apidoc\models\TraitDoc; echo $item->name; ?> -render('classSummary', ['item' => $item]); ?> +render('classSummary', ['item' => $item]) ?> -renderPartial('propertySummary',array('class'=>$item,'protected'=>false)); ?> -renderPartial('propertySummary',array('class'=>$item,'protected'=>true)); ?> +render('propertySummary', ['item' => $item,'protected' => false]) ?> +render('propertySummary', ['item' => $item,'protected' => true]) ?> -renderPartial('methodSummary',array('class'=>$item,'protected'=>false)); ?> -renderPartial('methodSummary',array('class'=>$item,'protected'=>true)); ?> +render('methodSummary', ['item' => $item, 'protected' => false]) ?> +render('methodSummary', ['item' => $item, 'protected' => true]) ?> -renderPartial('eventSummary',array('class'=>$item)); ?> +render('eventSummary', ['item' => $item]) ?> + + +render('constSummary', ['item' => $item]) ?> renderPartial('propertyDetails',array('class'=>$item)); ?> renderPartial('methodDetails',array('class'=>$item)); ?> diff --git a/extensions/yii/apidoc/views/classSummary.php b/extensions/yii/apidoc/views/classSummary.php index 1f6ad9a..2de3811 100644 --- a/extensions/yii/apidoc/views/classSummary.php +++ b/extensions/yii/apidoc/views/classSummary.php @@ -1,60 +1,49 @@ context; ?> -- - - - - - - - - -interfaces)): ?> - - - - - -traits)): ?> - - - - - -subclasses)): ?> - - - - - -since)): ?> - - - - - -version)): ?> - - - - - - - -renderSourceLink($item->sourcePath); ?> - + + + + + + + + interfaces)): ?> + + + traits)): ?> + + + subclasses)): ?> + + + implementedBy)): ?> + + + usedBy)): ?> + + + since)): ?> + + + + + +
Inheritancecontext->renderInheritance($item); ?>
Implementscontext->renderImplements($item); ?>
Uses Traitscontext->renderTraitUses($item); ?>
Subclassescontext->renderSubclasses($item); ?>
Sincesince; ?>
Versionversion; ?>
Source Code
InheritancerenderInheritance($item) ?>
ImplementsrenderInterfaces($item->interfaces) ?>
Uses TraitsrenderTraits($item->traits) ?>
SubclassesrenderClasses($item->subclasses) ?>
Implemented byrenderClasses($item->implementedBy) ?>
Implemented byrenderClasses($item->usedBy) ?>
Available since versionsince ?>
Source CoderenderSourceLink($item->sourcePath) ?>
-description; ?> + shortDescription ?> +

description) ?>

\ No newline at end of file diff --git a/extensions/yii/apidoc/views/constSummary.php b/extensions/yii/apidoc/views/constSummary.php new file mode 100644 index 0000000..7ed2d1a --- /dev/null +++ b/extensions/yii/apidoc/views/constSummary.php @@ -0,0 +1,34 @@ +constants)) { + return; +} ?> +
+

Constants

+ +

Hide inherited constants

+ + ++ + + + + + + +constants as $constant): ?> +definedBy != $item->name ? ' class="inherited"' : '' ?> id="name ?>"> + + + + + +
ConstantDescriptionDefined By
context->subjectLink($constant) ?>shortDescription ?>context->typeLink($constant->definedBy) ?>
+
\ No newline at end of file diff --git a/extensions/yii/apidoc/views/eventSummary.php b/extensions/yii/apidoc/views/eventSummary.php new file mode 100644 index 0000000..72ebafa --- /dev/null +++ b/extensions/yii/apidoc/views/eventSummary.php @@ -0,0 +1,34 @@ +events)) { + return; +} ?> +
+

Events

+ +

Hide inherited events

+ + ++ + + + + + + +events as $event): ?> +definedBy != $item->name ? ' class="inherited"' : '' ?> id="name ?>"> + + + + + +
EventDescriptionDefined By
context->subjectLink($event) ?>shortDescription ?>context->typeLink($event->definedBy) ?>
+
\ No newline at end of file diff --git a/extensions/yii/apidoc/views/index.php b/extensions/yii/apidoc/views/index.php index c329707..bdaa548 100644 --- a/extensions/yii/apidoc/views/index.php +++ b/extensions/yii/apidoc/views/index.php @@ -23,7 +23,7 @@ use yii\apidoc\models\TraitDoc; ksort($items); foreach($items as $i=>$class): ?> - context->link($class, $class->name); ?> + context->typeLink($class, $class->name); ?> shortDescription; ?> diff --git a/extensions/yii/apidoc/views/methodSummary.php b/extensions/yii/apidoc/views/methodSummary.php new file mode 100644 index 0000000..81a2c41 --- /dev/null +++ b/extensions/yii/apidoc/views/methodSummary.php @@ -0,0 +1,40 @@ +getProtectedMethods()) == 0 || !$protected && count($item->getPublicMethods()) == 0) { + return; +} ?> + +
+

+ +

Hide inherited methods

+ + ++ + + + + + + +methods as $method): ?> +visibility == 'protected' || !$protected && $method->visibility != 'protected'): ?> +definedBy != $item->name ? ' class="inherited"' : '' ?> id="name ?>"> + + + + + + +
MethodDescriptionDefined By
context->subjectLink($method, $method->name.'()') ?>shortDescription ?>context->typeLink($method->definedBy) ?>
+
\ No newline at end of file diff --git a/extensions/yii/apidoc/views/propertySummary.php b/extensions/yii/apidoc/views/propertySummary.php new file mode 100644 index 0000000..be6af10 --- /dev/null +++ b/extensions/yii/apidoc/views/propertySummary.php @@ -0,0 +1,42 @@ +getProtectedProperties()) == 0 || !$protected && count($item->getPublicProperties()) == 0) { + return; +} ?> + +
+

+ +

Hide inherited properties

+ + ++ + + + + + + + +properties as $property): ?> +visibility == 'protected' || !$protected && $property->visibility != 'protected'): ?> +definedBy != $item->name ? ' class="inherited"' : '' ?> id="name ?>"> + + + + + + + +
PropertyTypeDescriptionDefined By
context->subjectLink($property); ?>context->typeLink($property->types); ?>shortDescription; ?>context->typeLink($property->definedBy); ?>
+
\ No newline at end of file