Skip to content

Commit

Permalink
finalize structure code and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaselia committed Dec 24, 2020
1 parent 963ea8c commit cf9aa8f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 53 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
/vendor
composer.lock
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ To use the command simply run:
php artisan export:postman
```

The output Postman collection will have a `base_url` variable set by default for ease of use.

## Extra Usage
You can also supply the `--structured` option to nest the routes into folders based on their name, or `--bearer` to create a bearer authorization token which can be managed in a single place within variables.

```bash
--structured # nest the routes into folders based on relationships
--bearer # create a bearer authorization token in variables and all routes that require it
```
The output Postman collection will have a `base_url` variable set by default for ease of use.

## Contributing

Expand Down
89 changes: 44 additions & 45 deletions src/ExportPostman.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,28 @@ public function handle(): void
$structured = $this->option('structured') ?? false;
$bearer = $this->option('bearer') ?? false;

$filename = date('Y_m_d_His') . '_postman';

$variables = [
[
'key' => 'base_url',
'value' => 'https://api.example.com/',
$this->routes = [
'variable' => [
[
'key' => 'base_url',
'value' => 'https://api.example.com/',
],
],
'info' => [
'name' => $filename = date('Y_m_d_His') . '_postman',
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
],
'item' => [],
];

if ($bearer) {
$variables[] = [
$this->routes['variable'][] = [
'key' => 'token',
'value' => '1|token',
];
}

$this->routes = [
'variable' => $variables,
'info' => [
'name' => $filename,
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
],
'item' => [],
];

$routerRoutes = $this->router->getRoutes();

foreach ($routerRoutes as $route) {
foreach ($this->router->getRoutes() as $route) {
$middleware = $route->middleware();

foreach ($route->methods as $method) {
Expand All @@ -78,6 +72,12 @@ public function handle(): void
];
}

$request = $this->makeItem($route, $method, $routeHeaders);

if (!$structured) {
$this->routes['item'][] = $request;
}

if ($structured) {
$not = ['index', 'show', 'store', 'update', 'destroy'];

Expand All @@ -89,7 +89,7 @@ public function handle(): void

$destination = end($routeNames);

$this->nestedRoutes($routeNames, $route, $method, $routeHeaders, $destination);
$this->ensurePath($this->routes, $routeNames, $request, $destination);
}
}
}
Expand All @@ -99,41 +99,40 @@ public function handle(): void
$this->info("Postman Collection Exported: $exportName");
}

public function nestedRoutes(&$routeNames, $route, $method, $routeHeaders, $destination)
protected function ensurePath(array &$root, array $segments, array $request, string $destination): void
{
$item = $this->makeItem($route, $method, $routeHeaders);
$parent = &$root;

if (empty($routeNames)) {
return $item;
}
foreach ($segments as $segment) {
$matched = false;

$index = array_shift($routeNames);
foreach ($parent['item'] as &$item) {
if ($item['name'] === $segment) {
$parent = &$item;

if ($this->findKeyValue($this->routes, 'name', $index, $item, $destination)) {
return;
}

$this->routes['item'][] = [
'name' => $index,
'item' => [$this->nestedRoutes($routeNames, $route, $method, $routeHeaders, $destination)],
];
}
if ($segment === $destination) {
$parent['item'][] = $request;
}

public function findKeyValue(&$array, $key, $val, $template, $destination)
{
foreach ($array as &$item) {
if (is_array($item) && $this->findKeyValue($item, $key, $val, $template, $destination)) {
return true;
$matched = true;
break;
}
}

if (isset($item[$key]) && $item[$key] == $val) {
$item['item'][] = $template;
unset($item);

return true;
if (!$matched) {
$item = [
'name' => $segment,
'item' => [$request],
];

$parent['item'][] = &$item;
$parent = &$item;
}
}

return false;
unset($item);
}
}

public function makeItem($route, $method, $routeHeaders)
Expand Down

0 comments on commit cf9aa8f

Please sign in to comment.