Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added feature of viewing method source code without external links #232

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

Merged
samdark merged 10 commits into yiisoft:master from arogachev:147-method-source-code
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
10 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CHANGELOG.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ 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)
- Enh #147: Added feature of viewing method source code without external links (arogachev)


2.1.6 May 05, 2021
Expand Down
2 changes: 1 addition & 1 deletion models/BaseDoc.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BaseDoc extends BaseObject

/**
* @param Type|null $aggregatedType
* @return string[]|array
* @return string[]
*/
protected function splitTypes($aggregatedType)
{
Expand Down
9 changes: 9 additions & 0 deletions models/MethodDoc.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class MethodDoc extends FunctionDoc
public $visibility;
// will be set by creating class
public $definedBy;
/**
* @var string
*/
public $sourceCode = '';


/**
Expand All @@ -44,5 +48,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);
Copy link
Contributor Author

@arogachev arogachev Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 can be calculated dynamically. 🤔

bizley reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is "4" exactly?

Copy link
Member

@samdark samdark Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number of spaces.

arogachev reacted with thumbs up emoji
Copy link
Contributor Author

@arogachev arogachev Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's the number of spaces we need to remove from the beginning of each line. I thought of existence of cases with different amount of spaces used for indentation, tabs instead of spaces and so on. But we discussed it with @samdark and decided to leave it as is now.

}
}
}
5 changes: 5 additions & 0 deletions templates/html/ApiRenderer.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,12 +103,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);

Expand Down
20 changes: 20 additions & 0 deletions templates/html/views/methodDetails.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -90,6 +91,25 @@
<?php endif; ?>
</table>

<?php
$sourceCode = $method->sourceCode;
$collapseId = 'collapse' . ucfirst($method->name);
?>

<p>
<a class="btn btn-link" data-toggle="collapse" href="#<?= $collapseId ?>" role="button" aria-expanded="false"
aria-controls="<?= $collapseId ?>">
Source code
</a>
</p>
<div class="collapse" id="<?= $collapseId ?>">
<div class="card card-body">
<pre>
<code class="hljs php language-php"><?= $highlighter->highlight('php', $sourceCode)->value ?></code>
</pre>
</div>
</div>

<?= $this->render('@yii/apidoc/templates/html/views/todos', ['doc' => $method]) ?>

<?php endforeach; ?>
Expand Down
3 changes: 2 additions & 1 deletion templates/html/views/type.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
?>
Expand Down Expand Up @@ -105,7 +106,7 @@
<?= $this->render('@yii/apidoc/templates/html/views/constSummary', ['type' => $type]) ?>

<?= $this->render('@yii/apidoc/templates/html/views/propertyDetails', ['type' => $type]) ?>
<?= $this->render('@yii/apidoc/templates/html/views/methodDetails', ['type' => $type]) ?>
<?= $this->render('@yii/apidoc/templates/html/views/methodDetails', ['type' => $type, 'highlighter' => $highlighter]) ?>
<?php if ($type instanceof ClassDoc): ?>
<?= $this->render('@yii/apidoc/templates/html/views/eventDetails', ['type' => $type]) ?>
<?php endif; ?>
4 changes: 2 additions & 2 deletions tests/data/api/animal/Cat.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
}
4 changes: 2 additions & 2 deletions tests/data/api/animal/Dog.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
}

AltStyle によって変換されたページ (->オリジナル) /