Skip to content

Commit

Permalink
introducing library to handle version+name (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Idrinth committed Aug 12, 2020
1 parent 1bcd9b9 commit 078c9c0
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 157 deletions.
26 changes: 0 additions & 26 deletions src/Implementations/Composer.php

This file was deleted.

44 changes: 9 additions & 35 deletions src/Implementations/Controller.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Implementations;

use De\Idrinth\PhalconRoutes2OpenApi\Interfaces\Controller as ControllerInterface;
use De\Idrinth\PhalconRoutes2OpenApi\Interfaces\Path2PathConverter;
use De\Idrinth\PhalconRoutes2OpenApi\Interfaces\RecursiveMerger;
use De\Idrinth\PhalconRoutes2OpenApi\Interfaces\RecursiveMerger as RMI;
use PackageVersions\Versions;
use Phalcon\Http\ResponseInterface;
use Phalcon\Mvc\Controller as PhalconController;

class Controller extends PhalconController implements ControllerInterface
{
/**
* @var string
*/
private $root;

/**
* @var array
*/
private static $body = [
"openapi"=> "3.0.1",
"info"=> [
"title"=> "unknown",
"version"=> "1.0.0"
]
];

/**
* Generates an overview over routes registered
* @return-200 application/json {"type":"object"}
Expand All @@ -38,17 +23,16 @@ public function index(): ResponseInterface
foreach ($this->router->getRoutes() as $route) {
$paths[] = $converter->convert($route);
}
$merger = $this->di->get(RecursiveMerger::class);
return $this
->getCorsEnabledResponse()
->setJsonContent(
$merger->merge(
self::$body,
[
'paths' => $merger->mergeAll(...$paths),
'info' => (new Composer())(dirname(__DIR__, 5).'/composer.json')
[
'paths' => $this->di->get(RMI::class)->mergeAll(...$paths),
'info' => [
"title"=> Versions::ROOT_PACKAGE_NAME,
"version"=> Versions::getVersion(Versions::ROOT_PACKAGE_NAME)
]
)
]
);
}

Expand Down Expand Up @@ -96,14 +80,4 @@ private function getCorsEnabledResponse(): ResponseInterface
)
->setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}

/**
* @param string $root
* @return ControllerInterface
*/
public function setRoot(string $root): ControllerInterface
{
$this->root = $root;
return $this;
}
}
2 changes: 1 addition & 1 deletion src/Implementations/DefaultResponse.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Implementations;

Expand Down
22 changes: 4 additions & 18 deletions src/Implementations/NoValueConversionMerger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Implementations;

Expand Down Expand Up @@ -42,26 +42,12 @@ public function merge(array $array1, array $array2): array
* @return array
* @throws InvalidArgumentException
*/
public function mergeAll(...$sets): array
public function mergeAll(array ...$sets): array
{
$initial = $this->checkArray(array_shift($sets), 1);
$initial = array_shift($sets);
foreach ($sets as $pos => $set) {
$initial = $this->merge($initial, $this->checkArray($set, $pos+2));
$initial = $this->merge($initial, $set);
}
return $initial;
}

/**
* @param mixed $set
* @param int $num
* @throws InvalidArgumentException
* @return array
*/
private function checkArray($set, int $num): array
{
if (!is_array($set)) {
throw new InvalidArgumentException("Set #$num is not an array, but a(n) " . gettype($set) . '.');
}
return $set;
}
}
2 changes: 1 addition & 1 deletion src/Implementations/PhalconPath2PathArray.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Implementations;

Expand Down
6 changes: 3 additions & 3 deletions src/Implementations/Reflector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Implementations;

Expand Down Expand Up @@ -46,11 +46,11 @@ public function __invoke(string $class, string $method):array
{
try {
if (!isset($this->cache[$class])) {
$this->cache[$class]['____class'] = new ReflectionClass($class);
$this->cache[$class]['#'] = new ReflectionClass($class);
}
if (!isset($this->cache[$class][$method])) {
$this->cache[$class][$method] = DefaultResponse::add(
$this->getReflect($this->cache[$class]['____class'], $method)
$this->getReflect($this->cache[$class]['#'], $method)
);
}
} catch (Exception $e) {
Expand Down
8 changes: 1 addition & 7 deletions src/Interfaces/Controller.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Interfaces;

use Phalcon\Http\ResponseInterface;

interface Controller
{
/**
* Sets the api's root, used for filtering
* @param string $root
*/
public function setRoot(string $root):Controller;

/**
* generates api-documentation
* @return ResponseInterface
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/Path2PathConverter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Interfaces;

use Phalcon\Mvc\Router\RouteInterface;

interface Path2PathConverter
{
public function convert(RouteInterface $route):array;
public function convert(RouteInterface $route): array;
}
4 changes: 2 additions & 2 deletions src/Interfaces/PathTargetAnnotationResolver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Interfaces;

Expand All @@ -10,5 +10,5 @@ interface PathTargetAnnotationResolver
* @param string $method
* @return array
*/
public function __invoke(string $class, string $method):array;
public function __invoke(string $class, string $method): array;
}
4 changes: 2 additions & 2 deletions src/Interfaces/RecursiveMerger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi\Interfaces;

Expand All @@ -15,5 +15,5 @@ public function merge(array $array1, array $array2): array;
* @param array ...$sets each array to be merged into the first as a parameter
* @return array
*/
public function mergeAll(...$sets): array;
public function mergeAll(array ...$sets): array;
}
18 changes: 5 additions & 13 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace De\Idrinth\PhalconRoutes2OpenApi;

Expand All @@ -18,22 +18,14 @@

class ServiceProvider implements ServiceProviderInterface
{
/**
* @param string $apiRoot
*/
public function __construct(string $apiRoot = '/')
{
$this->apiRoot = ($apiRoot{0}==='/' ? '' : '/') . $apiRoot;
}

/**
* Registers controller at api-root
* @param DiInterface $serviceContainer
* @return void
*/
public function register(DiInterface $serviceContainer)
{
$this->registerServices($serviceContainer, $this->apiRoot);
$this->registerServices($serviceContainer);
$this->registerRoutes($serviceContainer->get('router'));
}

Expand All @@ -42,10 +34,10 @@ public function register(DiInterface $serviceContainer)
* @param string $root
* @return void
*/
private function registerServices(DiInterface $serviceContainer, string $root)
private function registerServices(DiInterface $serviceContainer)
{
$serviceContainer->set(Controller::class, function () use ($root) {
return (new ControllerImplementation())->setRoot($root);
$serviceContainer->set(Controller::class, function () {
return (new ControllerImplementation());
});
$serviceContainer->set(Path2PathConverter::class, function () use (&$serviceContainer) {
return new PhalconPath2PathArray(
Expand Down
25 changes: 0 additions & 25 deletions test/ComposerTest.php

This file was deleted.

Loading

0 comments on commit 078c9c0

Please sign in to comment.