Skip to content

Commit

Permalink
added method, method parameters and return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetbarut committed Nov 27, 2021
1 parent 6e926b6 commit 30f25a2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": "7.4 || >=8",
"php": ">=7.4 || 8.*",
"laravel/framework": ">=v8.0.0"
},
"extra": {
Expand Down
30 changes: 28 additions & 2 deletions src/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace AhmetBarut\LaravelRouteDocs;

use Illuminate\Routing\Router;
use ReflectionMethod;
use ReflectionParameter;

class Matcher
{
Expand All @@ -27,7 +29,6 @@ public function getDetails(): array

foreach ($this->route->getRoutes()->getRoutes() as $route) {


if ($route->getPrefix() !== '_ignition')
{
if ($route->getAction('controller') === null)
Expand All @@ -50,13 +51,18 @@ public function getDetails(): array
'prefix' => $route->getAction('prefix'),
],
'comment' => $this->getDocComment($reflection->getDocComment()),
'method' => [
'name' => $reflection->getName(),
'parameters' => $this->getParameters($reflection->getParameters()),
'return' => $reflection->getReturnType(),
],
];
}
}
return $array;
}

public function getDocComment($text): mixed
public function getDocComment($text)
{
preg_match_all('/[a-zA-Z0-9öÖçÇğĞİşŞüÜı\@\-_\\\\]+/mu', $text, $res);
return preg_replace_callback('#@route-doc(.*)@end-doc#', function ($matched){
Expand All @@ -71,4 +77,24 @@ public function resolve(string $namespace)
$this->resolveMethod = $explode[1];
}

public function getParameters(array $parameters): array
{
$array = [];
foreach ($parameters as $parameter) {
$array[] = [
'name' => $parameter->getName(),
'type' => $parameter->getType()->getName(),
'isOptional' => $parameter->isOptional(),
'default' => $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null,
];
}
return $array;
}

public function getReturn($reflection): array
{
return [
'type' => $reflection->getName(),
];
}
}
4 changes: 3 additions & 1 deletion src/RouteDocsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function __construct(Router $route)
public function handle()
{
$path = $this->argument('path') ?? './docs/routes.md';

if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0777, false);
}
$matcher = new Markdown();
$matcher->write($this->routes->getDetails(), $path);

Expand Down
30 changes: 27 additions & 3 deletions src/Template/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public function write($data, $path = './docs/routes.md')
$text = $stubText;
$text = $this->replaceRouteName(trim($route['name']) ?? 'Undefined', $text);
$text = $this->replaceRouteUri(trim($route['uri']), $text);
$text = $this->replaceRouteMethod(trim(implode(', ', $route['methods'])), $text);
$text = $this->replaceRouteRequestMethod(trim(implode(', ', $route['methods'])), $text);
$text = $this->replaceRouteDescription(trim($route['comment']), $text);

$text = $this->replaceMethod($route['method']['name'], $text);
$text = $this->replaceParameter($route['method']['parameters'], $text);
$text = $this->replaceReturnType($route['method']['return'], $text);
$allText .= $text;
}
file_put_contents($path, $allText);
Expand Down Expand Up @@ -70,8 +72,30 @@ public function replaceRouteDescription($replace, $text, string $search = '{{des
* @param string $search
* @return array|string|string[]
*/
public function replaceRouteMethod($replace, $text, string $search = '{{method}}')
public function replaceRouteRequestMethod($replace, $text, string $search = '{{RequestMethod}}')
{
return str_replace($search, $replace, $text);
}

public function replaceMethod($replace, $text, string $search = '{{method}}')
{
return str_replace($search, $replace, $text);
}

public function replaceParameter($replace, $text, string $search = '{{parameters}}')
{
$param = "";
foreach ($replace as $parameter) {
$param = "({$parameter['type']} \${$parameter['name']})";
}
return str_replace($search, $param, $text);
}

public function replaceReturnType($replace, $text, string $search = '{{return_type}}')
{
if ($replace !== null) {
return str_replace($search, ': ' . $replace, $text);
}
return str_replace($search, $replace, $text);
}
}
3 changes: 2 additions & 1 deletion src/stubs.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### # {{route_name}}
#### # {{uri}}
#### # {{method}}
#### # {{RequestMethod}}
#### # {{method}} {{parameters}} {{return_type}}
{{description}}

---------------------------

0 comments on commit 30f25a2

Please sign in to comment.