From c85153c6fac82bc3c5313be8ae4fd6c98f0900e0 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 30 Nov 2021 13:31:39 +0600 Subject: [PATCH 1/5] Added feature of viewing method source code without external links --- CHANGELOG.md | 1 + models/MethodDoc.php | 9 +++++++++ templates/html/ApiRenderer.php | 5 +++++ templates/html/views/methodDetails.php | 20 +++++++++++++++++++- templates/html/views/type.php | 3 ++- 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16863e23..93361dc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 apidoc extension Change Log - Bug #218: Extended detection of `@inheritdoc` tag in `BaseDoc` (WinterSilence) - Bug #180: Fixed "All Classes" broken link (arogachev) - Bug #34: Improved highlighting of PHP templates (arogachev) +- Enh #147: Added feature of viewing method source code without external links (arogachev) 2.1.6 May 05, 2021 diff --git a/models/MethodDoc.php b/models/MethodDoc.php index 6b0d3299..14004b27 100644 --- a/models/MethodDoc.php +++ b/models/MethodDoc.php @@ -21,6 +21,10 @@ class MethodDoc extends FunctionDoc public $visibility; // will be set by creating class public $definedBy; + /** + * @var string + */ + public $sourceCode = ''; /** @@ -41,5 +45,10 @@ public function __construct($reflector = null, $context = null, $config = []) $this->isStatic = $reflector->isStatic(); $this->visibility = $reflector->getVisibility(); + + $lines = file($this->sourceFile); + for($i = $this->startLine - 1; $i <= $this->endLine - 1; $i++) { + $this->sourceCode .= substr($lines[$i], 4); + } } } diff --git a/templates/html/ApiRenderer.php b/templates/html/ApiRenderer.php index 59deb6cd..10219684 100644 --- a/templates/html/ApiRenderer.php +++ b/templates/html/ApiRenderer.php @@ -7,6 +7,7 @@ namespace yii\apidoc\templates\html; +use Highlight\Highlighter; use yii\apidoc\helpers\ApiMarkdown; use yii\apidoc\models\MethodDoc; use yii\apidoc\models\PropertyDoc; @@ -103,12 +104,16 @@ public function render($context, $targetDir) if ($this->controller !== null) { Console::startProgress(0, $typeCount, 'Rendering files: ', false); } + $done = 0; + $higlighter = new Highlighter; + foreach ($types as $type) { $fileContent = $this->renderWithLayout($this->typeView, [ 'type' => $type, 'apiContext' => $context, 'types' => $types, + 'highlighter' => $higlighter, ]); file_put_contents($targetDir . '/' . $this->generateFileName($type->name), $fileContent); diff --git a/templates/html/views/methodDetails.php b/templates/html/views/methodDetails.php index a769ba21..fef87d59 100644 --- a/templates/html/views/methodDetails.php +++ b/templates/html/views/methodDetails.php @@ -8,6 +8,7 @@ /* @var $type ClassDoc|TraitDoc */ /* @var $this yii\web\View */ /* @var $renderer \yii\apidoc\templates\html\ApiRenderer */ +/* @var $highlighter \Highlight\Highlighter */ $renderer = $this->context; @@ -90,7 +91,24 @@ -renderPartial('sourceCode',array('object'=>$method)); ?> + sourceCode; + $collapseId = 'collapse' . ucfirst($method->name); + ?> + +

+ +

