Skip to content
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

feture to hide parameters of api #26

Merged
merged 19 commits into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Laravel API Docs Generator

The MIT License (MIT)

Copyright (c) 2014 F2M2, Inc. <hello@f2m2.com>
Copyright (c) 2014-2015 F2M2, Inc. <hello@f2m2.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ This Laravel package provides an API Documentation generator based upon your Rou

## Installation

#### Laravel 5.1
#### Laravel 5.0

Begin by installing this package through Composer. Edit your project's `composer.json` file to require `f2m2/apidocs`.

"require": {
"laravel/framework": "~5.1",
"f2m2/apidocs": "~2.0"
"require-dev": {
"f2m2/apidocs": "dev-master"
}

Next, update Composer from the Terminal:
Expand All @@ -20,7 +19,7 @@ Next, update Composer from the Terminal:

Once the packaage has installed, the final step is to add the service provider. Open `config/app.php`, and add a new item to the providers array.

'F2m2\Apidocs\ApidocsServiceProvider::class',
'F2m2\Apidocs\ApidocsServiceProvider',

Run the `artisan` command from the Terminal to see the new `apidocs` command.

Expand All @@ -34,7 +33,7 @@ Create a copy of the API Docs Config by running this `artisan` command:
#### Laravel 4.2

See installation:
https://github.com/f2m2/apidocs/tree/1.0
https://github.com/f2m2/apidocs/tree/1.0.0


Notes
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"homepage": "https://github.com/f2m2/apidocs",
"license": "MIT",
"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"illuminate/support": "5.*",
"php" : ">=5.4.0",
"phpdocumentor/reflection-docblock": "2.0.*@dev"
},
"autoload": {
Expand Down
68 changes: 49 additions & 19 deletions src/Commands/ApiDocsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ApiDocsGenerator {
protected $routes;
protected $router;

protected $packageDir;
protected $prefix;
protected $dotPrefix;
protected $storagePath;
Expand All @@ -45,7 +44,6 @@ public function __construct(Router $router)
$this->storagePath = storage_path(). '/templates/apidocs';
$this->router = $router;
$this->routes = $router->getRoutes();
$this->packageDir = __DIR__ . '/../';
}


Expand Down Expand Up @@ -134,6 +132,7 @@ protected function getEndpointMethods($endpoints){

$reflector = new ReflectionClass($class);
$docBlock = new DocBlock($reflector->getMethod($methodName));
$controllerDocBlock = new DocBlock($reflector);

$endpointNameCamelCase= $this->convertToSnakeCase($endpointName);
$endpointNameCamelCasePlural = $this->convertToSnakeCase($endpointName) . 's';
Expand All @@ -145,6 +144,7 @@ protected function getEndpointMethods($endpoints){

$route['function'] = $methodName;
$route['docBlock'] = $docBlock;
$route['controllerDocBlock'] = $controllerDocBlock;

array_push($endpoints["${endpointName}"]['methods'], $route);
}
Expand Down Expand Up @@ -185,25 +185,25 @@ protected function generateHTMLCode($endpoints)
/*
* Docs Index
*/
$this->updatePrefixAndSaveTemplate('docs', $this->packageDir . Config::get('apidocs.index_template_path'));
$this->updatePrefixAndSaveTemplate('docs', Config::get('apidocs.index_template_path'));

/*
* Default Layout
*/

$this->updatePrefixAndSaveTemplate('layouts', $this->packageDir . Config::get('apidocs.default_layout_template_path'));
$this->updatePrefixAndSaveTemplate('layouts', Config::get('apidocs.default_layout_template_path'));

/*
* Head
*/

$this->updatePrefixAndSaveTemplate('includes', $this->packageDir . Config::get('apidocs.head_template_path'));
$this->updatePrefixAndSaveTemplate('includes', Config::get('apidocs.head_template_path'));

/*
* Introduction
*/

$this->updatePrefixAndSaveTemplate('includes', $this->packageDir . Config::get('apidocs.introduction_template_path'));
$this->updatePrefixAndSaveTemplate('includes', Config::get('apidocs.introduction_template_path'));

// let's generate the body
$content = $this->createContentForTemplate($endpoints);
Expand Down Expand Up @@ -257,7 +257,7 @@ protected function updateAndSaveDefaultLayoutTemplate($content){

$type = 'layouts';

$path = $this->packageDir . Config::get('apidocs.default_layout_template_path');
$path = Config::get('apidocs.default_layout_template_path');

$file = File::get($path);
$file = str_replace('{prefix}', $this->dotPrefix, $file);
Expand Down Expand Up @@ -314,7 +314,7 @@ private function generateAssetsDirectory()
// create assets directory
File::makeDirectory($destinationPath, $mode = 0777, true, true);

$targetPath = $this->packageDir . Config::get('apidocs.assets_path');
$targetPath = Config::get('apidocs.assets_path');
$directories = ['css', 'img', 'js'];

foreach ($directories as $directory)
Expand Down Expand Up @@ -351,10 +351,10 @@ private function createContentForTemplate($endpoints = array())
$navSections = '';
$navItems = '';

$navSections .= File::get($this->packageDir . config::get('apidocs.navigation_template_path'));
$navSections .= File::get(config::get('apidocs.navigation_template_path'));
$navSections = str_replace('{column-title}', $sectionName, $navSections);

$sectionHead .= File::get($this->packageDir . config::get('apidocs.section_header_template_path'));
$sectionHead .= File::get(config::get('apidocs.section_header_template_path'));
$sectionHead = str_replace('{column-name}', $sectionName, $sectionHead);
$sectionHead = str_replace('{main-description}', $endpoints[$endpoint_name]['description'], $sectionHead);

Expand All @@ -364,14 +364,13 @@ private function createContentForTemplate($endpoints = array())
foreach ($array['methods'] as $key => $value) {

$endpoint = $value;

$uri = explode(' ', $endpoint['uri']);

$navItems .= File::get($this->packageDir . config::get('apidocs.nav_items_template_path'));
$navItems .= File::get(config::get('apidocs.nav_items_template_path'));
$navItems = str_replace('{column-title}', $sectionName, $navItems);
$navItems = str_replace('{function}', $endpoint['function'], $navItems);

$sectionItem .= File::get($this->packageDir . config::get('apidocs.body_content_template_path'));
$sectionItem .= File::get(config::get('apidocs.body_content_template_path'));
$sectionItem = str_replace('{column-name}', $sectionName, $sectionItem);
$sectionItem = str_replace('{request-type}', $endpoint['method'], $sectionItem);
$sectionItem = str_replace('{endpoint-short-description}', $endpoint['docBlock']->getShortDescription(), $sectionItem);
Expand All @@ -380,16 +379,20 @@ private function createContentForTemplate($endpoints = array())
$sectionItem = str_replace('{function}', $endpoint['function'], $sectionItem);
$sectionItem = str_replace('{request-uri}', end($uri), $sectionItem);

$params = $endpoint['docBlock']->getTagsByName('param');
$method_params = $endpoint['docBlock']->getTagsByName('param');
$controller_params = $endpoint['controllerDocBlock']->getTagsByName('param');

$params = array_merge($method_params, $controller_params);

$parameters = '';

foreach ($params as $param)
{

$param_name = str_replace($param->getDescription(), '', $param->getContent());
$param_name = str_replace($param->getType(), '', $param_name);
$param_name = str_replace(' ', '', $param_name);
$param_name = urldecode($param_name);

if($param_name[0] == '$'){
$param_name = str_replace('$', '', $param_name);
}
Expand All @@ -398,14 +401,25 @@ private function createContentForTemplate($endpoints = array())
$param_name .= '[]';
}

$parameters .= File::get($this->packageDir . config::get('apidocs.parameters_template_path'));
$li_id = $endpoint['function'].'-'.$param_name;


preg_match_all("/(API:)\w+(([\s]|[\n])){0,1}/", $param->getDescription(), $output_array);
$configs = $output_array[0];
$parameters .= File::get(config::get('apidocs.parameters_template_path'));

$parameters = str_replace('{li-id}', $li_id, $parameters);
$parameters = str_replace('{param-name}', $param_name , $parameters);
$parameters = str_replace('{param-type}', $param->getType(), $parameters);
$parameters = str_replace('{param-desc}', $param->getDescription(), $parameters);

if(strpos(strtolower($param_name),'password') !== false ){
$parameters = str_replace('type="text" class="parameter-value-text" name="' . $param_name . '"', 'type="password" class="parameter-value-text" name="'. $param_name . '"' , $parameters);
}

if(strpos(strtolower($param_name),'file') !== false ){
$parameters = str_replace('type="text" class="parameter-value-text" name="' . $param_name . '"', 'type="file" class="parameter-value-text" name="'. $param_name . '"' , $parameters);
}
}

if(strlen($parameters) > 0){
Expand All @@ -422,6 +436,17 @@ private function createContentForTemplate($endpoints = array())
</li>
{request-parameters}
</ul>', '', $sectionItem);


}

foreach ($configs as $conf) {
$prop = explode(':', $conf)[1];
switch ($prop) {
case 'HIDDEN':
$sectionItem = str_replace('id="'.$li_id.'"', 'id="'.$li_id.'" style="display:none"', $sectionItem);
break;
}
}

}
Expand All @@ -435,7 +460,7 @@ private function createContentForTemplate($endpoints = array())

$navigation .= $navSections;

$bodySection .= File::get($this->packageDir . config::get('apidocs.compile_content_template_path'));
$bodySection .= File::get(config::get('apidocs.compile_content_template_path'));
$bodySection = str_replace('{section-header}', $sectionHead, $bodySection);
$bodySection = str_replace('{section-details}', $sectionItem, $bodySection);

Expand Down Expand Up @@ -490,6 +515,8 @@ protected function getRoutes()
* @param \Illuminate\Routing\Route $route
* @return string
*/

/*
protected function getBeforeFilters($route)
{
$before = array_keys($route->beforeFilters());
Expand All @@ -498,6 +525,7 @@ protected function getBeforeFilters($route)

return implode(', ', $before);
}
*/

/**
* Get all of the pattern filters matching the route.
Expand Down Expand Up @@ -539,8 +567,8 @@ protected function getRouteInformation(Route $route)
'uri' => $uri,
'name' => $route->getName(),
'action' => $route->getActionName(),
'before' => $this->getBeforeFilters($route),
'after' => $this->getAfterFilters($route),
// 'before' => $this->getBeforeFilters($route),
// 'after' => $this->getAfterFilters($route),
'prefix' => $route->getPrefix(),
'method' => $route->methods()[0],
));
Expand Down Expand Up @@ -580,10 +608,12 @@ protected function getMethodPatterns($uri, $method)
* @param \Illuminate\Routing\Route $route
* @return string
*/
/*
protected function getAfterFilters($route)
{
return implode(', ', array_keys($route->afterFilters()));
}
*/

/**
* Converts a CamelCase String to Snake Case
Expand Down
17 changes: 14 additions & 3 deletions src/Commands/ApiDocsGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ public function __construct(ApiDocsGenerator $generator)
*/
public function fire()
{

$prefix = is_null($this->argument('prefix')) ? $this->ask('What is the API Prefix? i.e. "api/v1"') : $this->argument('prefix');
$this->info('Generating ' . $prefix . ' API Documentation.');
if (!is_null($this->argument('prefix'))) {
// Command line argument takes 1st precedence.
$prefix = $this->argument('prefix');
}
else {
// Check for an environment variable, so you don't have to type
// the prefix in each time. Otherwise, ask them.
if (!empty(getenv('APIDOCS_PREFIX'))) {
$prefix = getenv('APIDOCS_PREFIX');
} else {
$prefix = $this->ask('What is the API Prefix? i.e. "api/v1"');
}
}
$this->info('Generating ' . $prefix . ' API Documentation.');

// generate the docs
$this->generator->make($prefix);
Expand Down
44 changes: 44 additions & 0 deletions src/config/apidocs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Where the templates for the API Doc HTML are stored...
|--------------------------------------------------------------------------
|
*/

// head
'index_template_path' => 'vendor/f2m2/apidocs/src/templates/docs/index.blade.php',
'head_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/head.blade.php',
'introduction_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/introduction.blade.php',
'body_content_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/body_content.blade.php',
'compile_content_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/compile_content.blade.php',
'nav_items_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/nav_items.blade.php',
'navigation_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/navigation.blade.php',
'parameters_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/parameters.blade.php',
'section_header_template_path' => 'vendor/f2m2/apidocs/src/templates/includes/section_header.blade.php',
'default_layout_template_path' => 'vendor/f2m2/apidocs/src/templates/layouts/default.blade.php',


/*
|--------------------------------------------------------------------------
| Where the Assets are stored
|--------------------------------------------------------------------------
|
*/

'assets_path' => 'vendor/f2m2/apidocs/src/templates/assets/',
'logo_path' => '/assets/docs/{prefix}/img/f2m2_logo.svg',

/*
|--------------------------------------------------------------------------
| Where the generated HTML Files will be saved...
|--------------------------------------------------------------------------
|
*/

'view_target_path' => app_path('views'),

];
44 changes: 0 additions & 44 deletions src/config/config.php

This file was deleted.

Loading