+
+
+
+                highlight('php', $sourceCode)->value ?>
+            
+
+
diff --git a/templates/html/views/type.php b/templates/html/views/type.php index 148f37e1..30ad64ef 100644 --- a/templates/html/views/type.php +++ b/templates/html/views/type.php @@ -8,6 +8,7 @@ /* @var $type ClassDoc|InterfaceDoc|TraitDoc */ /* @var $this yii\web\View */ /* @var $renderer \yii\apidoc\templates\html\ApiRenderer */ +/* @var $highlighter \Highlight\Highlighter */ $renderer = $this->context; ?> @@ -105,7 +106,7 @@ render('@yii/apidoc/templates/html/views/constSummary', ['type' => $type]) ?> render('@yii/apidoc/templates/html/views/propertyDetails', ['type' => $type]) ?> -render('@yii/apidoc/templates/html/views/methodDetails', ['type' => $type]) ?> +render('@yii/apidoc/templates/html/views/methodDetails', ['type' => $type, 'highlighter' => $highlighter]) ?> render('@yii/apidoc/templates/html/views/eventDetails', ['type' => $type]) ?> From b35db8f483acb08cea71fb72fe473f6d0fd1db0d Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 30 Nov 2021 18:45:27 +0600 Subject: [PATCH 2/5] Fix tests --- tests/data/api/animal/Cat.php | 4 ++-- tests/data/api/animal/Dog.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/data/api/animal/Cat.php b/tests/data/api/animal/Cat.php index 466ade70..20fe38ab 100644 --- a/tests/data/api/animal/Cat.php +++ b/tests/data/api/animal/Cat.php @@ -20,7 +20,7 @@ class Cat extends Animal */ public function render() { - // this method has `@inheritdoc` tag in brackets + // this method has `inheritdoc` tag in brackets return 'This is a cat'; } -} \ No newline at end of file +} diff --git a/tests/data/api/animal/Dog.php b/tests/data/api/animal/Dog.php index 5960fd57..de2ad0a2 100644 --- a/tests/data/api/animal/Dog.php +++ b/tests/data/api/animal/Dog.php @@ -20,7 +20,7 @@ class Dog extends Animal */ public function render() { - // this method has `@inheritdoc` tag without brackets + // this method has `inheritdoc` tag without brackets return 'This is a dog'; } -} \ No newline at end of file +} From 29b3ce7323b3e20cff7f9b56ed21a384c775a4d1 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Wed, 1 Dec 2021 12:42:57 +0600 Subject: [PATCH 3/5] Sync CHANGELOG.md with master --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45bd6504..b7267a98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,6 @@ Yii Framework 2 apidoc extension Change Log - Bug #218: Extended detection of `@inheritdoc` tag in `BaseDoc` (WinterSilence) - Bug #180: Fixed "All Classes" broken link (arogachev) - Bug #34: Improved highlighting of PHP templates (arogachev) -- Enh #147: Added feature of viewing method source code without external links (arogachev) - Bug #203: Add PHP 8 compatibility, raise minimum PHP version to 7.2 (bizley, arogachev) - Enh #146: Updated `nikic/php-parser` version (bizley, arogachev) - Bug #213: Fixed error: "Call to undefined method `phpDocumentor\Reflection\Php\Argument::getNode()`" (arogachev) From 716afd779f4455453df2032209a8af26712fe5c5 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 1 Dec 2021 15:12:43 +0300 Subject: [PATCH 4/5] Update models/MethodDoc.php Co-authored-by: Bizley --- models/MethodDoc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/MethodDoc.php b/models/MethodDoc.php index fad119e1..1006459b 100644 --- a/models/MethodDoc.php +++ b/models/MethodDoc.php @@ -50,7 +50,7 @@ public function __construct($reflector = null, $context = null, $config = []) $this->visibility = $reflector->getVisibility(); $lines = file($this->sourceFile); - for($i = $this->startLine - 1; $i <= $this->endLine - 1; $i++) { + for ($i = $this->startLine - 1; $i <= $this->endLine - 1; $i++) { $this->sourceCode .= substr($lines[$i], 4); } } From bca81108a961d289c865842e6ab3e618f8731707 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Thu, 2 Dec 2021 16:40:19 +0600 Subject: [PATCH 5/5] (Chore) Fix changelog order, fix return type --- CHANGELOG.md | 8 ++++---- models/BaseDoc.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6994f9e9..7c992dea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,11 +17,11 @@ Yii Framework 2 apidoc extension Change Log - Bug #179: Fixed incorrect output when string type hint is used in method parameters (arogachev) - Bug #199: Fixed processing of nullable return types (arogachev) - Bug #148: Fixed processing of code containing uniform variable syntax (arogachev) -- Enh #143: Do not include methods and properties marked as internal (arogachev) -- Bug #197: Adapted fixing of Markdown links for multiple links (arogachev) -- Bug #128: Fixed extracting of first sentence from the text containing backticks (arogachev) -- Enh #209: Added support for todos in properties and methods (arogachev) - Enh #18: Added pretty print for arrays (arogachev) +- Enh #209: Added support for todos in properties and methods (arogachev) +- Bug #128: Fixed extracting of first sentence from the text containing backticks (arogachev) +- Bug #197: Adapted fixing of Markdown links for multiple links (arogachev) +- Enh #143: Do not include methods and properties marked as internal (arogachev) 2.1.6 May 05, 2021 diff --git a/models/BaseDoc.php b/models/BaseDoc.php index ab8fbd4b..324064ba 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -50,7 +50,7 @@ class BaseDoc extends BaseObject /** * @param Type|null $aggregatedType - * @return string[]|array + * @return string[] */ protected function splitTypes($aggregatedType) {